2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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.
26 #include "command-line.h"
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-error.h"
38 #include "socket-util.h"
42 #include "openvswitch/vlog.h"
44 /* -m, --more: Verbosity level for "show-log" command output. */
45 static int show_log_verbosity;
47 static const struct ovs_cmdl_command *get_all_commands(void);
49 OVS_NO_RETURN static void usage(void);
50 static void parse_options(int argc, char *argv[]);
52 static const char *default_db(void);
53 static const char *default_schema(void);
56 main(int argc, char *argv[])
58 struct ovs_cmdl_context ctx = { .argc = 0, };
59 set_program_name(argv[0]);
60 parse_options(argc, argv);
61 fatal_ignore_sigpipe();
62 ctx.argc = argc - optind;
63 ctx.argv = argv + optind;
64 ovs_cmdl_run_command(&ctx, get_all_commands());
69 parse_options(int argc, char *argv[])
71 static const struct option long_options[] = {
72 {"more", no_argument, NULL, 'm'},
73 {"verbose", optional_argument, NULL, 'v'},
74 {"help", no_argument, NULL, 'h'},
75 {"option", no_argument, NULL, 'o'},
76 {"version", no_argument, NULL, 'V'},
79 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
84 c = getopt_long(argc, argv, short_options, long_options, NULL);
98 ovs_cmdl_print_options(long_options);
102 ovs_print_version(0, 0);
106 vlog_set_verbosity(optarg);
122 printf("%s: Open vSwitch database management utility\n"
123 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
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());
138 printf("\nOther options:\n"
139 " -m, --more increase show-log verbosity\n"
140 " -h, --help display this help message\n"
141 " -V, --version display version information\n");
150 db = xasprintf("%s/conf.db", ovs_dbdir());
160 schema = xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
166 parse_json(const char *s)
168 struct json *json = json_from_string(s);
169 if (json->type == JSON_STRING) {
170 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
176 print_and_free_json(struct json *json)
178 char *string = json_to_string(json, JSSF_SORT);
185 check_ovsdb_error(struct ovsdb_error *error)
188 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
193 do_create(struct ovs_cmdl_context *ctx)
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();
197 struct ovsdb_schema *schema;
198 struct ovsdb_log *log;
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);
204 ovsdb_schema_destroy(schema);
206 /* Create database file. */
207 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
209 check_ovsdb_error(ovsdb_log_write(log, json));
210 check_ovsdb_error(ovsdb_log_commit(log));
211 ovsdb_log_close(log);
217 compact_or_convert(const char *src_name_, const char *dst_name_,
218 const struct ovsdb_schema *new_schema,
221 char *src_name, *dst_name;
222 struct lockfile *src_lock;
223 struct lockfile *dst_lock;
224 bool in_place = dst_name_ == NULL;
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_);
234 ? xasprintf("%s.tmp", src_name)
235 : follow_symlinks(dst_name_));
237 /* Lock the source, if we will be replacing it. */
239 retval = lockfile_lock(src_name, &src_lock);
241 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
245 /* Get (temporary) destination and lock it. */
246 retval = lockfile_lock(dst_name, &dst_lock);
248 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
252 check_ovsdb_error(new_schema
253 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
254 : ovsdb_file_open(src_name, true, &db, NULL));
255 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
258 /* Replace source. */
263 if (rename(dst_name, src_name)) {
264 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
267 fsync_parent_dir(dst_name);
268 lockfile_unlock(src_lock);
271 lockfile_unlock(dst_lock);
278 do_compact(struct ovs_cmdl_context *ctx)
280 const char *db = ctx->argc >= 2 ? ctx->argv[1] : default_db();
281 const char *target = ctx->argc >= 3 ? ctx->argv[2] : NULL;
283 compact_or_convert(db, target, NULL, "compacted by ovsdb-tool "VERSION);
287 do_convert(struct ovs_cmdl_context *ctx)
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;
292 struct ovsdb_schema *new_schema;
294 check_ovsdb_error(ovsdb_schema_from_file(schema, &new_schema));
295 compact_or_convert(db, target, new_schema,
296 "converted by ovsdb-tool "VERSION);
297 ovsdb_schema_destroy(new_schema);
301 do_needs_conversion(struct ovs_cmdl_context *ctx)
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();
305 struct ovsdb_schema *schema1, *schema2;
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);
315 do_db_version(struct ovs_cmdl_context *ctx)
317 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
318 struct ovsdb_schema *schema;
320 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
321 puts(schema->version);
322 ovsdb_schema_destroy(schema);
326 do_db_cksum(struct ovs_cmdl_context *ctx)
328 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
329 struct ovsdb_schema *schema;
331 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
333 ovsdb_schema_destroy(schema);
337 do_schema_version(struct ovs_cmdl_context *ctx)
339 const char *schema_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_schema();
340 struct ovsdb_schema *schema;
342 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
343 puts(schema->version);
344 ovsdb_schema_destroy(schema);
348 do_schema_cksum(struct ovs_cmdl_context *ctx)
350 const char *schema_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_schema();
351 struct ovsdb_schema *schema;
353 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
355 ovsdb_schema_destroy(schema);
359 transact(bool read_only, int argc, char *argv[])
361 const char *db_file_name = argc >= 3 ? argv[1] : default_db();
362 const char *transaction = argv[argc - 1];
363 struct json *request, *result;
366 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
368 request = parse_json(transaction);
369 result = ovsdb_execute(db, NULL, request, 0, NULL);
370 json_destroy(request);
372 print_and_free_json(result);
377 do_query(struct ovs_cmdl_context *ctx)
379 transact(true, ctx->argc, ctx->argv);
383 do_transact(struct ovs_cmdl_context *ctx)
385 transact(false, ctx->argc, ctx->argv);
389 print_db_changes(struct shash *tables, struct shash *names,
390 const struct ovsdb_schema *schema)
392 struct shash_node *n1;
394 SHASH_FOR_EACH (n1, tables) {
395 const char *table = n1->name;
396 struct ovsdb_table_schema *table_schema;
397 struct json *rows = n1->data;
398 struct shash_node *n2;
400 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
404 table_schema = shash_find_data(&schema->tables, table);
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;
412 old_name = new_name = shash_find_data(names, row_uuid);
413 if (columns->type == JSON_OBJECT) {
414 struct json *new_name_json;
416 new_name_json = shash_find_data(json_object(columns), "name");
418 new_name = json_to_string(new_name_json, JSSF_SORT);
419 free_new_name = true;
423 printf("\ttable %s", table);
427 printf(" insert row %s (%.8s):\n", new_name, row_uuid);
429 printf(" insert row %.8s:\n", row_uuid);
432 printf(" row %s (%.8s):\n", old_name, row_uuid);
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;
439 const struct ovsdb_column *column_schema;
440 struct json *value = n3->data;
441 char *value_string = NULL;
445 ? shash_find_data(&table_schema->columns, column)
448 const struct ovsdb_type *type;
449 struct ovsdb_error *error;
450 struct ovsdb_datum datum;
452 type = &column_schema->type;
453 error = ovsdb_datum_from_json(&datum, type,
459 ovsdb_datum_to_string(&datum, type, &s);
460 value_string = ds_steal_cstr(&s);
462 ovsdb_error_destroy(error);
466 value_string = json_to_string(value, JSSF_SORT);
468 printf("\t\t%s=%s\n", column, value_string);
473 || (new_name != old_name && strcmp(old_name, new_name))) {
475 shash_delete(names, shash_find(names, row_uuid));
478 shash_add(names, row_uuid, (new_name
480 : xmemdup0(row_uuid, 8)));
482 } else if (columns->type == JSON_NULL) {
483 struct shash_node *node;
485 printf("\t\tdelete row\n");
486 node = shash_find(names, row_uuid);
488 shash_delete(names, node);
501 do_show_log(struct ovs_cmdl_context *ctx)
503 const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db();
505 struct ovsdb_log *log;
506 struct ovsdb_schema *schema;
509 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
516 check_ovsdb_error(ovsdb_log_read(log, &json));
521 printf("record %u:", i);
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) {
527 struct json *date, *comment;
529 date = shash_find_data(json_object(json), "_date");
530 if (date && date->type == JSON_INTEGER) {
531 long long int t = json_integer(date);
535 /* Older versions of ovsdb wrote timestamps in seconds. */
539 s = xastrftime_msec(" %Y-%m-%d %H:%M:%S.###", t, true);
544 comment = shash_find_data(json_object(json), "_comment");
545 if (comment && comment->type == JSON_STRING) {
546 printf(" \"%s\"", json_string(comment));
549 if (i > 0 && show_log_verbosity > 0) {
551 print_db_changes(json_object(json), &names, schema);
558 ovsdb_log_close(log);
559 ovsdb_schema_destroy(schema);
560 /* XXX free 'names'. */
564 do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
570 do_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED)
572 ovs_cmdl_print_commands(get_all_commands());
575 static const struct ovs_cmdl_command all_commands[] = {
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 },
592 static const struct ovs_cmdl_command *get_all_commands(void)