]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/ovsdb-tool.c
json: Move from lib to include/openvswitch.
[mirror_ovs.git] / ovsdb / ovsdb-tool.c
CommitLineData
f85f8ebb 1/*
a15fce8e 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
f85f8ebb
BP
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#include <errno.h>
19#include <fcntl.h>
20#include <getopt.h>
21#include <signal.h>
22#include <stdlib.h>
23#include <string.h>
24
4e92542c 25#include "column.h"
f85f8ebb
BP
26#include "command-line.h"
27#include "compiler.h"
e4476f74 28#include "dirs.h"
3e8a2ad1 29#include "openvswitch/dynamic-string.h"
8a777cf6 30#include "fatal-signal.h"
bd06962a 31#include "file.h"
1e19e50e 32#include "lockfile.h"
41709ccc 33#include "log.h"
ee89ea7b 34#include "openvswitch/json.h"
f85f8ebb 35#include "ovsdb.h"
4e92542c 36#include "ovsdb-data.h"
f85f8ebb 37#include "ovsdb-error.h"
1e19e50e 38#include "socket-util.h"
f85f8ebb
BP
39#include "table.h"
40#include "timeval.h"
41#include "util.h"
e6211adc 42#include "openvswitch/vlog.h"
5136ce49 43
c6782bb0
BP
44/* -m, --more: Verbosity level for "show-log" command output. */
45static int show_log_verbosity;
46
5f383751 47static const struct ovs_cmdl_command *get_all_commands(void);
f85f8ebb 48
cab50449 49OVS_NO_RETURN static void usage(void);
f85f8ebb
BP
50static void parse_options(int argc, char *argv[]);
51
e4476f74
BP
52static const char *default_db(void);
53static const char *default_schema(void);
54
f85f8ebb
BP
55int
56main(int argc, char *argv[])
57{
1636c761 58 struct ovs_cmdl_context ctx = { .argc = 0, };
f85f8ebb 59 set_program_name(argv[0]);
f85f8ebb 60 parse_options(argc, argv);
8a777cf6 61 fatal_ignore_sigpipe();
1636c761
RB
62 ctx.argc = argc - optind;
63 ctx.argv = argv + optind;
64 ovs_cmdl_run_command(&ctx, get_all_commands());
f85f8ebb
BP
65 return 0;
66}
67
68static void
69parse_options(int argc, char *argv[])
70{
07fc4ed3 71 static const struct option long_options[] = {
e3c17733
BP
72 {"more", no_argument, NULL, 'm'},
73 {"verbose", optional_argument, NULL, 'v'},
74 {"help", no_argument, NULL, 'h'},
66fa2c88 75 {"option", no_argument, NULL, 'o'},
e3c17733
BP
76 {"version", no_argument, NULL, 'V'},
77 {NULL, 0, NULL, 0},
f85f8ebb 78 };
5f383751 79 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
f85f8ebb
BP
80
81 for (;;) {
82 int c;
83
84 c = getopt_long(argc, argv, short_options, long_options, NULL);
85 if (c == -1) {
86 break;
87 }
88
89 switch (c) {
c6782bb0
BP
90 case 'm':
91 show_log_verbosity++;
92 break;
93
f85f8ebb
BP
94 case 'h':
95 usage();
96
66fa2c88 97 case 'o':
5f383751 98 ovs_cmdl_print_options(long_options);
66fa2c88
AW
99 exit(EXIT_SUCCESS);
100
f85f8ebb 101 case 'V':
55d5bb44 102 ovs_print_version(0, 0);
f85f8ebb
BP
103 exit(EXIT_SUCCESS);
104
105 case 'v':
106 vlog_set_verbosity(optarg);
107 break;
108
109 case '?':
110 exit(EXIT_FAILURE);
111
112 default:
113 abort();
114 }
115 }
116 free(short_options);
117}
118
119static void
120usage(void)
121{
122 printf("%s: Open vSwitch database management utility\n"
123 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
e4476f74
BP
124 " create [DB [SCHEMA]] create DB with the given SCHEMA\n"
125 " compact [DB [DST]] compact DB in-place (or to DST)\n"
126 " convert [DB [SCHEMA [DST]]] convert DB to SCHEMA (to DST)\n"
127 " db-version [DB] report version of schema used by DB\n"
128 " db-cksum [DB] report checksum of schema used by DB\n"
129 " schema-version [SCHEMA] report SCHEMA's schema version\n"
130 " schema-cksum [SCHEMA] report SCHEMA's checksum\n"
131 " query [DB] TRNS execute read-only transaction on DB\n"
132 " transact [DB] TRNS execute read/write transaction on DB\n"
133 " [-m]... show-log [DB] print DB's log entries\n"
134 "The default DB is %s.\n"
135 "The default SCHEMA is %s.\n",
136 program_name, program_name, default_db(), default_schema());
f85f8ebb
BP
137 vlog_usage();
138 printf("\nOther options:\n"
c6782bb0 139 " -m, --more increase show-log verbosity\n"
f85f8ebb
BP
140 " -h, --help display this help message\n"
141 " -V, --version display version information\n");
142 exit(EXIT_SUCCESS);
143}
e4476f74
BP
144
145static const char *
146default_db(void)
147{
148 static char *db;
149 if (!db) {
f973f2af 150 db = xasprintf("%s/conf.db", ovs_dbdir());
e4476f74
BP
151 }
152 return db;
153}
154
155static const char *
156default_schema(void)
157{
158 static char *schema;
159 if (!schema) {
160 schema = xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
161 }
162 return schema;
163}
f85f8ebb
BP
164\f
165static struct json *
166parse_json(const char *s)
167{
168 struct json *json = json_from_string(s);
169 if (json->type == JSON_STRING) {
170 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
171 }
172 return json;
173}
174
175static void
176print_and_free_json(struct json *json)
177{
178 char *string = json_to_string(json, JSSF_SORT);
179 json_destroy(json);
180 puts(string);
181 free(string);
182}
183
184static void
185check_ovsdb_error(struct ovsdb_error *error)
186{
187 if (error) {
188 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
189 }
190}
191\f
192static void
1636c761 193do_create(struct ovs_cmdl_context *ctx)
f85f8ebb 194{
1636c761
RB
195 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
196 const char *schema_file_name = ctx->argc >= 3 ? ctx->argv[2] : default_schema();
f85f8ebb 197 struct ovsdb_schema *schema;
41709ccc 198 struct ovsdb_log *log;
f85f8ebb
BP
199 struct json *json;
200
201 /* Read schema from file and convert to JSON. */
202 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
203 json = ovsdb_schema_to_json(schema);
90cc4071 204 ovsdb_schema_destroy(schema);
f85f8ebb
BP
205
206 /* Create database file. */
7446f148
BP
207 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
208 -1, &log));
41709ccc
BP
209 check_ovsdb_error(ovsdb_log_write(log, json));
210 check_ovsdb_error(ovsdb_log_commit(log));
211 ovsdb_log_close(log);
f85f8ebb
BP
212
213 json_destroy(json);
214}
215
1e19e50e 216static void
a35ae81c 217compact_or_convert(const char *src_name_, const char *dst_name_,
1e19e50e
BP
218 const struct ovsdb_schema *new_schema,
219 const char *comment)
220{
a35ae81c 221 char *src_name, *dst_name;
1e19e50e
BP
222 struct lockfile *src_lock;
223 struct lockfile *dst_lock;
a35ae81c 224 bool in_place = dst_name_ == NULL;
1e19e50e
BP
225 struct ovsdb *db;
226 int retval;
227
a35ae81c
BP
228 /* Dereference symlinks for source and destination names. In the in-place
229 * case this ensures that, if the source name is a symlink, we replace its
230 * target instead of replacing the symlink by a regular file. In the
231 * non-in-place, this has the same effect for the destination name. */
232 src_name = follow_symlinks(src_name_);
233 dst_name = (in_place
234 ? xasprintf("%s.tmp", src_name)
235 : follow_symlinks(dst_name_));
236
aebfc869 237 /* Lock the source, if we will be replacing it. */
1e19e50e 238 if (in_place) {
4770e795 239 retval = lockfile_lock(src_name, &src_lock);
aebfc869
BP
240 if (retval) {
241 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
242 }
1e19e50e
BP
243 }
244
aebfc869 245 /* Get (temporary) destination and lock it. */
4770e795 246 retval = lockfile_lock(dst_name, &dst_lock);
1e19e50e
BP
247 if (retval) {
248 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
249 }
250
251 /* Save a copy. */
252 check_ovsdb_error(new_schema
253 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
ada496b5 254 : ovsdb_file_open(src_name, true, &db, NULL));
1e19e50e
BP
255 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
256 ovsdb_destroy(db);
257
258 /* Replace source. */
259 if (in_place) {
10c119c3
GS
260#ifdef _WIN32
261 unlink(src_name);
262#endif
1e19e50e
BP
263 if (rename(dst_name, src_name)) {
264 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
265 dst_name, src_name);
266 }
267 fsync_parent_dir(dst_name);
1e19e50e
BP
268 lockfile_unlock(src_lock);
269 }
270
271 lockfile_unlock(dst_lock);
e49190c4 272
a35ae81c
BP
273 free(src_name);
274 free(dst_name);
1e19e50e
BP
275}
276
277static void
1636c761 278do_compact(struct ovs_cmdl_context *ctx)
1e19e50e 279{
1636c761
RB
280 const char *db = ctx->argc >= 2 ? ctx->argv[1] : default_db();
281 const char *target = ctx->argc >= 3 ? ctx->argv[2] : NULL;
e4476f74 282
8a07709c 283 compact_or_convert(db, target, NULL, "compacted by ovsdb-tool "VERSION);
1e19e50e
BP
284}
285
286static void
1636c761 287do_convert(struct ovs_cmdl_context *ctx)
1e19e50e 288{
1636c761
RB
289 const char *db = ctx->argc >= 2 ? ctx->argv[1] : default_db();
290 const char *schema = ctx->argc >= 3 ? ctx->argv[2] : default_schema();
291 const char *target = ctx->argc >= 4 ? ctx->argv[3] : NULL;
1e19e50e
BP
292 struct ovsdb_schema *new_schema;
293
e4476f74
BP
294 check_ovsdb_error(ovsdb_schema_from_file(schema, &new_schema));
295 compact_or_convert(db, target, new_schema,
8a07709c 296 "converted by ovsdb-tool "VERSION);
1e19e50e
BP
297 ovsdb_schema_destroy(new_schema);
298}
299
403e3a25 300static void
1636c761 301do_needs_conversion(struct ovs_cmdl_context *ctx)
403e3a25 302{
1636c761
RB
303 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
304 const char *schema_file_name = ctx->argc >= 3 ? ctx->argv[2] : default_schema();
403e3a25
BP
305 struct ovsdb_schema *schema1, *schema2;
306
307 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1));
308 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2));
309 puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes");
310 ovsdb_schema_destroy(schema1);
311 ovsdb_schema_destroy(schema2);
312}
313
8159b984 314static void
1636c761 315do_db_version(struct ovs_cmdl_context *ctx)
8159b984 316{
1636c761 317 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
e1ebc8ce 318 struct ovsdb_schema *schema;
8159b984 319
e1ebc8ce
BP
320 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
321 puts(schema->version);
322 ovsdb_schema_destroy(schema);
8159b984
BP
323}
324
6aa09313 325static void
1636c761 326do_db_cksum(struct ovs_cmdl_context *ctx)
6aa09313 327{
1636c761 328 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
6aa09313
BP
329 struct ovsdb_schema *schema;
330
331 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
332 puts(schema->cksum);
333 ovsdb_schema_destroy(schema);
334}
335
8159b984 336static void
1636c761 337do_schema_version(struct ovs_cmdl_context *ctx)
8159b984 338{
1636c761 339 const char *schema_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_schema();
8159b984
BP
340 struct ovsdb_schema *schema;
341
342 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
343 puts(schema->version);
344 ovsdb_schema_destroy(schema);
345}
346
6aa09313 347static void
1636c761 348do_schema_cksum(struct ovs_cmdl_context *ctx)
6aa09313 349{
1636c761 350 const char *schema_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_schema();
6aa09313
BP
351 struct ovsdb_schema *schema;
352
353 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
354 puts(schema->cksum);
355 ovsdb_schema_destroy(schema);
356}
357
f85f8ebb 358static void
e4476f74 359transact(bool read_only, int argc, char *argv[])
f85f8ebb 360{
e4476f74
BP
361 const char *db_file_name = argc >= 3 ? argv[1] : default_db();
362 const char *transaction = argv[argc - 1];
f85f8ebb
BP
363 struct json *request, *result;
364 struct ovsdb *db;
365
ada496b5 366 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
f85f8ebb
BP
367
368 request = parse_json(transaction);
da897f41 369 result = ovsdb_execute(db, NULL, request, 0, NULL);
f85f8ebb
BP
370 json_destroy(request);
371
372 print_and_free_json(result);
373 ovsdb_destroy(db);
374}
375
376static void
1636c761 377do_query(struct ovs_cmdl_context *ctx)
f85f8ebb 378{
1636c761 379 transact(true, ctx->argc, ctx->argv);
f85f8ebb
BP
380}
381
382static void
1636c761 383do_transact(struct ovs_cmdl_context *ctx)
f85f8ebb 384{
1636c761 385 transact(false, ctx->argc, ctx->argv);
f85f8ebb
BP
386}
387
c6782bb0 388static void
4e92542c
BP
389print_db_changes(struct shash *tables, struct shash *names,
390 const struct ovsdb_schema *schema)
c6782bb0
BP
391{
392 struct shash_node *n1;
393
394 SHASH_FOR_EACH (n1, tables) {
395 const char *table = n1->name;
4e92542c 396 struct ovsdb_table_schema *table_schema;
c6782bb0
BP
397 struct json *rows = n1->data;
398 struct shash_node *n2;
399
400 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
401 continue;
402 }
403
4e92542c 404 table_schema = shash_find_data(&schema->tables, table);
c6782bb0
BP
405 SHASH_FOR_EACH (n2, json_object(rows)) {
406 const char *row_uuid = n2->name;
407 struct json *columns = n2->data;
408 struct shash_node *n3;
409 char *old_name, *new_name;
410 bool free_new_name = false;
411
412 old_name = new_name = shash_find_data(names, row_uuid);
413 if (columns->type == JSON_OBJECT) {
414 struct json *new_name_json;
415
416 new_name_json = shash_find_data(json_object(columns), "name");
417 if (new_name_json) {
418 new_name = json_to_string(new_name_json, JSSF_SORT);
419 free_new_name = true;
420 }
421 }
422
423 printf("\ttable %s", table);
424
425 if (!old_name) {
426 if (new_name) {
a0a15a00 427 printf(" insert row %s (%.8s):\n", new_name, row_uuid);
c6782bb0
BP
428 } else {
429 printf(" insert row %.8s:\n", row_uuid);
430 }
431 } else {
a0a15a00 432 printf(" row %s (%.8s):\n", old_name, row_uuid);
c6782bb0
BP
433 }
434
435 if (columns->type == JSON_OBJECT) {
436 if (show_log_verbosity > 1) {
437 SHASH_FOR_EACH (n3, json_object(columns)) {
438 const char *column = n3->name;
4e92542c 439 const struct ovsdb_column *column_schema;
c6782bb0 440 struct json *value = n3->data;
4e92542c
BP
441 char *value_string = NULL;
442
443 column_schema =
444 (table_schema
445 ? shash_find_data(&table_schema->columns, column)
446 : NULL);
447 if (column_schema) {
4e92542c 448 const struct ovsdb_type *type;
a15fce8e 449 struct ovsdb_error *error;
4e92542c
BP
450 struct ovsdb_datum datum;
451
452 type = &column_schema->type;
453 error = ovsdb_datum_from_json(&datum, type,
454 value, NULL);
455 if (!error) {
456 struct ds s;
457
458 ds_init(&s);
459 ovsdb_datum_to_string(&datum, type, &s);
460 value_string = ds_steal_cstr(&s);
a15fce8e
BP
461 } else {
462 ovsdb_error_destroy(error);
4e92542c
BP
463 }
464 }
465 if (!value_string) {
466 value_string = json_to_string(value, JSSF_SORT);
467 }
c6782bb0
BP
468 printf("\t\t%s=%s\n", column, value_string);
469 free(value_string);
470 }
471 }
472 if (!old_name
473 || (new_name != old_name && strcmp(old_name, new_name))) {
474 if (old_name) {
475 shash_delete(names, shash_find(names, row_uuid));
476 free(old_name);
477 }
478 shash_add(names, row_uuid, (new_name
479 ? xstrdup(new_name)
480 : xmemdup0(row_uuid, 8)));
481 }
482 } else if (columns->type == JSON_NULL) {
b932d88b
BP
483 struct shash_node *node;
484
c6782bb0 485 printf("\t\tdelete row\n");
b932d88b
BP
486 node = shash_find(names, row_uuid);
487 if (node) {
488 shash_delete(names, node);
489 }
c6782bb0
BP
490 free(old_name);
491 }
492
493 if (free_new_name) {
494 free(new_name);
495 }
496 }
497 }
498}
499
722f6301 500static void
1636c761 501do_show_log(struct ovs_cmdl_context *ctx)
722f6301 502{
1636c761 503 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
c6782bb0 504 struct shash names;
722f6301 505 struct ovsdb_log *log;
4e92542c 506 struct ovsdb_schema *schema;
722f6301
BP
507 unsigned int i;
508
7446f148
BP
509 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
510 -1, &log));
c6782bb0 511 shash_init(&names);
4e92542c 512 schema = NULL;
722f6301
BP
513 for (i = 0; ; i++) {
514 struct json *json;
515
516 check_ovsdb_error(ovsdb_log_read(log, &json));
517 if (!json) {
518 break;
519 }
520
521 printf("record %u:", i);
4e92542c
BP
522 if (i == 0) {
523 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
524 printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
525 schema->name, schema->version, schema->cksum);
526 } else if (json->type == JSON_OBJECT) {
722f6301
BP
527 struct json *date, *comment;
528
529 date = shash_find_data(json_object(json), "_date");
530 if (date && date->type == JSON_INTEGER) {
1ff1065c
PI
531 long long int t = json_integer(date);
532 char *s;
533
534 if (t < INT32_MAX) {
535 /* Older versions of ovsdb wrote timestamps in seconds. */
536 t *= 1000;
537 }
538
57093462 539 s = xastrftime_msec(" %Y-%m-%d %H:%M:%S.###", t, true);
3e78870d
BP
540 fputs(s, stdout);
541 free(s);
722f6301
BP
542 }
543
544 comment = shash_find_data(json_object(json), "_comment");
545 if (comment && comment->type == JSON_STRING) {
546 printf(" \"%s\"", json_string(comment));
547 }
c6782bb0
BP
548
549 if (i > 0 && show_log_verbosity > 0) {
550 putchar('\n');
4e92542c 551 print_db_changes(json_object(json), &names, schema);
c6782bb0 552 }
722f6301
BP
553 }
554 json_destroy(json);
555 putchar('\n');
556 }
c6782bb0 557
400eb935 558 ovsdb_log_close(log);
4e92542c 559 ovsdb_schema_destroy(schema);
c6782bb0 560 /* XXX free 'names'. */
722f6301
BP
561}
562
f85f8ebb 563static void
1636c761 564do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
f85f8ebb
BP
565{
566 usage();
567}
568
451de37e 569static void
1636c761 570do_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED)
451de37e 571{
5f383751 572 ovs_cmdl_print_commands(get_all_commands());
451de37e
AW
573}
574
5f383751 575static const struct ovs_cmdl_command all_commands[] = {
451de37e
AW
576 { "create", "[db [schema]]", 0, 2, do_create },
577 { "compact", "[db [dst]]", 0, 2, do_compact },
578 { "convert", "[db [schema [dst]]]", 0, 3, do_convert },
579 { "needs-conversion", NULL, 0, 2, do_needs_conversion },
580 { "db-version", "[db]", 0, 1, do_db_version },
581 { "db-cksum", "[db]", 0, 1, do_db_cksum },
582 { "schema-version", "[schema]", 0, 1, do_schema_version },
583 { "schema-cksum", "[schema]", 0, 1, do_schema_cksum },
584 { "query", "[db] trns", 1, 2, do_query },
585 { "transact", "[db] trns", 1, 2, do_transact },
586 { "show-log", "[db]", 0, 1, do_show_log },
587 { "help", NULL, 0, INT_MAX, do_help },
588 { "list-commands", NULL, 0, INT_MAX, do_list_commands },
589 { NULL, NULL, 0, 0, NULL },
f85f8ebb 590};
3815d6c2 591
5f383751 592static const struct ovs_cmdl_command *get_all_commands(void)
3815d6c2
LS
593{
594 return all_commands;
595}