From 6aa09313722406629133b375871547fb426800ef Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 8 Feb 2011 15:23:33 -0800 Subject: [PATCH] ovsdb-tool: Add commands for printing the database checksum. --- ovsdb/SPECS | 4 ++++ ovsdb/ovsdb-tool.1.in | 31 +++++++++++++++++++------------ ovsdb/ovsdb-tool.c | 26 ++++++++++++++++++++++++++ ovsdb/ovsdb.c | 18 ++++++++++++------ ovsdb/ovsdb.h | 6 ++++-- tests/ovsdb-execution.at | 3 ++- tests/ovsdb-server.at | 2 +- tests/ovsdb-tool.at | 20 +++++++++++++++++++- 8 files changed, 87 insertions(+), 23 deletions(-) diff --git a/ovsdb/SPECS b/ovsdb/SPECS index 97f98826..2c83cf2a 100644 --- a/ovsdb/SPECS +++ b/ovsdb/SPECS @@ -108,6 +108,7 @@ is represented by , as described below. "name": required "version": required + "cksum": optional "tables": {: , ...} required The "name" identifies the database as a whole. It must be @@ -121,6 +122,9 @@ is represented by , as described below. present. Open vSwitch semantics for "version" are described in ovs-vswitchd.conf.db(5). + The "cksum" optionally reports an implementation-defined checksum + for the database schema. + A JSON object with the following members: diff --git a/ovsdb/ovsdb-tool.1.in b/ovsdb/ovsdb-tool.1.in index 069ab1a4..79bd2a6a 100644 --- a/ovsdb/ovsdb-tool.1.in +++ b/ovsdb/ovsdb-tool.1.in @@ -23,6 +23,10 @@ ovsdb\-tool \- Open vSwitch database management utility .br \fBovsdb\-tool \fR[\fIoptions\fR] \fBschema\-version\fI schema\fR .br +\fBovsdb\-tool \fR[\fIoptions\fR] \fBdb\-cksum\fI db\fR +.br +\fBovsdb\-tool \fR[\fIoptions\fR] \fBschema\-cksum\fI schema\fR +.br \fBovsdb\-tool \fR[\fIoptions\fR] \fBquery\fI db transaction\fR .br \fBovsdb\-tool \fR[\fIoptions\fR] \fBtransact\fI db transaction\fR @@ -73,24 +77,27 @@ set to their default values. All of \fIschema\fR's constraints apply in full. . .IP "\fBdb\-version\fI db\fR" -Reads \fIdb\fR and prints the version number of the schema embedded -within the database on stdout. A schema version number has the form -\fIx\fB.\fIy\fB.\fIz\fR. See \fBovs\-vswitchd.conf.db\fR(5) for -details. +.IQ "\fBschema\-version\fI schema\fR" +Prints the version number in the schema embedded within the database +\fIdb\fR or in the standalone schema \fIschema\fR on stdout. A schema +version number has the form \fIx\fB.\fIy\fB.\fIz\fR. See +\fBovs\-vswitchd.conf.db\fR(5) for details. .IP Schema version numbers and Open vSwitch version numbers are independent. .IP -If \fIdb\fR was created before schema versioning was introduced, then -it will not have a version number and this command will print a blank -line. +If \fIschema\fR or \fIdb\fR was created before schema versioning was +introduced, then it will not have a version number and this command +will print a blank line. . -.IP "\fBschema\-version\fI schema\fR" -Reads \fIschema\fR and prints the schema's version number on stdout. +.IP "\fBdb\-cksum\fI db\fR" +.IQ "\fBschema\-cksum\fI schema\fR" +Prints the checksum in the schema embedded within the database +\fIdb\fR or of the standalone schema \fIschema\fR on stdout. .IP -If \fIschema\fR was created before versioning was introduced, then it -does not have a version number and this command will print a blank -line. +If \fIschema\fR or \fIdb\fR was created before schema versioning was +introduced, then it will not have a version number and this command +will print a blank line. . .IP "\fBquery\fI db transaction\fR" Opens \fIdb\fR, executes \fItransaction\fR on it, and prints the diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c index 2e134ce6..3730e672 100644 --- a/ovsdb/ovsdb-tool.c +++ b/ovsdb/ovsdb-tool.c @@ -111,7 +111,9 @@ usage(void) " compact DB [DST] compact DB in-place (or to DST)\n" " convert DB SCHEMA [DST] convert DB to SCHEMA (to DST)\n" " db-version DB report version of schema used by DB\n" + " db-cksum DB report checksum of schema used by DB\n" " schema-version SCHEMA report SCHEMA's schema version\n" + " schema-cksum SCHEMA report SCHEMA's checksum\n" " query DB TRNS execute read-only transaction on DB\n" " transact DB TRNS execute read/write transaction on DB\n" " show-log DB prints information about DB's log entries\n", @@ -252,6 +254,17 @@ do_db_version(int argc OVS_UNUSED, char *argv[]) ovsdb_schema_destroy(schema); } +static void +do_db_cksum(int argc OVS_UNUSED, char *argv[]) +{ + const char *db_file_name = argv[1]; + struct ovsdb_schema *schema; + + check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema)); + puts(schema->cksum); + ovsdb_schema_destroy(schema); +} + static void do_schema_version(int argc OVS_UNUSED, char *argv[]) { @@ -263,6 +276,17 @@ do_schema_version(int argc OVS_UNUSED, char *argv[]) ovsdb_schema_destroy(schema); } +static void +do_schema_cksum(int argc OVS_UNUSED, char *argv[]) +{ + const char *schema_file_name = argv[1]; + struct ovsdb_schema *schema; + + check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema)); + puts(schema->cksum); + ovsdb_schema_destroy(schema); +} + static void transact(bool read_only, const char *db_file_name, const char *transaction) { @@ -435,7 +459,9 @@ static const struct command all_commands[] = { { "compact", 1, 2, do_compact }, { "convert", 2, 3, do_convert }, { "db-version", 1, 1, do_db_version }, + { "db-cksum", 1, 1, do_db_cksum }, { "schema-version", 1, 1, do_schema_version }, + { "schema-cksum", 1, 1, do_schema_cksum }, { "query", 2, 2, do_query }, { "transact", 2, 2, do_transact }, { "show-log", 1, 1, do_show_log }, diff --git a/ovsdb/ovsdb.c b/ovsdb/ovsdb.c index b767d386..46d06a0f 100644 --- a/ovsdb/ovsdb.c +++ b/ovsdb/ovsdb.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, 2010 Nicira Networks +/* Copyright (c) 2009, 2010, 2011 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,13 +26,14 @@ #include "transaction.h" struct ovsdb_schema * -ovsdb_schema_create(const char *name, const char *version) +ovsdb_schema_create(const char *name, const char *version, const char *cksum) { struct ovsdb_schema *schema; schema = xzalloc(sizeof *schema); schema->name = xstrdup(name); schema->version = xstrdup(version); + schema->cksum = xstrdup(cksum); shash_init(&schema->tables); return schema; @@ -44,7 +45,7 @@ ovsdb_schema_clone(const struct ovsdb_schema *old) struct ovsdb_schema *new; struct shash_node *node; - new = ovsdb_schema_create(old->name, old->version); + new = ovsdb_schema_create(old->name, old->version, old->cksum); SHASH_FOR_EACH (node, &old->tables) { const struct ovsdb_table_schema *ts = node->data; @@ -68,6 +69,7 @@ ovsdb_schema_destroy(struct ovsdb_schema *schema) shash_destroy(&schema->tables); free(schema->name); free(schema->version); + free(schema->cksum); free(schema); } @@ -129,7 +131,7 @@ struct ovsdb_error * ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap) { struct ovsdb_schema *schema; - const struct json *name, *tables, *version_json; + const struct json *name, *tables, *version_json, *cksum; struct ovsdb_error *error; struct shash_node *node; struct ovsdb_parser parser; @@ -141,7 +143,7 @@ ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap) name = ovsdb_parser_member(&parser, "name", OP_ID); version_json = ovsdb_parser_member(&parser, "version", OP_STRING | OP_OPTIONAL); - ovsdb_parser_member(&parser, "cksum", OP_STRING | OP_OPTIONAL); + cksum = ovsdb_parser_member(&parser, "cksum", OP_STRING | OP_OPTIONAL); tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT); error = ovsdb_parser_finish(&parser); if (error) { @@ -159,7 +161,8 @@ ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap) version = ""; } - schema = ovsdb_schema_create(json_string(name), version); + schema = ovsdb_schema_create(json_string(name), version, + cksum ? json_string(cksum) : ""); SHASH_FOR_EACH (node, json_object(tables)) { struct ovsdb_table_schema *table; @@ -217,6 +220,9 @@ ovsdb_schema_to_json(const struct ovsdb_schema *schema) if (schema->version[0]) { json_object_put_string(json, "version", schema->version); } + if (schema->cksum[0]) { + json_object_put_string(json, "cksum", schema->cksum); + } tables = json_object_create(); diff --git a/ovsdb/ovsdb.h b/ovsdb/ovsdb.h index 642f686f..ae743bbc 100644 --- a/ovsdb/ovsdb.h +++ b/ovsdb/ovsdb.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, 2010 Nicira Networks +/* Copyright (c) 2009, 2010, 2011 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,11 +30,13 @@ struct uuid; struct ovsdb_schema { char *name; char *version; + char *cksum; struct shash tables; /* Contains "struct ovsdb_table_schema *"s. */ }; struct ovsdb_schema *ovsdb_schema_create(const char *name, - const char *version); + const char *version, + const char *cksum); struct ovsdb_schema *ovsdb_schema_clone(const struct ovsdb_schema *); void ovsdb_schema_destroy(struct ovsdb_schema *); diff --git a/tests/ovsdb-execution.at b/tests/ovsdb-execution.at index a457b2a8..d4f2380f 100644 --- a/tests/ovsdb-execution.at +++ b/tests/ovsdb-execution.at @@ -7,7 +7,8 @@ m4_define([ORDINAL_SCHEMA], "columns": { "number": {"type": "integer"}, "name": {"type": "string"}}}}, - "version": "5.1.3"}]]) + "version": "5.1.3", + "cksum": "12345678 9"}]]) m4_define([CONSTRAINT_SCHEMA], [[{"name": "constraints", diff --git a/tests/ovsdb-server.at b/tests/ovsdb-server.at index c9168f8d..dbaacd58 100644 --- a/tests/ovsdb-server.at +++ b/tests/ovsdb-server.at @@ -192,7 +192,7 @@ AT_CHECK( [0], [stdout], [ignore], [test ! -e pid || kill `cat pid`]) dnl Check that all the crap is in fact in the database log. AT_CHECK([[perl $srcdir/uuidfilt.pl db | grep -v ^OVSDB | sed 's/"_date":[0-9]*/"_date":0/' | test-json --multiple -]], [0], - [[{"name":"ordinals","tables":{"ordinals":{"columns":{"name":{"type":"string"},"number":{"type":"integer"}}}},"version":"5.1.3"} + [[{"cksum":"12345678 9","name":"ordinals","tables":{"ordinals":{"columns":{"name":{"type":"string"},"number":{"type":"integer"}}}},"version":"5.1.3"} {"_comment":"add row for zero 0","_date":0,"ordinals":{"<0>":{"name":"zero"}}} {"_comment":"delete row for 0","_date":0,"ordinals":{"<0>":null}} {"_comment":"add back row for zero 0","_date":0,"ordinals":{"<1>":{"name":"zero"}}} diff --git a/tests/ovsdb-tool.at b/tests/ovsdb-tool.at index 664e616d..0f11668e 100644 --- a/tests/ovsdb-tool.at +++ b/tests/ovsdb-tool.at @@ -83,7 +83,7 @@ AT_CHECK( [0], [stdout], [ignore]) dnl Check that all the crap is in fact in the database log. AT_CHECK([[perl $srcdir/uuidfilt.pl db | grep -v ^OVSDB | sed 's/"_date":[0-9]*/"_date":0/' | test-json --multiple -]], [0], - [[{"name":"ordinals","tables":{"ordinals":{"columns":{"name":{"type":"string"},"number":{"type":"integer"}}}},"version":"5.1.3"} + [[{"cksum":"12345678 9","name":"ordinals","tables":{"ordinals":{"columns":{"name":{"type":"string"},"number":{"type":"integer"}}}},"version":"5.1.3"} {"_comment":"add row for zero 0","_date":0,"ordinals":{"<0>":{"name":"zero"}}} {"_comment":"delete row for 0","_date":0,"ordinals":{"<0>":null}} {"_comment":"add back row for zero 0","_date":0,"ordinals":{"<1>":{"name":"zero"}}} @@ -288,3 +288,21 @@ AT_CHECK([ovsdb-tool create db schema], [0], [], [ignore]) AT_CHECK([ovsdb-tool db-version db], [0], [5.1.3 ]) AT_CLEANUP + +AT_SETUP([ovsdb-tool schema-cksum]) +AT_KEYWORDS([ovsdb file positive]) +AT_DATA([schema], [ORDINAL_SCHEMA +]) +AT_CHECK([ovsdb-tool schema-cksum schema], [0], [12345678 9 +]) +AT_CLEANUP + +AT_SETUP([ovsdb-tool db-cksum]) +AT_KEYWORDS([ovsdb file positive]) +AT_DATA([schema], [ORDINAL_SCHEMA +]) +touch .db.~lock~ +AT_CHECK([ovsdb-tool create db schema], [0], [], [ignore]) +AT_CHECK([ovsdb-tool db-cksum db], [0], [12345678 9 +]) +AT_CLEANUP -- 2.30.2