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.
29 #include "command-line.h"
34 #include "dynamic-string.h"
37 #include "lib/table.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-error.h"
44 #include "stream-ssl.h"
50 VLOG_DEFINE_THIS_MODULE(ovsdb_client
);
53 NEED_NONE
, /* No JSON-RPC connection or database name needed. */
54 NEED_RPC
, /* JSON-RPC connection needed. */
55 NEED_DATABASE
/* JSON-RPC connection and database name needed. */
58 struct ovsdb_client_command
{
60 enum args_needed need
;
63 void (*handler
)(struct jsonrpc
*rpc
, const char *database
,
64 int argc
, char *argv
[]);
67 /* Format for table output. */
68 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
70 static const struct ovsdb_client_command all_commands
[];
72 static void usage(void) NO_RETURN
;
73 static void parse_options(int argc
, char *argv
[]);
74 static struct jsonrpc
*open_jsonrpc(const char *server
);
75 static void fetch_dbs(struct jsonrpc
*, struct sset
*dbs
);
78 main(int argc
, char *argv
[])
80 const struct ovsdb_client_command
*command
;
84 proctitle_init(argc
, argv
);
85 set_program_name(argv
[0]);
86 parse_options(argc
, argv
);
87 signal(SIGPIPE
, SIG_IGN
);
90 ovs_fatal(0, "missing command name; use --help for help");
93 for (command
= all_commands
; ; command
++) {
95 VLOG_FATAL("unknown command '%s'; use --help for help",
97 } else if (!strcmp(command
->name
, argv
[optind
])) {
103 if (command
->need
!= NEED_NONE
) {
104 if (argc
- optind
> command
->min_args
105 && (isalpha((unsigned char) argv
[optind
][0])
106 && strchr(argv
[optind
], ':'))) {
107 rpc
= open_jsonrpc(argv
[optind
++]);
109 char *sock
= xasprintf("unix:%s/db.sock", ovs_rundir());
110 rpc
= open_jsonrpc(sock
);
117 if (command
->need
== NEED_DATABASE
) {
121 fetch_dbs(rpc
, &dbs
);
122 if (argc
- optind
> command
->min_args
123 && sset_contains(&dbs
, argv
[optind
])) {
124 database
= argv
[optind
++];
125 } else if (sset_count(&dbs
) == 1) {
126 database
= xstrdup(SSET_FIRST(&dbs
));
127 } else if (sset_contains(&dbs
, "Open_vSwitch")) {
128 database
= "Open_vSwitch";
130 ovs_fatal(0, "no default database for `%s' command, please "
131 "specify a database name", command
->name
);
138 if (argc
- optind
< command
->min_args
||
139 argc
- optind
> command
->max_args
) {
140 VLOG_FATAL("invalid syntax for '%s' (use --help for help)",
144 command
->handler(rpc
, database
, argc
- optind
, argv
+ optind
);
148 if (ferror(stdout
)) {
149 VLOG_FATAL("write to stdout failed");
151 if (ferror(stderr
)) {
152 VLOG_FATAL("write to stderr failed");
159 parse_options(int argc
, char *argv
[])
162 OPT_BOOTSTRAP_CA_CERT
= UCHAR_MAX
+ 1,
166 static struct option long_options
[] = {
167 {"verbose", optional_argument
, NULL
, 'v'},
168 {"help", no_argument
, NULL
, 'h'},
169 {"version", no_argument
, NULL
, 'V'},
172 {"bootstrap-ca-cert", required_argument
, NULL
, OPT_BOOTSTRAP_CA_CERT
},
173 STREAM_SSL_LONG_OPTIONS
,
178 char *short_options
= long_options_to_short_options(long_options
);
183 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
193 ovs_print_version(0, 0);
197 vlog_set_verbosity(optarg
);
200 DAEMON_OPTION_HANDLERS
202 TABLE_OPTION_HANDLERS(&table_style
)
204 STREAM_SSL_OPTION_HANDLERS
206 case OPT_BOOTSTRAP_CA_CERT
:
207 stream_ssl_set_ca_cert_file(optarg
, true);
214 /* getopt_long() already set the value for us. */
227 printf("%s: Open vSwitch database JSON-RPC client\n"
228 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
229 "\nValid commands are:\n"
230 "\n list-dbs [SERVER]\n"
231 " list databases available on SERVER\n"
232 "\n get-schema [SERVER] [DATABASE]\n"
233 " retrieve schema for DATABASE from SERVER\n"
234 "\n get-schema-version [SERVER] [DATABASE]\n"
235 " retrieve schema for DATABASE from SERVER and report only its\n"
236 " version number on stdout\n"
237 "\n list-tables [SERVER] [DATABASE]\n"
238 " list tables for DATABASE on SERVER\n"
239 "\n list-columns [SERVER] [DATABASE] [TABLE]\n"
240 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
241 "\n transact [SERVER] TRANSACTION\n"
242 " run TRANSACTION (a JSON array of operations) on SERVER\n"
243 " and print the results as JSON on stdout\n"
244 "\n monitor [SERVER] [DATABASE] TABLE [COLUMN,...]...\n"
245 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
246 " COLUMNs may include !initial, !insert, !delete, !modify\n"
247 " to avoid seeing the specified kinds of changes.\n"
248 "\n dump [SERVER] [DATABASE]\n"
249 " dump contents of DATABASE on SERVER to stdout\n"
250 "\nThe default SERVER is unix:%s/db.sock.\n"
251 "The default DATABASE is Open_vSwitch.\n",
252 program_name
, program_name
, ovs_rundir());
253 stream_usage("SERVER", true, true, true);
254 printf("\nOutput formatting options:\n"
255 " -f, --format=FORMAT set output formatting to FORMAT\n"
256 " (\"table\", \"html\", \"csv\", "
258 " --no-headings omit table heading row\n"
259 " --pretty pretty-print JSON in output");
262 printf("\nOther options:\n"
263 " -h, --help display this help message\n"
264 " -V, --version display version information\n");
269 parse_json(const char *s
)
271 struct json
*json
= json_from_string(s
);
272 if (json
->type
== JSON_STRING
) {
273 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
278 static struct jsonrpc
*
279 open_jsonrpc(const char *server
)
281 struct stream
*stream
;
284 error
= stream_open_block(jsonrpc_stream_open(server
, &stream
), &stream
);
285 if (error
== EAFNOSUPPORT
) {
286 struct pstream
*pstream
;
288 error
= jsonrpc_pstream_open(server
, &pstream
);
290 ovs_fatal(error
, "failed to connect or listen to \"%s\"", server
);
293 VLOG_INFO("%s: waiting for connection...", server
);
294 error
= pstream_accept_block(pstream
, &stream
);
296 ovs_fatal(error
, "failed to accept connection on \"%s\"", server
);
299 pstream_close(pstream
);
301 ovs_fatal(error
, "failed to connect to \"%s\"", server
);
304 return jsonrpc_open(stream
);
308 print_json(struct json
*json
)
310 char *string
= json_to_string(json
, table_style
.json_flags
);
311 fputs(string
, stdout
);
316 print_and_free_json(struct json
*json
)
323 check_ovsdb_error(struct ovsdb_error
*error
)
326 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
330 static struct ovsdb_schema
*
331 fetch_schema(struct jsonrpc
*rpc
, const char *database
)
333 struct jsonrpc_msg
*request
, *reply
;
334 struct ovsdb_schema
*schema
;
337 request
= jsonrpc_create_request("get_schema",
339 json_string_create(database
)),
341 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
343 ovs_fatal(error
, "transaction failed");
345 check_ovsdb_error(ovsdb_schema_from_json(reply
->result
, &schema
));
346 jsonrpc_msg_destroy(reply
);
352 fetch_dbs(struct jsonrpc
*rpc
, struct sset
*dbs
)
354 struct jsonrpc_msg
*request
, *reply
;
358 request
= jsonrpc_create_request("list_dbs", json_array_create_empty(),
360 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
362 ovs_fatal(error
, "transaction failed");
365 if (reply
->result
->type
!= JSON_ARRAY
) {
366 ovs_fatal(0, "list_dbs response is not array");
369 for (i
= 0; i
< reply
->result
->u
.array
.n
; i
++) {
370 const struct json
*name
= reply
->result
->u
.array
.elems
[i
];
372 if (name
->type
!= JSON_STRING
) {
373 ovs_fatal(0, "list_dbs response %zu is not string", i
);
375 sset_add(dbs
, name
->u
.string
);
377 jsonrpc_msg_destroy(reply
);
381 do_list_dbs(struct jsonrpc
*rpc
, const char *database OVS_UNUSED
,
382 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
388 fetch_dbs(rpc
, &dbs
);
389 SSET_FOR_EACH (db_name
, &dbs
) {
396 do_get_schema(struct jsonrpc
*rpc
, const char *database
,
397 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
399 struct ovsdb_schema
*schema
= fetch_schema(rpc
, database
);
400 print_and_free_json(ovsdb_schema_to_json(schema
));
401 ovsdb_schema_destroy(schema
);
405 do_get_schema_version(struct jsonrpc
*rpc
, const char *database
,
406 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
408 struct ovsdb_schema
*schema
= fetch_schema(rpc
, database
);
409 puts(schema
->version
);
410 ovsdb_schema_destroy(schema
);
414 do_list_tables(struct jsonrpc
*rpc
, const char *database
,
415 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
417 struct ovsdb_schema
*schema
;
418 struct shash_node
*node
;
421 schema
= fetch_schema(rpc
, database
);
423 table_add_column(&t
, "Table");
424 SHASH_FOR_EACH (node
, &schema
->tables
) {
425 struct ovsdb_table_schema
*ts
= node
->data
;
428 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
430 ovsdb_schema_destroy(schema
);
431 table_print(&t
, &table_style
);
435 do_list_columns(struct jsonrpc
*rpc
, const char *database
,
436 int argc OVS_UNUSED
, char *argv
[])
438 const char *table_name
= argv
[0];
439 struct ovsdb_schema
*schema
;
440 struct shash_node
*table_node
;
443 schema
= fetch_schema(rpc
, database
);
446 table_add_column(&t
, "Table");
448 table_add_column(&t
, "Column");
449 table_add_column(&t
, "Type");
450 SHASH_FOR_EACH (table_node
, &schema
->tables
) {
451 struct ovsdb_table_schema
*ts
= table_node
->data
;
453 if (!table_name
|| !strcmp(table_name
, ts
->name
)) {
454 struct shash_node
*column_node
;
456 SHASH_FOR_EACH (column_node
, &ts
->columns
) {
457 const struct ovsdb_column
*column
= column_node
->data
;
461 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
463 table_add_cell(&t
)->text
= xstrdup(column
->name
);
464 table_add_cell(&t
)->json
= ovsdb_type_to_json(&column
->type
);
468 ovsdb_schema_destroy(schema
);
469 table_print(&t
, &table_style
);
473 do_transact(struct jsonrpc
*rpc
, const char *database OVS_UNUSED
,
474 int argc OVS_UNUSED
, char *argv
[])
476 struct jsonrpc_msg
*request
, *reply
;
477 struct json
*transaction
;
480 transaction
= parse_json(argv
[0]);
482 request
= jsonrpc_create_request("transact", transaction
, NULL
);
483 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
485 ovs_fatal(error
, "transaction failed");
488 ovs_fatal(error
, "transaction returned error: %s",
489 json_to_string(reply
->error
, table_style
.json_flags
));
491 print_json(reply
->result
);
493 jsonrpc_msg_destroy(reply
);
497 monitor_print_row(struct json
*row
, const char *type
, const char *uuid
,
498 const struct ovsdb_column_set
*columns
, struct table
*t
)
503 ovs_error(0, "missing %s row", type
);
505 } else if (row
->type
!= JSON_OBJECT
) {
506 ovs_error(0, "<row> is not object");
511 table_add_cell(t
)->text
= xstrdup(uuid
);
512 table_add_cell(t
)->text
= xstrdup(type
);
513 for (i
= 0; i
< columns
->n_columns
; i
++) {
514 const struct ovsdb_column
*column
= columns
->columns
[i
];
515 struct json
*value
= shash_find_data(json_object(row
), column
->name
);
516 struct cell
*cell
= table_add_cell(t
);
518 cell
->json
= json_clone(value
);
519 cell
->type
= &column
->type
;
525 monitor_print(struct json
*table_updates
,
526 const struct ovsdb_table_schema
*table
,
527 const struct ovsdb_column_set
*columns
, bool initial
)
529 struct json
*table_update
;
530 struct shash_node
*node
;
536 if (table_updates
->type
!= JSON_OBJECT
) {
537 ovs_error(0, "<table-updates> is not object");
540 table_update
= shash_find_data(json_object(table_updates
), table
->name
);
544 if (table_update
->type
!= JSON_OBJECT
) {
545 ovs_error(0, "<table-update> is not object");
549 table_add_column(&t
, "row");
550 table_add_column(&t
, "action");
551 for (i
= 0; i
< columns
->n_columns
; i
++) {
552 table_add_column(&t
, "%s", columns
->columns
[i
]->name
);
554 SHASH_FOR_EACH (node
, json_object(table_update
)) {
555 struct json
*row_update
= node
->data
;
556 struct json
*old
, *new;
558 if (row_update
->type
!= JSON_OBJECT
) {
559 ovs_error(0, "<row-update> is not object");
562 old
= shash_find_data(json_object(row_update
), "old");
563 new = shash_find_data(json_object(row_update
), "new");
565 monitor_print_row(new, "initial", node
->name
, columns
, &t
);
567 monitor_print_row(new, "insert", node
->name
, columns
, &t
);
569 monitor_print_row(old
, "delete", node
->name
, columns
, &t
);
571 monitor_print_row(old
, "old", node
->name
, columns
, &t
);
572 monitor_print_row(new, "new", "", columns
, &t
);
575 table_print(&t
, &table_style
);
580 add_column(const char *server
, const struct ovsdb_column
*column
,
581 struct ovsdb_column_set
*columns
, struct json
*columns_json
)
583 if (ovsdb_column_set_contains(columns
, column
->index
)) {
584 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
585 server
, column
->name
);
587 ovsdb_column_set_add(columns
, column
);
588 json_array_add(columns_json
, json_string_create(column
->name
));
592 parse_monitor_columns(char *arg
, const char *server
, const char *database
,
593 const struct ovsdb_table_schema
*table
,
594 struct ovsdb_column_set
*columns
)
596 bool initial
, insert
, delete, modify
;
597 struct json
*mr
, *columns_json
;
598 char *save_ptr
= NULL
;
601 mr
= json_object_create();
602 columns_json
= json_array_create_empty();
603 json_object_put(mr
, "columns", columns_json
);
605 initial
= insert
= delete = modify
= true;
606 for (token
= strtok_r(arg
, ",", &save_ptr
); token
!= NULL
;
607 token
= strtok_r(NULL
, ",", &save_ptr
)) {
608 if (!strcmp(token
, "!initial")) {
610 } else if (!strcmp(token
, "!insert")) {
612 } else if (!strcmp(token
, "!delete")) {
614 } else if (!strcmp(token
, "!modify")) {
617 const struct ovsdb_column
*column
;
619 column
= ovsdb_table_schema_get_column(table
, token
);
621 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
622 "column named \"%s\"",
623 server
, table
->name
, database
, token
);
625 add_column(server
, column
, columns
, columns_json
);
629 if (columns_json
->u
.array
.n
== 0) {
630 const struct shash_node
**nodes
;
633 n
= shash_count(&table
->columns
);
634 nodes
= shash_sort(&table
->columns
);
635 for (i
= 0; i
< n
; i
++) {
636 const struct ovsdb_column
*column
= nodes
[i
]->data
;
637 if (column
->index
!= OVSDB_COL_UUID
638 && column
->index
!= OVSDB_COL_VERSION
) {
639 add_column(server
, column
, columns
, columns_json
);
644 add_column(server
, ovsdb_table_schema_get_column(table
,"_version"),
645 columns
, columns_json
);
648 if (!initial
|| !insert
|| !delete || !modify
) {
649 struct json
*select
= json_object_create();
650 json_object_put(select
, "initial", json_boolean_create(initial
));
651 json_object_put(select
, "insert", json_boolean_create(insert
));
652 json_object_put(select
, "delete", json_boolean_create(delete));
653 json_object_put(select
, "modify", json_boolean_create(modify
));
654 json_object_put(mr
, "select", select
);
661 do_monitor(struct jsonrpc
*rpc
, const char *database
,
662 int argc
, char *argv
[])
664 const char *server
= jsonrpc_get_name(rpc
);
665 const char *table_name
= argv
[0];
666 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
667 struct ovsdb_table_schema
*table
;
668 struct ovsdb_schema
*schema
;
669 struct jsonrpc_msg
*request
;
670 struct json
*monitor
, *monitor_request_array
,
671 *monitor_requests
, *request_id
;
673 schema
= fetch_schema(rpc
, database
);
674 table
= shash_find_data(&schema
->tables
, table_name
);
676 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
677 server
, database
, table_name
);
680 monitor_request_array
= json_array_create_empty();
684 for (i
= 1; i
< argc
; i
++) {
686 monitor_request_array
,
687 parse_monitor_columns(argv
[i
], server
, database
, table
,
691 /* Allocate a writable empty string since parse_monitor_columns() is
692 * going to strtok() it and that's risky with literal "". */
695 monitor_request_array
,
696 parse_monitor_columns(empty
, server
, database
, table
, &columns
));
699 monitor_requests
= json_object_create();
700 json_object_put(monitor_requests
, table_name
, monitor_request_array
);
702 monitor
= json_array_create_3(json_string_create(database
),
703 json_null_create(), monitor_requests
);
704 request
= jsonrpc_create_request("monitor", monitor
, NULL
);
705 request_id
= json_clone(request
->id
);
706 jsonrpc_send(rpc
, request
);
708 struct jsonrpc_msg
*msg
;
711 error
= jsonrpc_recv_block(rpc
, &msg
);
713 ovsdb_schema_destroy(schema
);
714 ovs_fatal(error
, "%s: receive failed", server
);
717 if (msg
->type
== JSONRPC_REQUEST
&& !strcmp(msg
->method
, "echo")) {
718 jsonrpc_send(rpc
, jsonrpc_create_reply(json_clone(msg
->params
),
720 } else if (msg
->type
== JSONRPC_REPLY
721 && json_equal(msg
->id
, request_id
)) {
722 monitor_print(msg
->result
, table
, &columns
, true);
725 /* daemonize() closes the standard file descriptors. We output
726 * to stdout, so we need to save and restore STDOUT_FILENO. */
727 int fd
= dup(STDOUT_FILENO
);
729 dup2(fd
, STDOUT_FILENO
);
732 } else if (msg
->type
== JSONRPC_NOTIFY
733 && !strcmp(msg
->method
, "update")) {
734 struct json
*params
= msg
->params
;
735 if (params
->type
== JSON_ARRAY
736 && params
->u
.array
.n
== 2
737 && params
->u
.array
.elems
[0]->type
== JSON_NULL
) {
738 monitor_print(params
->u
.array
.elems
[1],
739 table
, &columns
, false);
743 jsonrpc_msg_destroy(msg
);
747 struct dump_table_aux
{
748 struct ovsdb_datum
**data
;
749 const struct ovsdb_column
**columns
;
754 compare_data(size_t a_y
, size_t b_y
, size_t x
,
755 const struct dump_table_aux
*aux
)
757 return ovsdb_datum_compare_3way(&aux
->data
[a_y
][x
],
759 &aux
->columns
[x
]->type
);
763 compare_rows(size_t a_y
, size_t b_y
, void *aux_
)
765 struct dump_table_aux
*aux
= aux_
;
768 /* Skip UUID columns on the first pass, since their values tend to be
769 * random and make our results less reproducible. */
770 for (x
= 0; x
< aux
->n_columns
; x
++) {
771 if (aux
->columns
[x
]->type
.key
.type
!= OVSDB_TYPE_UUID
) {
772 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
779 /* Use UUID columns as tie-breakers. */
780 for (x
= 0; x
< aux
->n_columns
; x
++) {
781 if (aux
->columns
[x
]->type
.key
.type
== OVSDB_TYPE_UUID
) {
782 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
793 swap_rows(size_t a_y
, size_t b_y
, void *aux_
)
795 struct dump_table_aux
*aux
= aux_
;
796 struct ovsdb_datum
*tmp
= aux
->data
[a_y
];
797 aux
->data
[a_y
] = aux
->data
[b_y
];
798 aux
->data
[b_y
] = tmp
;
802 compare_columns(const void *a_
, const void *b_
)
804 const struct ovsdb_column
*const *ap
= a_
;
805 const struct ovsdb_column
*const *bp
= b_
;
806 const struct ovsdb_column
*a
= *ap
;
807 const struct ovsdb_column
*b
= *bp
;
809 return strcmp(a
->name
, b
->name
);
813 dump_table(const struct ovsdb_table_schema
*ts
, struct json_array
*rows
)
815 const struct ovsdb_column
**columns
;
818 struct ovsdb_datum
**data
;
820 struct dump_table_aux aux
;
821 struct shash_node
*node
;
825 /* Sort columns by name, for reproducibility. */
826 columns
= xmalloc(shash_count(&ts
->columns
) * sizeof *columns
);
828 SHASH_FOR_EACH (node
, &ts
->columns
) {
829 struct ovsdb_column
*column
= node
->data
;
830 if (strcmp(column
->name
, "_version")) {
831 columns
[n_columns
++] = column
;
834 qsort(columns
, n_columns
, sizeof *columns
, compare_columns
);
836 /* Extract data from table. */
837 data
= xmalloc(rows
->n
* sizeof *data
);
838 for (y
= 0; y
< rows
->n
; y
++) {
841 if (rows
->elems
[y
]->type
!= JSON_OBJECT
) {
842 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
843 "%s", y
, ts
->name
, json_to_string(rows
->elems
[y
], 0));
845 row
= json_object(rows
->elems
[y
]);
847 data
[y
] = xmalloc(n_columns
* sizeof **data
);
848 for (x
= 0; x
< n_columns
; x
++) {
849 const struct json
*json
= shash_find_data(row
, columns
[x
]->name
);
851 ovs_fatal(0, "row %zu in table %s response lacks %s column",
852 y
, ts
->name
, columns
[x
]->name
);
855 check_ovsdb_error(ovsdb_datum_from_json(&data
[y
][x
],
861 /* Sort rows by column values, for reproducibility. */
863 aux
.columns
= columns
;
864 aux
.n_columns
= n_columns
;
865 sort(rows
->n
, compare_rows
, swap_rows
, &aux
);
867 /* Add column headings. */
869 table_set_caption(&t
, xasprintf("%s table", ts
->name
));
870 for (x
= 0; x
< n_columns
; x
++) {
871 table_add_column(&t
, "%s", columns
[x
]->name
);
875 for (y
= 0; y
< rows
->n
; y
++) {
877 for (x
= 0; x
< n_columns
; x
++) {
878 struct cell
*cell
= table_add_cell(&t
);
879 cell
->json
= ovsdb_datum_to_json(&data
[y
][x
], &columns
[x
]->type
);
880 cell
->type
= &columns
[x
]->type
;
883 table_print(&t
, &table_style
);
888 do_dump(struct jsonrpc
*rpc
, const char *database
,
889 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
891 struct jsonrpc_msg
*request
, *reply
;
892 struct ovsdb_schema
*schema
;
893 struct json
*transaction
;
896 const struct shash_node
**tables
;
901 schema
= fetch_schema(rpc
, database
);
902 tables
= shash_sort(&schema
->tables
);
903 n_tables
= shash_count(&schema
->tables
);
905 /* Construct transaction to retrieve entire database. */
906 transaction
= json_array_create_1(json_string_create(database
));
907 for (i
= 0; i
< n_tables
; i
++) {
908 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
909 struct json
*op
, *columns
;
910 struct shash_node
*node
;
912 columns
= json_array_create_empty();
913 SHASH_FOR_EACH (node
, &ts
->columns
) {
914 const struct ovsdb_column
*column
= node
->data
;
916 if (strcmp(column
->name
, "_version")) {
917 json_array_add(columns
, json_string_create(column
->name
));
921 op
= json_object_create();
922 json_object_put_string(op
, "op", "select");
923 json_object_put_string(op
, "table", tables
[i
]->name
);
924 json_object_put(op
, "where", json_array_create_empty());
925 json_object_put(op
, "columns", columns
);
926 json_array_add(transaction
, op
);
929 /* Send request, get reply. */
930 request
= jsonrpc_create_request("transact", transaction
, NULL
);
931 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
933 ovs_fatal(error
, "transaction failed");
936 /* Print database contents. */
937 if (reply
->result
->type
!= JSON_ARRAY
938 || reply
->result
->u
.array
.n
!= n_tables
) {
939 ovs_fatal(0, "reply is not array of %zu elements: %s",
940 n_tables
, json_to_string(reply
->result
, 0));
942 for (i
= 0; i
< n_tables
; i
++) {
943 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
944 const struct json
*op_result
= reply
->result
->u
.array
.elems
[i
];
947 if (op_result
->type
!= JSON_OBJECT
948 || !(rows
= shash_find_data(json_object(op_result
), "rows"))
949 || rows
->type
!= JSON_ARRAY
) {
950 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
952 ts
->name
, json_to_string(op_result
, 0));
955 dump_table(ts
, &rows
->u
.array
);
960 do_help(struct jsonrpc
*rpc OVS_UNUSED
, const char *database OVS_UNUSED
,
961 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
966 /* All command handlers (except for "help") are expected to take an optional
967 * server socket name (e.g. "unix:...") as their first argument. The socket
968 * name argument must be included in max_args (but left out of min_args). The
969 * command name and socket name are not included in the arguments passed to the
970 * handler: the argv[0] passed to the handler is the first argument after the
971 * optional server socket name. The connection to the server is available as
972 * global variable 'rpc'. */
973 static const struct ovsdb_client_command all_commands
[] = {
974 { "list-dbs", NEED_RPC
, 0, 0, do_list_dbs
},
975 { "get-schema", NEED_DATABASE
, 0, 0, do_get_schema
},
976 { "get-schema-version", NEED_DATABASE
, 0, 0, do_get_schema_version
},
977 { "list-tables", NEED_DATABASE
, 0, 0, do_list_tables
},
978 { "list-columns", NEED_DATABASE
, 0, 1, do_list_columns
},
979 { "transact", NEED_RPC
, 1, 1, do_transact
},
980 { "monitor", NEED_DATABASE
, 1, INT_MAX
, do_monitor
},
981 { "dump", NEED_DATABASE
, 0, 0, do_dump
},
983 { "help", NEED_NONE
, 0, INT_MAX
, do_help
},
985 { NULL
, 0, 0, 0, NULL
},