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"
44 /* -m, --more: Verbosity level for "show-log" command output. */
45 static int show_log_verbosity;
47 static const struct command *get_all_commands(void);
49 static void usage(void) NO_RETURN;
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 set_program_name(argv[0]);
59 parse_options(argc, argv);
60 fatal_ignore_sigpipe();
61 run_command(argc - optind, argv + optind, get_all_commands());
66 parse_options(int argc, char *argv[])
68 static const struct option long_options[] = {
69 {"more", no_argument, NULL, 'm'},
70 {"verbose", optional_argument, NULL, 'v'},
71 {"help", no_argument, NULL, 'h'},
72 {"version", no_argument, NULL, 'V'},
75 char *short_options = long_options_to_short_options(long_options);
80 c = getopt_long(argc, argv, short_options, long_options, NULL);
94 ovs_print_version(0, 0);
98 vlog_set_verbosity(optarg);
114 printf("%s: Open vSwitch database management utility\n"
115 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
116 " create [DB [SCHEMA]] create DB with the given SCHEMA\n"
117 " compact [DB [DST]] compact DB in-place (or to DST)\n"
118 " convert [DB [SCHEMA [DST]]] convert DB to SCHEMA (to DST)\n"
119 " db-version [DB] report version of schema used by DB\n"
120 " db-cksum [DB] report checksum of schema used by DB\n"
121 " schema-version [SCHEMA] report SCHEMA's schema version\n"
122 " schema-cksum [SCHEMA] report SCHEMA's checksum\n"
123 " query [DB] TRNS execute read-only transaction on DB\n"
124 " transact [DB] TRNS execute read/write transaction on DB\n"
125 " [-m]... show-log [DB] print DB's log entries\n"
126 "The default DB is %s.\n"
127 "The default SCHEMA is %s.\n",
128 program_name, program_name, default_db(), default_schema());
130 printf("\nOther options:\n"
131 " -m, --more increase show-log verbosity\n"
132 " -h, --help display this help message\n"
133 " -V, --version display version information\n");
142 db = xasprintf("%s/conf.db", ovs_dbdir());
152 schema = xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
158 parse_json(const char *s)
160 struct json *json = json_from_string(s);
161 if (json->type == JSON_STRING) {
162 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
168 print_and_free_json(struct json *json)
170 char *string = json_to_string(json, JSSF_SORT);
177 check_ovsdb_error(struct ovsdb_error *error)
180 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
185 do_create(int argc, char *argv[])
187 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
188 const char *schema_file_name = argc >= 3 ? argv[2] : default_schema();
189 struct ovsdb_schema *schema;
190 struct ovsdb_log *log;
193 /* Read schema from file and convert to JSON. */
194 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
195 json = ovsdb_schema_to_json(schema);
196 ovsdb_schema_destroy(schema);
198 /* Create database file. */
199 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
201 check_ovsdb_error(ovsdb_log_write(log, json));
202 check_ovsdb_error(ovsdb_log_commit(log));
203 ovsdb_log_close(log);
209 compact_or_convert(const char *src_name_, const char *dst_name_,
210 const struct ovsdb_schema *new_schema,
213 char *src_name, *dst_name;
214 struct lockfile *src_lock;
215 struct lockfile *dst_lock;
216 bool in_place = dst_name_ == NULL;
220 /* Dereference symlinks for source and destination names. In the in-place
221 * case this ensures that, if the source name is a symlink, we replace its
222 * target instead of replacing the symlink by a regular file. In the
223 * non-in-place, this has the same effect for the destination name. */
224 src_name = follow_symlinks(src_name_);
226 ? xasprintf("%s.tmp", src_name)
227 : follow_symlinks(dst_name_));
229 /* Lock the source, if we will be replacing it. */
231 retval = lockfile_lock(src_name, &src_lock);
233 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
237 /* Get (temporary) destination and lock it. */
238 retval = lockfile_lock(dst_name, &dst_lock);
240 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
244 check_ovsdb_error(new_schema
245 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
246 : ovsdb_file_open(src_name, true, &db, NULL));
247 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
250 /* Replace source. */
252 if (rename(dst_name, src_name)) {
253 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
256 fsync_parent_dir(dst_name);
257 lockfile_unlock(src_lock);
260 lockfile_unlock(dst_lock);
267 do_compact(int argc, char *argv[])
269 const char *db = argc >= 2 ? argv[1] : default_db();
270 const char *target = argc >= 3 ? argv[2] : NULL;
272 compact_or_convert(db, target, NULL, "compacted by ovsdb-tool "VERSION);
276 do_convert(int argc, char *argv[])
278 const char *db = argc >= 2 ? argv[1] : default_db();
279 const char *schema = argc >= 3 ? argv[2] : default_schema();
280 const char *target = argc >= 4 ? argv[3] : NULL;
281 struct ovsdb_schema *new_schema;
283 check_ovsdb_error(ovsdb_schema_from_file(schema, &new_schema));
284 compact_or_convert(db, target, new_schema,
285 "converted by ovsdb-tool "VERSION);
286 ovsdb_schema_destroy(new_schema);
290 do_needs_conversion(int argc, char *argv[])
292 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
293 const char *schema_file_name = argc >= 3 ? argv[2] : default_schema();
294 struct ovsdb_schema *schema1, *schema2;
296 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1));
297 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2));
298 puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes");
299 ovsdb_schema_destroy(schema1);
300 ovsdb_schema_destroy(schema2);
304 do_db_version(int argc, char *argv[])
306 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
307 struct ovsdb_schema *schema;
309 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
310 puts(schema->version);
311 ovsdb_schema_destroy(schema);
315 do_db_cksum(int argc OVS_UNUSED, char *argv[])
317 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
318 struct ovsdb_schema *schema;
320 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
322 ovsdb_schema_destroy(schema);
326 do_schema_version(int argc, char *argv[])
328 const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
329 struct ovsdb_schema *schema;
331 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
332 puts(schema->version);
333 ovsdb_schema_destroy(schema);
337 do_schema_cksum(int argc, char *argv[])
339 const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
340 struct ovsdb_schema *schema;
342 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
344 ovsdb_schema_destroy(schema);
348 transact(bool read_only, int argc, char *argv[])
350 const char *db_file_name = argc >= 3 ? argv[1] : default_db();
351 const char *transaction = argv[argc - 1];
352 struct json *request, *result;
355 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
357 request = parse_json(transaction);
358 result = ovsdb_execute(db, NULL, request, 0, NULL);
359 json_destroy(request);
361 print_and_free_json(result);
366 do_query(int argc, char *argv[])
368 transact(true, argc, argv);
372 do_transact(int argc, char *argv[])
374 transact(false, argc, argv);
378 print_db_changes(struct shash *tables, struct shash *names,
379 const struct ovsdb_schema *schema)
381 struct shash_node *n1;
383 SHASH_FOR_EACH (n1, tables) {
384 const char *table = n1->name;
385 struct ovsdb_table_schema *table_schema;
386 struct json *rows = n1->data;
387 struct shash_node *n2;
389 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
393 table_schema = shash_find_data(&schema->tables, table);
394 SHASH_FOR_EACH (n2, json_object(rows)) {
395 const char *row_uuid = n2->name;
396 struct json *columns = n2->data;
397 struct shash_node *n3;
398 char *old_name, *new_name;
399 bool free_new_name = false;
401 old_name = new_name = shash_find_data(names, row_uuid);
402 if (columns->type == JSON_OBJECT) {
403 struct json *new_name_json;
405 new_name_json = shash_find_data(json_object(columns), "name");
407 new_name = json_to_string(new_name_json, JSSF_SORT);
408 free_new_name = true;
412 printf("\ttable %s", table);
416 printf(" insert row %s (%.8s):\n", new_name, row_uuid);
418 printf(" insert row %.8s:\n", row_uuid);
421 printf(" row %s (%.8s):\n", old_name, row_uuid);
424 if (columns->type == JSON_OBJECT) {
425 if (show_log_verbosity > 1) {
426 SHASH_FOR_EACH (n3, json_object(columns)) {
427 const char *column = n3->name;
428 const struct ovsdb_column *column_schema;
429 struct json *value = n3->data;
430 char *value_string = NULL;
434 ? shash_find_data(&table_schema->columns, column)
437 const struct ovsdb_type *type;
438 struct ovsdb_error *error;
439 struct ovsdb_datum datum;
441 type = &column_schema->type;
442 error = ovsdb_datum_from_json(&datum, type,
448 ovsdb_datum_to_string(&datum, type, &s);
449 value_string = ds_steal_cstr(&s);
451 ovsdb_error_destroy(error);
455 value_string = json_to_string(value, JSSF_SORT);
457 printf("\t\t%s=%s\n", column, value_string);
462 || (new_name != old_name && strcmp(old_name, new_name))) {
464 shash_delete(names, shash_find(names, row_uuid));
467 shash_add(names, row_uuid, (new_name
469 : xmemdup0(row_uuid, 8)));
471 } else if (columns->type == JSON_NULL) {
472 struct shash_node *node;
474 printf("\t\tdelete row\n");
475 node = shash_find(names, row_uuid);
477 shash_delete(names, node);
490 do_show_log(int argc, char *argv[])
492 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
494 struct ovsdb_log *log;
495 struct ovsdb_schema *schema;
498 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
505 check_ovsdb_error(ovsdb_log_read(log, &json));
510 printf("record %u:", i);
512 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
513 printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
514 schema->name, schema->version, schema->cksum);
515 } else if (json->type == JSON_OBJECT) {
516 struct json *date, *comment;
518 date = shash_find_data(json_object(json), "_date");
519 if (date && date->type == JSON_INTEGER) {
520 long long int t = json_integer(date);
524 /* Older versions of ovsdb wrote timestamps in seconds. */
528 s = xastrftime_msec(" %Y-%m-%d %H:%M:%S.###", t, true);
533 comment = shash_find_data(json_object(json), "_comment");
534 if (comment && comment->type == JSON_STRING) {
535 printf(" \"%s\"", json_string(comment));
538 if (i > 0 && show_log_verbosity > 0) {
540 print_db_changes(json_object(json), &names, schema);
547 ovsdb_log_close(log);
548 ovsdb_schema_destroy(schema);
549 /* XXX free 'names'. */
553 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
558 static const struct command all_commands[] = {
559 { "create", 0, 2, do_create },
560 { "compact", 0, 2, do_compact },
561 { "convert", 0, 3, do_convert },
562 { "needs-conversion", 0, 2, do_needs_conversion },
563 { "db-version", 0, 1, do_db_version },
564 { "db-cksum", 0, 1, do_db_cksum },
565 { "schema-version", 0, 1, do_schema_version },
566 { "schema-cksum", 0, 1, do_schema_cksum },
567 { "query", 1, 2, do_query },
568 { "transact", 1, 2, do_transact },
569 { "show-log", 0, 1, do_show_log },
570 { "help", 0, INT_MAX, do_help },
571 { NULL, 0, 0, NULL },
574 static const struct command *get_all_commands(void)