]>
git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/ovsdb.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2017 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
23 #include "openvswitch/json.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
26 #include "ovsdb-types.h"
30 #include "transaction.h"
33 #include "openvswitch/vlog.h"
34 VLOG_DEFINE_THIS_MODULE(ovsdb
);
37 ovsdb_schema_create(const char *name
, const char *version
, const char *cksum
)
39 struct ovsdb_schema
*schema
;
41 schema
= xzalloc(sizeof *schema
);
42 schema
->name
= xstrdup(name
);
43 schema
->version
= xstrdup(version
);
44 schema
->cksum
= xstrdup(cksum
);
45 shash_init(&schema
->tables
);
51 ovsdb_schema_clone(const struct ovsdb_schema
*old
)
53 struct ovsdb_schema
*new;
54 struct shash_node
*node
;
56 new = ovsdb_schema_create(old
->name
, old
->version
, old
->cksum
);
57 SHASH_FOR_EACH (node
, &old
->tables
) {
58 const struct ovsdb_table_schema
*ts
= node
->data
;
60 shash_add(&new->tables
, node
->name
, ovsdb_table_schema_clone(ts
));
66 ovsdb_schema_destroy(struct ovsdb_schema
*schema
)
68 struct shash_node
*node
;
74 SHASH_FOR_EACH (node
, &schema
->tables
) {
75 ovsdb_table_schema_destroy(node
->data
);
77 shash_destroy(&schema
->tables
);
79 free(schema
->version
);
85 ovsdb_schema_from_file(const char *file_name
, struct ovsdb_schema
**schemap
)
87 struct ovsdb_schema
*schema
;
88 struct ovsdb_error
*error
;
92 json
= json_from_file(file_name
);
93 if (json
->type
== JSON_STRING
) {
94 error
= ovsdb_error("failed to read schema",
95 "\"%s\" could not be read as JSON (%s)",
96 file_name
, json_string(json
));
101 error
= ovsdb_schema_from_json(json
, &schema
);
104 return ovsdb_wrap_error(error
,
105 "failed to parse \"%s\" as ovsdb schema",
113 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
114 ovsdb_schema_check_ref_table(struct ovsdb_column
*column
,
115 const struct shash
*tables
,
116 const struct ovsdb_base_type
*base
,
117 const char *base_name
)
119 struct ovsdb_table_schema
*refTable
;
121 if (base
->type
!= OVSDB_TYPE_UUID
|| !base
->uuid
.refTableName
) {
125 refTable
= shash_find_data(tables
, base
->uuid
.refTableName
);
127 return ovsdb_syntax_error(NULL
, NULL
,
128 "column %s %s refers to undefined table %s",
129 column
->name
, base_name
,
130 base
->uuid
.refTableName
);
133 if (ovsdb_base_type_is_strong_ref(base
) && !refTable
->is_root
) {
134 /* We cannot allow a strong reference to a non-root table to be
135 * ephemeral: if it is the only reference to a row, then replaying the
136 * database log from disk will cause the referenced row to be deleted,
137 * even though it did exist in memory. If there are references to that
138 * row later in the log (to modify it, to delete it, or just to point
139 * to it), then this will yield a transaction error. */
140 column
->persistent
= true;
146 /* Attempts to parse 's' as a version string in the format "<x>.<y>.<z>". If
147 * successful, stores each part of the version into 'version->x', 'version->y',
148 * and 'version->z', respectively, and returns true. On failure, returns
151 ovsdb_parse_version(const char *s
, struct ovsdb_version
*version
)
154 return (ovs_scan(s
, "%u.%u.%u%n", &version
->x
, &version
->y
, &version
->z
,
156 && n
!= -1 && s
[n
] == '\0');
159 /* Returns true if 's' is a version string in the format "<x>.<y>.<z>",
162 ovsdb_is_valid_version(const char *s
)
164 struct ovsdb_version version
;
165 return ovsdb_parse_version(s
, &version
);
168 /* Returns the number of tables in 'schema''s root set. */
170 root_set_size(const struct ovsdb_schema
*schema
)
172 struct shash_node
*node
;
175 SHASH_FOR_EACH (node
, &schema
->tables
) {
176 struct ovsdb_table_schema
*table
= node
->data
;
178 n_root
+= table
->is_root
;
184 ovsdb_schema_from_json(const struct json
*json
, struct ovsdb_schema
**schemap
)
186 struct ovsdb_schema
*schema
;
187 const struct json
*name
, *tables
, *version_json
, *cksum
;
188 struct ovsdb_error
*error
;
189 struct shash_node
*node
;
190 struct ovsdb_parser parser
;
195 ovsdb_parser_init(&parser
, json
, "database schema");
196 name
= ovsdb_parser_member(&parser
, "name", OP_ID
);
197 version_json
= ovsdb_parser_member(&parser
, "version",
198 OP_STRING
| OP_OPTIONAL
);
199 cksum
= ovsdb_parser_member(&parser
, "cksum", OP_STRING
| OP_OPTIONAL
);
200 tables
= ovsdb_parser_member(&parser
, "tables", OP_OBJECT
);
201 error
= ovsdb_parser_finish(&parser
);
207 version
= json_string(version_json
);
208 if (!ovsdb_is_valid_version(version
)) {
209 return ovsdb_syntax_error(json
, NULL
, "schema version \"%s\" not "
210 "in format x.y.z", version
);
213 /* Backward compatibility with old databases. */
217 schema
= ovsdb_schema_create(json_string(name
), version
,
218 cksum
? json_string(cksum
) : "");
219 SHASH_FOR_EACH (node
, json_object(tables
)) {
220 struct ovsdb_table_schema
*table
;
222 if (node
->name
[0] == '_') {
223 error
= ovsdb_syntax_error(json
, NULL
, "names beginning with "
224 "\"_\" are reserved");
225 } else if (!ovsdb_parser_is_id(node
->name
)) {
226 error
= ovsdb_syntax_error(json
, NULL
, "name must be a valid id");
228 error
= ovsdb_table_schema_from_json(node
->data
, node
->name
,
232 ovsdb_schema_destroy(schema
);
236 shash_add(&schema
->tables
, table
->name
, table
);
239 /* "isRoot" was not part of the original schema definition. Before it was
240 * added, there was no support for garbage collection. So, for backward
241 * compatibility, if the root set is empty then assume that every table is
242 * in the root set. */
243 if (root_set_size(schema
) == 0) {
244 SHASH_FOR_EACH (node
, &schema
->tables
) {
245 struct ovsdb_table_schema
*table
= node
->data
;
247 table
->is_root
= true;
251 /* Validate that all refTables refer to the names of tables that exist.
253 * Also force certain columns to be persistent, as explained in
254 * ovsdb_schema_check_ref_table(). This requires 'is_root' to be known, so
255 * this must follow the loop updating 'is_root' above. */
256 SHASH_FOR_EACH (node
, &schema
->tables
) {
257 struct ovsdb_table_schema
*table
= node
->data
;
258 struct shash_node
*node2
;
260 SHASH_FOR_EACH (node2
, &table
->columns
) {
261 struct ovsdb_column
*column
= node2
->data
;
263 error
= ovsdb_schema_check_ref_table(column
, &schema
->tables
,
264 &column
->type
.key
, "key");
266 error
= ovsdb_schema_check_ref_table(column
, &schema
->tables
,
271 ovsdb_schema_destroy(schema
);
282 ovsdb_schema_to_json(const struct ovsdb_schema
*schema
)
284 struct json
*json
, *tables
;
285 struct shash_node
*node
;
286 bool default_is_root
;
288 json
= json_object_create();
289 json_object_put_string(json
, "name", schema
->name
);
290 if (schema
->version
[0]) {
291 json_object_put_string(json
, "version", schema
->version
);
293 if (schema
->cksum
[0]) {
294 json_object_put_string(json
, "cksum", schema
->cksum
);
297 /* "isRoot" was not part of the original schema definition. Before it was
298 * added, there was no support for garbage collection. So, for backward
299 * compatibility, if every table is in the root set then do not output
300 * "isRoot" in table schemas. */
301 default_is_root
= root_set_size(schema
) == shash_count(&schema
->tables
);
303 tables
= json_object_create();
305 SHASH_FOR_EACH (node
, &schema
->tables
) {
306 struct ovsdb_table_schema
*table
= node
->data
;
307 json_object_put(tables
, table
->name
,
308 ovsdb_table_schema_to_json(table
, default_is_root
));
310 json_object_put(json
, "tables", tables
);
315 /* Returns true if 'a' and 'b' specify equivalent schemas, false if they
318 ovsdb_schema_equal(const struct ovsdb_schema
*a
,
319 const struct ovsdb_schema
*b
)
321 /* This implementation is simple, stupid, and slow, but I doubt that it
322 * will ever require much maintenance. */
323 struct json
*ja
= ovsdb_schema_to_json(a
);
324 struct json
*jb
= ovsdb_schema_to_json(b
);
325 bool equals
= json_equal(ja
, jb
);
332 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
333 ovsdb_schema_check_for_ephemeral_columns(const struct ovsdb_schema
*schema
)
335 struct shash_node
*node
;
336 SHASH_FOR_EACH (node
, &schema
->tables
) {
337 struct ovsdb_table_schema
*table
= node
->data
;
338 struct shash_node
*node2
;
340 SHASH_FOR_EACH (node2
, &table
->columns
) {
341 struct ovsdb_column
*column
= node2
->data
;
343 if (column
->index
>= OVSDB_N_STD_COLUMNS
&& !column
->persistent
) {
344 return ovsdb_syntax_error(
345 NULL
, NULL
, "Table %s column %s is ephemeral but "
346 "clustered databases do not support ephemeral columns.",
347 table
->name
, column
->name
);
355 ovsdb_schema_persist_ephemeral_columns(struct ovsdb_schema
*schema
,
356 const char *filename
)
359 const char *example_table
= NULL
;
360 const char *example_column
= NULL
;
362 struct shash_node
*node
;
363 SHASH_FOR_EACH (node
, &schema
->tables
) {
364 struct ovsdb_table_schema
*table
= node
->data
;
365 struct shash_node
*node2
;
367 SHASH_FOR_EACH (node2
, &table
->columns
) {
368 struct ovsdb_column
*column
= node2
->data
;
370 if (column
->index
>= OVSDB_N_STD_COLUMNS
&& !column
->persistent
) {
371 column
->persistent
= true;
372 example_table
= table
->name
;
373 example_column
= column
->name
;
380 VLOG_WARN("%s: changed %d columns in '%s' database from ephemeral to "
381 "persistent, including '%s' column in '%s' table, because "
382 "clusters do not support ephemeral columns",
383 filename
, n
, schema
->name
, example_column
, example_table
);
388 ovsdb_set_ref_table(const struct shash
*tables
,
389 struct ovsdb_base_type
*base
)
391 if (base
->type
== OVSDB_TYPE_UUID
&& base
->uuid
.refTableName
) {
392 struct ovsdb_table
*table
;
394 table
= shash_find_data(tables
, base
->uuid
.refTableName
);
395 base
->uuid
.refTable
= table
;
399 /* Creates and returns a new ovsdb based on 'schema' and 'storage' and takes
402 * At least one of the arguments must be nonnull. */
404 ovsdb_create(struct ovsdb_schema
*schema
, struct ovsdb_storage
*storage
)
406 struct shash_node
*node
;
409 db
= xzalloc(sizeof *db
);
410 db
->name
= xstrdup(schema
412 : ovsdb_storage_get_name(storage
));
414 db
->storage
= storage
;
415 ovs_list_init(&db
->monitors
);
416 ovs_list_init(&db
->triggers
);
417 db
->run_triggers
= false;
419 shash_init(&db
->tables
);
421 SHASH_FOR_EACH (node
, &schema
->tables
) {
422 struct ovsdb_table_schema
*ts
= node
->data
;
423 shash_add(&db
->tables
, node
->name
, ovsdb_table_create(ts
));
426 /* Set all the refTables. */
427 SHASH_FOR_EACH (node
, &schema
->tables
) {
428 struct ovsdb_table_schema
*table
= node
->data
;
429 struct shash_node
*node2
;
431 SHASH_FOR_EACH (node2
, &table
->columns
) {
432 struct ovsdb_column
*column
= node2
->data
;
434 ovsdb_set_ref_table(&db
->tables
, &column
->type
.key
);
435 ovsdb_set_ref_table(&db
->tables
, &column
->type
.value
);
440 /* Use RBAC roles table if present. */
441 db
->rbac_role
= ovsdb_get_table(db
, "RBAC_Role");
447 ovsdb_destroy(struct ovsdb
*db
)
450 struct shash_node
*node
;
453 ovsdb_storage_close(db
->storage
);
455 /* Remove all the monitors. */
456 ovsdb_monitors_remove(db
);
458 /* Destroy txn history. */
459 ovsdb_txn_history_destroy(db
);
461 /* The caller must ensure that no triggers remain. */
462 ovs_assert(ovs_list_is_empty(&db
->triggers
));
464 /* Delete all the tables. This also deletes their schemas. */
465 SHASH_FOR_EACH (node
, &db
->tables
) {
466 struct ovsdb_table
*table
= node
->data
;
467 ovsdb_table_destroy(table
);
469 shash_destroy(&db
->tables
);
471 /* The schemas, but not the table that points to them, were deleted in
472 * the previous step, so we need to clear out the table. We can't
473 * destroy the table, because ovsdb_schema_destroy() will do that. */
475 shash_clear(&db
->schema
->tables
);
476 ovsdb_schema_destroy(db
->schema
);
484 /* Adds some memory usage statistics for 'db' into 'usage', for use with
485 * memory_report(). */
487 ovsdb_get_memory_usage(const struct ovsdb
*db
, struct simap
*usage
)
493 const struct shash_node
*node
;
494 unsigned int cells
= 0;
496 SHASH_FOR_EACH (node
, &db
->tables
) {
497 const struct ovsdb_table
*table
= node
->data
;
498 unsigned int n_columns
= shash_count(&table
->schema
->columns
);
499 unsigned int n_rows
= hmap_count(&table
->rows
);
501 cells
+= n_rows
* n_columns
;
504 simap_increase(usage
, "cells", cells
);
508 ovsdb_get_table(const struct ovsdb
*db
, const char *name
)
510 return shash_find_data(&db
->tables
, name
);
513 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
514 ovsdb_snapshot(struct ovsdb
*db
)
520 struct json
*schema
= ovsdb_schema_to_json(db
->schema
);
521 struct json
*data
= ovsdb_to_txn_json(db
, "compacting database online");
522 struct ovsdb_error
*error
= ovsdb_storage_store_snapshot(db
->storage
,
524 json_destroy(schema
);
530 ovsdb_replace(struct ovsdb
*dst
, struct ovsdb
*src
)
532 /* Cancel monitors. */
533 ovsdb_monitor_prereplace_db(dst
);
535 /* Cancel triggers. */
536 struct ovsdb_trigger
*trigger
, *next
;
537 LIST_FOR_EACH_SAFE (trigger
, next
, node
, &dst
->triggers
) {
538 ovsdb_trigger_prereplace_db(trigger
);
541 /* Destroy txn history. */
542 ovsdb_txn_history_destroy(dst
);
544 struct ovsdb_schema
*tmp_schema
= dst
->schema
;
545 dst
->schema
= src
->schema
;
546 src
->schema
= tmp_schema
;
548 shash_swap(&dst
->tables
, &src
->tables
);
550 dst
->rbac_role
= ovsdb_get_table(dst
, "RBAC_Role");