]> git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/ovsdb-client.c
Remove useless use of <assert.h>.
[mirror_ovs.git] / ovsdb / ovsdb-client.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "command-line.h"
29 #include "column.h"
30 #include "compiler.h"
31 #include "daemon.h"
32 #include "dirs.h"
33 #include "dynamic-string.h"
34 #include "json.h"
35 #include "jsonrpc.h"
36 #include "lib/table.h"
37 #include "ovsdb.h"
38 #include "ovsdb-data.h"
39 #include "ovsdb-error.h"
40 #include "sort.h"
41 #include "svec.h"
42 #include "stream.h"
43 #include "stream-ssl.h"
44 #include "table.h"
45 #include "timeval.h"
46 #include "util.h"
47 #include "vlog.h"
48
49 VLOG_DEFINE_THIS_MODULE(ovsdb_client);
50
51 enum args_needed {
52 NEED_NONE, /* No JSON-RPC connection or database name needed. */
53 NEED_RPC, /* JSON-RPC connection needed. */
54 NEED_DATABASE /* JSON-RPC connection and database name needed. */
55 };
56
57 struct ovsdb_client_command {
58 const char *name;
59 enum args_needed need;
60 int min_args;
61 int max_args;
62 void (*handler)(struct jsonrpc *rpc, const char *database,
63 int argc, char *argv[]);
64 };
65
66 /* --timestamp: Print a timestamp before each update on "monitor" command? */
67 static bool timestamp;
68
69 /* Format for table output. */
70 static struct table_style table_style = TABLE_STYLE_DEFAULT;
71
72 static const struct ovsdb_client_command all_commands[];
73
74 static void usage(void) NO_RETURN;
75 static void parse_options(int argc, char *argv[]);
76 static struct jsonrpc *open_jsonrpc(const char *server);
77 static void fetch_dbs(struct jsonrpc *, struct svec *dbs);
78
79 int
80 main(int argc, char *argv[])
81 {
82 const struct ovsdb_client_command *command;
83 const char *database;
84 struct jsonrpc *rpc;
85
86 proctitle_init(argc, argv);
87 set_program_name(argv[0]);
88 parse_options(argc, argv);
89 signal(SIGPIPE, SIG_IGN);
90
91 if (optind >= argc) {
92 ovs_fatal(0, "missing command name; use --help for help");
93 }
94
95 for (command = all_commands; ; command++) {
96 if (!command->name) {
97 VLOG_FATAL("unknown command '%s'; use --help for help",
98 argv[optind]);
99 } else if (!strcmp(command->name, argv[optind])) {
100 break;
101 }
102 }
103 optind++;
104
105 if (command->need != NEED_NONE) {
106 if (argc - optind > command->min_args
107 && (isalpha((unsigned char) argv[optind][0])
108 && strchr(argv[optind], ':'))) {
109 rpc = open_jsonrpc(argv[optind++]);
110 } else {
111 char *sock = xasprintf("unix:%s/db.sock", ovs_rundir());
112 rpc = open_jsonrpc(sock);
113 free(sock);
114 }
115 } else {
116 rpc = NULL;
117 }
118
119 if (command->need == NEED_DATABASE) {
120 struct svec dbs;
121
122 svec_init(&dbs);
123 fetch_dbs(rpc, &dbs);
124 if (argc - optind > command->min_args
125 && svec_contains(&dbs, argv[optind])) {
126 database = argv[optind++];
127 } else if (dbs.n == 1) {
128 database = xstrdup(dbs.names[0]);
129 } else if (svec_contains(&dbs, "Open_vSwitch")) {
130 database = "Open_vSwitch";
131 } else {
132 ovs_fatal(0, "no default database for `%s' command, please "
133 "specify a database name", command->name);
134 }
135 svec_destroy(&dbs);
136 } else {
137 database = NULL;
138 }
139
140 if (argc - optind < command->min_args ||
141 argc - optind > command->max_args) {
142 VLOG_FATAL("invalid syntax for '%s' (use --help for help)",
143 command->name);
144 }
145
146 command->handler(rpc, database, argc - optind, argv + optind);
147
148 jsonrpc_close(rpc);
149
150 if (ferror(stdout)) {
151 VLOG_FATAL("write to stdout failed");
152 }
153 if (ferror(stderr)) {
154 VLOG_FATAL("write to stderr failed");
155 }
156
157 return 0;
158 }
159
160 static void
161 parse_options(int argc, char *argv[])
162 {
163 enum {
164 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
165 OPT_TIMESTAMP,
166 DAEMON_OPTION_ENUMS,
167 TABLE_OPTION_ENUMS
168 };
169 static struct option long_options[] = {
170 {"verbose", optional_argument, NULL, 'v'},
171 {"help", no_argument, NULL, 'h'},
172 {"version", no_argument, NULL, 'V'},
173 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
174 DAEMON_LONG_OPTIONS,
175 #ifdef HAVE_OPENSSL
176 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
177 STREAM_SSL_LONG_OPTIONS,
178 #endif
179 TABLE_LONG_OPTIONS,
180 {NULL, 0, NULL, 0},
181 };
182 char *short_options = long_options_to_short_options(long_options);
183
184 for (;;) {
185 int c;
186
187 c = getopt_long(argc, argv, short_options, long_options, NULL);
188 if (c == -1) {
189 break;
190 }
191
192 switch (c) {
193 case 'h':
194 usage();
195
196 case 'V':
197 ovs_print_version(0, 0);
198 exit(EXIT_SUCCESS);
199
200 case 'v':
201 vlog_set_verbosity(optarg);
202 break;
203
204 DAEMON_OPTION_HANDLERS
205
206 TABLE_OPTION_HANDLERS(&table_style)
207
208 STREAM_SSL_OPTION_HANDLERS
209
210 case OPT_BOOTSTRAP_CA_CERT:
211 stream_ssl_set_ca_cert_file(optarg, true);
212 break;
213
214 case OPT_TIMESTAMP:
215 timestamp = true;
216 break;
217
218 case '?':
219 exit(EXIT_FAILURE);
220
221 case 0:
222 /* getopt_long() already set the value for us. */
223 break;
224
225 default:
226 abort();
227 }
228 }
229 free(short_options);
230 }
231
232 static void
233 usage(void)
234 {
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 dump [SERVER] [DATABASE]\n"
257 " dump contents of DATABASE on SERVER to stdout\n"
258 "\nThe default SERVER is unix:%s/db.sock.\n"
259 "The default DATABASE is Open_vSwitch.\n",
260 program_name, program_name, ovs_rundir());
261 stream_usage("SERVER", true, true, true);
262 printf("\nOutput formatting options:\n"
263 " -f, --format=FORMAT set output formatting to FORMAT\n"
264 " (\"table\", \"html\", \"csv\", "
265 "or \"json\")\n"
266 " --no-headings omit table heading row\n"
267 " --pretty pretty-print JSON in output\n"
268 " --timestamp timestamp \"monitor\" output");
269 daemon_usage();
270 vlog_usage();
271 printf("\nOther options:\n"
272 " -h, --help display this help message\n"
273 " -V, --version display version information\n");
274 exit(EXIT_SUCCESS);
275 }
276 \f
277 static void
278 check_txn(int error, struct jsonrpc_msg **reply_)
279 {
280 struct jsonrpc_msg *reply = *reply_;
281
282 if (error) {
283 ovs_fatal(error, "transaction failed");
284 }
285
286 if (reply->error) {
287 ovs_fatal(error, "transaction returned error: %s",
288 json_to_string(reply->error, table_style.json_flags));
289 }
290 }
291
292 static struct json *
293 parse_json(const char *s)
294 {
295 struct json *json = json_from_string(s);
296 if (json->type == JSON_STRING) {
297 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
298 }
299 return json;
300 }
301
302 static struct jsonrpc *
303 open_jsonrpc(const char *server)
304 {
305 struct stream *stream;
306 int error;
307
308 error = stream_open_block(jsonrpc_stream_open(server, &stream,
309 DSCP_DEFAULT), &stream);
310 if (error == EAFNOSUPPORT) {
311 struct pstream *pstream;
312
313 error = jsonrpc_pstream_open(server, &pstream, DSCP_DEFAULT);
314 if (error) {
315 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
316 }
317
318 VLOG_INFO("%s: waiting for connection...", server);
319 error = pstream_accept_block(pstream, &stream);
320 if (error) {
321 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
322 }
323
324 pstream_close(pstream);
325 } else if (error) {
326 ovs_fatal(error, "failed to connect to \"%s\"", server);
327 }
328
329 return jsonrpc_open(stream);
330 }
331
332 static void
333 print_json(struct json *json)
334 {
335 char *string = json_to_string(json, table_style.json_flags);
336 fputs(string, stdout);
337 free(string);
338 }
339
340 static void
341 print_and_free_json(struct json *json)
342 {
343 print_json(json);
344 json_destroy(json);
345 }
346
347 static void
348 check_ovsdb_error(struct ovsdb_error *error)
349 {
350 if (error) {
351 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
352 }
353 }
354
355 static struct ovsdb_schema *
356 fetch_schema(struct jsonrpc *rpc, const char *database)
357 {
358 struct jsonrpc_msg *request, *reply;
359 struct ovsdb_schema *schema;
360
361 request = jsonrpc_create_request("get_schema",
362 json_array_create_1(
363 json_string_create(database)),
364 NULL);
365 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
366 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
367 jsonrpc_msg_destroy(reply);
368
369 return schema;
370 }
371
372 static void
373 fetch_dbs(struct jsonrpc *rpc, struct svec *dbs)
374 {
375 struct jsonrpc_msg *request, *reply;
376 size_t i;
377
378 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
379 NULL);
380
381 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
382 if (reply->result->type != JSON_ARRAY) {
383 ovs_fatal(0, "list_dbs response is not array");
384 }
385
386 for (i = 0; i < reply->result->u.array.n; i++) {
387 const struct json *name = reply->result->u.array.elems[i];
388
389 if (name->type != JSON_STRING) {
390 ovs_fatal(0, "list_dbs response %zu is not string", i);
391 }
392 svec_add(dbs, name->u.string);
393 }
394 jsonrpc_msg_destroy(reply);
395 }
396 \f
397 static void
398 do_list_dbs(struct jsonrpc *rpc, const char *database OVS_UNUSED,
399 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
400 {
401 const char *db_name;
402 struct svec dbs;
403 size_t i;
404
405 svec_init(&dbs);
406 fetch_dbs(rpc, &dbs);
407 svec_sort(&dbs);
408 SVEC_FOR_EACH (i, db_name, &dbs) {
409 puts(db_name);
410 }
411 svec_destroy(&dbs);
412 }
413
414 static void
415 do_get_schema(struct jsonrpc *rpc, const char *database,
416 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
417 {
418 struct ovsdb_schema *schema = fetch_schema(rpc, database);
419 print_and_free_json(ovsdb_schema_to_json(schema));
420 ovsdb_schema_destroy(schema);
421 }
422
423 static void
424 do_get_schema_version(struct jsonrpc *rpc, const char *database,
425 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
426 {
427 struct ovsdb_schema *schema = fetch_schema(rpc, database);
428 puts(schema->version);
429 ovsdb_schema_destroy(schema);
430 }
431
432 static void
433 do_list_tables(struct jsonrpc *rpc, const char *database,
434 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
435 {
436 struct ovsdb_schema *schema;
437 struct shash_node *node;
438 struct table t;
439
440 schema = fetch_schema(rpc, database);
441 table_init(&t);
442 table_add_column(&t, "Table");
443 SHASH_FOR_EACH (node, &schema->tables) {
444 struct ovsdb_table_schema *ts = node->data;
445
446 table_add_row(&t);
447 table_add_cell(&t)->text = xstrdup(ts->name);
448 }
449 ovsdb_schema_destroy(schema);
450 table_print(&t, &table_style);
451 }
452
453 static void
454 do_list_columns(struct jsonrpc *rpc, const char *database,
455 int argc OVS_UNUSED, char *argv[])
456 {
457 const char *table_name = argv[0];
458 struct ovsdb_schema *schema;
459 struct shash_node *table_node;
460 struct table t;
461
462 schema = fetch_schema(rpc, database);
463 table_init(&t);
464 if (!table_name) {
465 table_add_column(&t, "Table");
466 }
467 table_add_column(&t, "Column");
468 table_add_column(&t, "Type");
469 SHASH_FOR_EACH (table_node, &schema->tables) {
470 struct ovsdb_table_schema *ts = table_node->data;
471
472 if (!table_name || !strcmp(table_name, ts->name)) {
473 struct shash_node *column_node;
474
475 SHASH_FOR_EACH (column_node, &ts->columns) {
476 const struct ovsdb_column *column = column_node->data;
477
478 table_add_row(&t);
479 if (!table_name) {
480 table_add_cell(&t)->text = xstrdup(ts->name);
481 }
482 table_add_cell(&t)->text = xstrdup(column->name);
483 table_add_cell(&t)->json = ovsdb_type_to_json(&column->type);
484 }
485 }
486 }
487 ovsdb_schema_destroy(schema);
488 table_print(&t, &table_style);
489 }
490
491 static void
492 do_transact(struct jsonrpc *rpc, const char *database OVS_UNUSED,
493 int argc OVS_UNUSED, char *argv[])
494 {
495 struct jsonrpc_msg *request, *reply;
496 struct json *transaction;
497
498 transaction = parse_json(argv[0]);
499
500 request = jsonrpc_create_request("transact", transaction, NULL);
501 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
502 print_json(reply->result);
503 putchar('\n');
504 jsonrpc_msg_destroy(reply);
505 }
506
507 static void
508 monitor_print_row(struct json *row, const char *type, const char *uuid,
509 const struct ovsdb_column_set *columns, struct table *t)
510 {
511 size_t i;
512
513 if (!row) {
514 ovs_error(0, "missing %s row", type);
515 return;
516 } else if (row->type != JSON_OBJECT) {
517 ovs_error(0, "<row> is not object");
518 return;
519 }
520
521 table_add_row(t);
522 table_add_cell(t)->text = xstrdup(uuid);
523 table_add_cell(t)->text = xstrdup(type);
524 for (i = 0; i < columns->n_columns; i++) {
525 const struct ovsdb_column *column = columns->columns[i];
526 struct json *value = shash_find_data(json_object(row), column->name);
527 struct cell *cell = table_add_cell(t);
528 if (value) {
529 cell->json = json_clone(value);
530 cell->type = &column->type;
531 }
532 }
533 }
534
535 static void
536 monitor_print(struct json *table_updates,
537 const struct ovsdb_table_schema *table,
538 const struct ovsdb_column_set *columns, bool initial)
539 {
540 struct json *table_update;
541 struct shash_node *node;
542 struct table t;
543 size_t i;
544
545 table_init(&t);
546 table_set_timestamp(&t, timestamp);
547
548 if (table_updates->type != JSON_OBJECT) {
549 ovs_error(0, "<table-updates> is not object");
550 return;
551 }
552 table_update = shash_find_data(json_object(table_updates), table->name);
553 if (!table_update) {
554 return;
555 }
556 if (table_update->type != JSON_OBJECT) {
557 ovs_error(0, "<table-update> is not object");
558 return;
559 }
560
561 table_add_column(&t, "row");
562 table_add_column(&t, "action");
563 for (i = 0; i < columns->n_columns; i++) {
564 table_add_column(&t, "%s", columns->columns[i]->name);
565 }
566 SHASH_FOR_EACH (node, json_object(table_update)) {
567 struct json *row_update = node->data;
568 struct json *old, *new;
569
570 if (row_update->type != JSON_OBJECT) {
571 ovs_error(0, "<row-update> is not object");
572 continue;
573 }
574 old = shash_find_data(json_object(row_update), "old");
575 new = shash_find_data(json_object(row_update), "new");
576 if (initial) {
577 monitor_print_row(new, "initial", node->name, columns, &t);
578 } else if (!old) {
579 monitor_print_row(new, "insert", node->name, columns, &t);
580 } else if (!new) {
581 monitor_print_row(old, "delete", node->name, columns, &t);
582 } else {
583 monitor_print_row(old, "old", node->name, columns, &t);
584 monitor_print_row(new, "new", "", columns, &t);
585 }
586 }
587 table_print(&t, &table_style);
588 table_destroy(&t);
589 }
590
591 static void
592 add_column(const char *server, const struct ovsdb_column *column,
593 struct ovsdb_column_set *columns, struct json *columns_json)
594 {
595 if (ovsdb_column_set_contains(columns, column->index)) {
596 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
597 server, column->name);
598 }
599 ovsdb_column_set_add(columns, column);
600 json_array_add(columns_json, json_string_create(column->name));
601 }
602
603 static struct json *
604 parse_monitor_columns(char *arg, const char *server, const char *database,
605 const struct ovsdb_table_schema *table,
606 struct ovsdb_column_set *columns)
607 {
608 bool initial, insert, delete, modify;
609 struct json *mr, *columns_json;
610 char *save_ptr = NULL;
611 char *token;
612
613 mr = json_object_create();
614 columns_json = json_array_create_empty();
615 json_object_put(mr, "columns", columns_json);
616
617 initial = insert = delete = modify = true;
618 for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
619 token = strtok_r(NULL, ",", &save_ptr)) {
620 if (!strcmp(token, "!initial")) {
621 initial = false;
622 } else if (!strcmp(token, "!insert")) {
623 insert = false;
624 } else if (!strcmp(token, "!delete")) {
625 delete = false;
626 } else if (!strcmp(token, "!modify")) {
627 modify = false;
628 } else {
629 const struct ovsdb_column *column;
630
631 column = ovsdb_table_schema_get_column(table, token);
632 if (!column) {
633 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
634 "column named \"%s\"",
635 server, table->name, database, token);
636 }
637 add_column(server, column, columns, columns_json);
638 }
639 }
640
641 if (columns_json->u.array.n == 0) {
642 const struct shash_node **nodes;
643 size_t i, n;
644
645 n = shash_count(&table->columns);
646 nodes = shash_sort(&table->columns);
647 for (i = 0; i < n; i++) {
648 const struct ovsdb_column *column = nodes[i]->data;
649 if (column->index != OVSDB_COL_UUID
650 && column->index != OVSDB_COL_VERSION) {
651 add_column(server, column, columns, columns_json);
652 }
653 }
654 free(nodes);
655
656 add_column(server, ovsdb_table_schema_get_column(table,"_version"),
657 columns, columns_json);
658 }
659
660 if (!initial || !insert || !delete || !modify) {
661 struct json *select = json_object_create();
662 json_object_put(select, "initial", json_boolean_create(initial));
663 json_object_put(select, "insert", json_boolean_create(insert));
664 json_object_put(select, "delete", json_boolean_create(delete));
665 json_object_put(select, "modify", json_boolean_create(modify));
666 json_object_put(mr, "select", select);
667 }
668
669 return mr;
670 }
671
672 static void
673 do_monitor(struct jsonrpc *rpc, const char *database,
674 int argc, char *argv[])
675 {
676 const char *server = jsonrpc_get_name(rpc);
677 const char *table_name = argv[0];
678 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
679 struct ovsdb_table_schema *table;
680 struct ovsdb_schema *schema;
681 struct jsonrpc_msg *request;
682 struct json *monitor, *monitor_request_array,
683 *monitor_requests, *request_id;
684
685 schema = fetch_schema(rpc, database);
686 table = shash_find_data(&schema->tables, table_name);
687 if (!table) {
688 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
689 server, database, table_name);
690 }
691
692 monitor_request_array = json_array_create_empty();
693 if (argc > 1) {
694 int i;
695
696 for (i = 1; i < argc; i++) {
697 json_array_add(
698 monitor_request_array,
699 parse_monitor_columns(argv[i], server, database, table,
700 &columns));
701 }
702 } else {
703 /* Allocate a writable empty string since parse_monitor_columns() is
704 * going to strtok() it and that's risky with literal "". */
705 char empty[] = "";
706 json_array_add(
707 monitor_request_array,
708 parse_monitor_columns(empty, server, database, table, &columns));
709 }
710
711 monitor_requests = json_object_create();
712 json_object_put(monitor_requests, table_name, monitor_request_array);
713
714 monitor = json_array_create_3(json_string_create(database),
715 json_null_create(), monitor_requests);
716 request = jsonrpc_create_request("monitor", monitor, NULL);
717 request_id = json_clone(request->id);
718 jsonrpc_send(rpc, request);
719 for (;;) {
720 struct jsonrpc_msg *msg;
721 int error;
722
723 error = jsonrpc_recv_block(rpc, &msg);
724 if (error) {
725 ovsdb_schema_destroy(schema);
726 ovs_fatal(error, "%s: receive failed", server);
727 }
728
729 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
730 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
731 msg->id));
732 } else if (msg->type == JSONRPC_REPLY
733 && json_equal(msg->id, request_id)) {
734 monitor_print(msg->result, table, &columns, true);
735 fflush(stdout);
736 if (get_detach()) {
737 daemon_save_fd(STDOUT_FILENO);
738 daemonize();
739 }
740 } else if (msg->type == JSONRPC_NOTIFY
741 && !strcmp(msg->method, "update")) {
742 struct json *params = msg->params;
743 if (params->type == JSON_ARRAY
744 && params->u.array.n == 2
745 && params->u.array.elems[0]->type == JSON_NULL) {
746 monitor_print(params->u.array.elems[1],
747 table, &columns, false);
748 fflush(stdout);
749 }
750 }
751 jsonrpc_msg_destroy(msg);
752 }
753 }
754
755 struct dump_table_aux {
756 struct ovsdb_datum **data;
757 const struct ovsdb_column **columns;
758 size_t n_columns;
759 };
760
761 static int
762 compare_data(size_t a_y, size_t b_y, size_t x,
763 const struct dump_table_aux *aux)
764 {
765 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
766 &aux->data[b_y][x],
767 &aux->columns[x]->type);
768 }
769
770 static int
771 compare_rows(size_t a_y, size_t b_y, void *aux_)
772 {
773 struct dump_table_aux *aux = aux_;
774 size_t x;
775
776 /* Skip UUID columns on the first pass, since their values tend to be
777 * random and make our results less reproducible. */
778 for (x = 0; x < aux->n_columns; x++) {
779 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
780 int cmp = compare_data(a_y, b_y, x, aux);
781 if (cmp) {
782 return cmp;
783 }
784 }
785 }
786
787 /* Use UUID columns as tie-breakers. */
788 for (x = 0; x < aux->n_columns; x++) {
789 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
790 int cmp = compare_data(a_y, b_y, x, aux);
791 if (cmp) {
792 return cmp;
793 }
794 }
795 }
796
797 return 0;
798 }
799
800 static void
801 swap_rows(size_t a_y, size_t b_y, void *aux_)
802 {
803 struct dump_table_aux *aux = aux_;
804 struct ovsdb_datum *tmp = aux->data[a_y];
805 aux->data[a_y] = aux->data[b_y];
806 aux->data[b_y] = tmp;
807 }
808
809 static int
810 compare_columns(const void *a_, const void *b_)
811 {
812 const struct ovsdb_column *const *ap = a_;
813 const struct ovsdb_column *const *bp = b_;
814 const struct ovsdb_column *a = *ap;
815 const struct ovsdb_column *b = *bp;
816
817 return strcmp(a->name, b->name);
818 }
819
820 static void
821 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
822 {
823 const struct ovsdb_column **columns;
824 size_t n_columns;
825
826 struct ovsdb_datum **data;
827
828 struct dump_table_aux aux;
829 struct shash_node *node;
830 struct table t;
831 size_t x, y;
832
833 /* Sort columns by name, for reproducibility. */
834 columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
835 n_columns = 0;
836 SHASH_FOR_EACH (node, &ts->columns) {
837 struct ovsdb_column *column = node->data;
838 if (strcmp(column->name, "_version")) {
839 columns[n_columns++] = column;
840 }
841 }
842 qsort(columns, n_columns, sizeof *columns, compare_columns);
843
844 /* Extract data from table. */
845 data = xmalloc(rows->n * sizeof *data);
846 for (y = 0; y < rows->n; y++) {
847 struct shash *row;
848
849 if (rows->elems[y]->type != JSON_OBJECT) {
850 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
851 "%s", y, ts->name, json_to_string(rows->elems[y], 0));
852 }
853 row = json_object(rows->elems[y]);
854
855 data[y] = xmalloc(n_columns * sizeof **data);
856 for (x = 0; x < n_columns; x++) {
857 const struct json *json = shash_find_data(row, columns[x]->name);
858 if (!json) {
859 ovs_fatal(0, "row %zu in table %s response lacks %s column",
860 y, ts->name, columns[x]->name);
861 }
862
863 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
864 &columns[x]->type,
865 json, NULL));
866 }
867 }
868
869 /* Sort rows by column values, for reproducibility. */
870 aux.data = data;
871 aux.columns = columns;
872 aux.n_columns = n_columns;
873 sort(rows->n, compare_rows, swap_rows, &aux);
874
875 /* Add column headings. */
876 table_init(&t);
877 table_set_caption(&t, xasprintf("%s table", ts->name));
878 for (x = 0; x < n_columns; x++) {
879 table_add_column(&t, "%s", columns[x]->name);
880 }
881
882 /* Print rows. */
883 for (y = 0; y < rows->n; y++) {
884 table_add_row(&t);
885 for (x = 0; x < n_columns; x++) {
886 struct cell *cell = table_add_cell(&t);
887 cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
888 cell->type = &columns[x]->type;
889 ovsdb_datum_destroy(&data[y][x], &columns[x]->type);
890 }
891 free(data[y]);
892 }
893 table_print(&t, &table_style);
894 table_destroy(&t);
895
896 free(data);
897 free(columns);
898 }
899
900 static void
901 do_dump(struct jsonrpc *rpc, const char *database,
902 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
903 {
904 struct jsonrpc_msg *request, *reply;
905 struct ovsdb_schema *schema;
906 struct json *transaction;
907
908 const struct shash_node **tables;
909 size_t n_tables;
910
911 size_t i;
912
913 schema = fetch_schema(rpc, database);
914 tables = shash_sort(&schema->tables);
915 n_tables = shash_count(&schema->tables);
916
917 /* Construct transaction to retrieve entire database. */
918 transaction = json_array_create_1(json_string_create(database));
919 for (i = 0; i < n_tables; i++) {
920 const struct ovsdb_table_schema *ts = tables[i]->data;
921 struct json *op, *columns;
922 struct shash_node *node;
923
924 columns = json_array_create_empty();
925 SHASH_FOR_EACH (node, &ts->columns) {
926 const struct ovsdb_column *column = node->data;
927
928 if (strcmp(column->name, "_version")) {
929 json_array_add(columns, json_string_create(column->name));
930 }
931 }
932
933 op = json_object_create();
934 json_object_put_string(op, "op", "select");
935 json_object_put_string(op, "table", tables[i]->name);
936 json_object_put(op, "where", json_array_create_empty());
937 json_object_put(op, "columns", columns);
938 json_array_add(transaction, op);
939 }
940
941 /* Send request, get reply. */
942 request = jsonrpc_create_request("transact", transaction, NULL);
943 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
944
945 /* Print database contents. */
946 if (reply->result->type != JSON_ARRAY
947 || reply->result->u.array.n != n_tables) {
948 ovs_fatal(0, "reply is not array of %zu elements: %s",
949 n_tables, json_to_string(reply->result, 0));
950 }
951 for (i = 0; i < n_tables; i++) {
952 const struct ovsdb_table_schema *ts = tables[i]->data;
953 const struct json *op_result = reply->result->u.array.elems[i];
954 struct json *rows;
955
956 if (op_result->type != JSON_OBJECT
957 || !(rows = shash_find_data(json_object(op_result), "rows"))
958 || rows->type != JSON_ARRAY) {
959 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
960 "member array: %s",
961 ts->name, json_to_string(op_result, 0));
962 }
963
964 dump_table(ts, &rows->u.array);
965 }
966
967 jsonrpc_msg_destroy(reply);
968 free(tables);
969 ovsdb_schema_destroy(schema);
970 }
971
972 static void
973 do_help(struct jsonrpc *rpc OVS_UNUSED, const char *database OVS_UNUSED,
974 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
975 {
976 usage();
977 }
978
979 /* All command handlers (except for "help") are expected to take an optional
980 * server socket name (e.g. "unix:...") as their first argument. The socket
981 * name argument must be included in max_args (but left out of min_args). The
982 * command name and socket name are not included in the arguments passed to the
983 * handler: the argv[0] passed to the handler is the first argument after the
984 * optional server socket name. The connection to the server is available as
985 * global variable 'rpc'. */
986 static const struct ovsdb_client_command all_commands[] = {
987 { "list-dbs", NEED_RPC, 0, 0, do_list_dbs },
988 { "get-schema", NEED_DATABASE, 0, 0, do_get_schema },
989 { "get-schema-version", NEED_DATABASE, 0, 0, do_get_schema_version },
990 { "list-tables", NEED_DATABASE, 0, 0, do_list_tables },
991 { "list-columns", NEED_DATABASE, 0, 1, do_list_columns },
992 { "transact", NEED_RPC, 1, 1, do_transact },
993 { "monitor", NEED_DATABASE, 1, INT_MAX, do_monitor },
994 { "dump", NEED_DATABASE, 0, 0, do_dump },
995
996 { "help", NEED_NONE, 0, INT_MAX, do_help },
997
998 { NULL, 0, 0, 0, NULL },
999 };