]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/ovsdb.c
dist-docs: Include manpages generated from rST.
[mirror_ovs.git] / ovsdb / ovsdb.c
CommitLineData
1b1d2e6d 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2017 Nicira, Inc.
f85f8ebb
BP
2 *
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <config.h>
17
18#include "ovsdb.h"
19
f38f98a2
IM
20#if HAVE_DECL_MALLOC_TRIM
21#include <malloc.h>
22#endif
23
0d0f05b9 24#include "column.h"
120fb2ca 25#include "file.h"
009bf21f 26#include "monitor.h"
ee89ea7b 27#include "openvswitch/json.h"
f85f8ebb
BP
28#include "ovsdb-error.h"
29#include "ovsdb-parser.h"
0d0f05b9 30#include "ovsdb-types.h"
0d085684 31#include "simap.h"
1b1d2e6d 32#include "storage.h"
f85f8ebb
BP
33#include "table.h"
34#include "transaction.h"
53178986 35#include "trigger.h"
f85f8ebb 36
1b1d2e6d
BP
37#include "openvswitch/vlog.h"
38VLOG_DEFINE_THIS_MODULE(ovsdb);
39
f85f8ebb 40struct ovsdb_schema *
6aa09313 41ovsdb_schema_create(const char *name, const char *version, const char *cksum)
f85f8ebb
BP
42{
43 struct ovsdb_schema *schema;
44
45 schema = xzalloc(sizeof *schema);
46 schema->name = xstrdup(name);
8159b984 47 schema->version = xstrdup(version);
6aa09313 48 schema->cksum = xstrdup(cksum);
f85f8ebb
BP
49 shash_init(&schema->tables);
50
51 return schema;
52}
53
58985e09
BP
54struct ovsdb_schema *
55ovsdb_schema_clone(const struct ovsdb_schema *old)
56{
57 struct ovsdb_schema *new;
58 struct shash_node *node;
59
6aa09313 60 new = ovsdb_schema_create(old->name, old->version, old->cksum);
58985e09
BP
61 SHASH_FOR_EACH (node, &old->tables) {
62 const struct ovsdb_table_schema *ts = node->data;
63
64 shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
65 }
66 return new;
67}
68
f85f8ebb
BP
69void
70ovsdb_schema_destroy(struct ovsdb_schema *schema)
71{
72 struct shash_node *node;
73
271915d3
BP
74 if (!schema) {
75 return;
76 }
77
f85f8ebb
BP
78 SHASH_FOR_EACH (node, &schema->tables) {
79 ovsdb_table_schema_destroy(node->data);
80 }
81 shash_destroy(&schema->tables);
f85f8ebb 82 free(schema->name);
8159b984 83 free(schema->version);
6aa09313 84 free(schema->cksum);
f85f8ebb
BP
85 free(schema);
86}
87
88struct ovsdb_error *
89ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
90{
91 struct ovsdb_schema *schema;
92 struct ovsdb_error *error;
93 struct json *json;
94
95 *schemap = NULL;
96 json = json_from_file(file_name);
97 if (json->type == JSON_STRING) {
98 error = ovsdb_error("failed to read schema",
99 "\"%s\" could not be read as JSON (%s)",
100 file_name, json_string(json));
101 json_destroy(json);
102 return error;
103 }
104
105 error = ovsdb_schema_from_json(json, &schema);
e084f690 106 json_destroy(json);
f85f8ebb 107 if (error) {
f85f8ebb
BP
108 return ovsdb_wrap_error(error,
109 "failed to parse \"%s\" as ovsdb schema",
110 file_name);
111 }
112
113 *schemap = schema;
114 return NULL;
115}
116
cab50449 117static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
42a49b96 118ovsdb_schema_check_ref_table(struct ovsdb_column *column,
0d0f05b9
BP
119 const struct shash *tables,
120 const struct ovsdb_base_type *base,
121 const char *base_name)
122{
42a49b96
BP
123 struct ovsdb_table_schema *refTable;
124
fa37affa 125 if (base->type != OVSDB_TYPE_UUID || !base->uuid.refTableName) {
42a49b96
BP
126 return NULL;
127 }
128
fa37affa 129 refTable = shash_find_data(tables, base->uuid.refTableName);
42a49b96 130 if (!refTable) {
0d0f05b9
BP
131 return ovsdb_syntax_error(NULL, NULL,
132 "column %s %s refers to undefined table %s",
133 column->name, base_name,
fa37affa 134 base->uuid.refTableName);
0d0f05b9 135 }
42a49b96
BP
136
137 if (ovsdb_base_type_is_strong_ref(base) && !refTable->is_root) {
138 /* We cannot allow a strong reference to a non-root table to be
139 * ephemeral: if it is the only reference to a row, then replaying the
140 * database log from disk will cause the referenced row to be deleted,
141 * even though it did exist in memory. If there are references to that
142 * row later in the log (to modify it, to delete it, or just to point
143 * to it), then this will yield a transaction error. */
144 column->persistent = true;
145 }
146
147 return NULL;
0d0f05b9
BP
148}
149
1b1d2e6d
BP
150/* Attempts to parse 's' as a version string in the format "<x>.<y>.<z>". If
151 * successful, stores each part of the version into 'version->x', 'version->y',
152 * and 'version->z', respectively, and returns true. On failure, returns
153 * false. */
154bool
155ovsdb_parse_version(const char *s, struct ovsdb_version *version)
8159b984
BP
156{
157 int n = -1;
1b1d2e6d
BP
158 return (ovs_scan(s, "%u.%u.%u%n", &version->x, &version->y, &version->z,
159 &n)
160 && n != -1 && s[n] == '\0');
161}
162
163/* Returns true if 's' is a version string in the format "<x>.<y>.<z>",
164 * otherwie false. */
165bool
166ovsdb_is_valid_version(const char *s)
167{
168 struct ovsdb_version version;
169 return ovsdb_parse_version(s, &version);
8159b984
BP
170}
171
c5f341ab
BP
172/* Returns the number of tables in 'schema''s root set. */
173static size_t
174root_set_size(const struct ovsdb_schema *schema)
175{
176 struct shash_node *node;
85dcedff 177 size_t n_root = 0;
c5f341ab
BP
178
179 SHASH_FOR_EACH (node, &schema->tables) {
180 struct ovsdb_table_schema *table = node->data;
181
182 n_root += table->is_root;
183 }
184 return n_root;
185}
186
f85f8ebb 187struct ovsdb_error *
53178986 188ovsdb_schema_from_json(const struct json *json, struct ovsdb_schema **schemap)
f85f8ebb
BP
189{
190 struct ovsdb_schema *schema;
6aa09313 191 const struct json *name, *tables, *version_json, *cksum;
f85f8ebb
BP
192 struct ovsdb_error *error;
193 struct shash_node *node;
194 struct ovsdb_parser parser;
8159b984 195 const char *version;
f85f8ebb
BP
196
197 *schemap = NULL;
198
199 ovsdb_parser_init(&parser, json, "database schema");
200 name = ovsdb_parser_member(&parser, "name", OP_ID);
8159b984
BP
201 version_json = ovsdb_parser_member(&parser, "version",
202 OP_STRING | OP_OPTIONAL);
6aa09313 203 cksum = ovsdb_parser_member(&parser, "cksum", OP_STRING | OP_OPTIONAL);
f85f8ebb
BP
204 tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
205 error = ovsdb_parser_finish(&parser);
206 if (error) {
207 return error;
208 }
209
8159b984
BP
210 if (version_json) {
211 version = json_string(version_json);
1b1d2e6d 212 if (!ovsdb_is_valid_version(version)) {
8159b984
BP
213 return ovsdb_syntax_error(json, NULL, "schema version \"%s\" not "
214 "in format x.y.z", version);
215 }
216 } else {
217 /* Backward compatibility with old databases. */
218 version = "";
219 }
220
6aa09313
BP
221 schema = ovsdb_schema_create(json_string(name), version,
222 cksum ? json_string(cksum) : "");
f85f8ebb
BP
223 SHASH_FOR_EACH (node, json_object(tables)) {
224 struct ovsdb_table_schema *table;
225
226 if (node->name[0] == '_') {
227 error = ovsdb_syntax_error(json, NULL, "names beginning with "
228 "\"_\" are reserved");
b966380b
BP
229 } else if (!ovsdb_parser_is_id(node->name)) {
230 error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
f85f8ebb
BP
231 } else {
232 error = ovsdb_table_schema_from_json(node->data, node->name,
233 &table);
234 }
235 if (error) {
236 ovsdb_schema_destroy(schema);
237 return error;
238 }
239
240 shash_add(&schema->tables, table->name, table);
241 }
0d0f05b9 242
42a49b96
BP
243 /* "isRoot" was not part of the original schema definition. Before it was
244 * added, there was no support for garbage collection. So, for backward
245 * compatibility, if the root set is empty then assume that every table is
246 * in the root set. */
247 if (root_set_size(schema) == 0) {
248 SHASH_FOR_EACH (node, &schema->tables) {
249 struct ovsdb_table_schema *table = node->data;
250
251 table->is_root = true;
252 }
253 }
254
255 /* Validate that all refTables refer to the names of tables that exist.
256 *
257 * Also force certain columns to be persistent, as explained in
258 * ovsdb_schema_check_ref_table(). This requires 'is_root' to be known, so
259 * this must follow the loop updating 'is_root' above. */
0d0f05b9
BP
260 SHASH_FOR_EACH (node, &schema->tables) {
261 struct ovsdb_table_schema *table = node->data;
262 struct shash_node *node2;
263
264 SHASH_FOR_EACH (node2, &table->columns) {
265 struct ovsdb_column *column = node2->data;
266
267 error = ovsdb_schema_check_ref_table(column, &schema->tables,
268 &column->type.key, "key");
269 if (!error) {
270 error = ovsdb_schema_check_ref_table(column, &schema->tables,
271 &column->type.value,
272 "value");
273 }
274 if (error) {
275 ovsdb_schema_destroy(schema);
276 return error;
277 }
278 }
279 }
280
f85f8ebb 281 *schemap = schema;
e3c17733 282 return NULL;
f85f8ebb
BP
283}
284
285struct json *
286ovsdb_schema_to_json(const struct ovsdb_schema *schema)
287{
288 struct json *json, *tables;
289 struct shash_node *node;
c5f341ab 290 bool default_is_root;
f85f8ebb
BP
291
292 json = json_object_create();
293 json_object_put_string(json, "name", schema->name);
8159b984
BP
294 if (schema->version[0]) {
295 json_object_put_string(json, "version", schema->version);
296 }
6aa09313
BP
297 if (schema->cksum[0]) {
298 json_object_put_string(json, "cksum", schema->cksum);
299 }
f85f8ebb 300
c5f341ab
BP
301 /* "isRoot" was not part of the original schema definition. Before it was
302 * added, there was no support for garbage collection. So, for backward
303 * compatibility, if every table is in the root set then do not output
304 * "isRoot" in table schemas. */
305 default_is_root = root_set_size(schema) == shash_count(&schema->tables);
306
f85f8ebb
BP
307 tables = json_object_create();
308
309 SHASH_FOR_EACH (node, &schema->tables) {
310 struct ovsdb_table_schema *table = node->data;
311 json_object_put(tables, table->name,
c5f341ab 312 ovsdb_table_schema_to_json(table, default_is_root));
f85f8ebb
BP
313 }
314 json_object_put(json, "tables", tables);
315
316 return json;
317}
403e3a25
BP
318
319/* Returns true if 'a' and 'b' specify equivalent schemas, false if they
320 * differ. */
321bool
322ovsdb_schema_equal(const struct ovsdb_schema *a,
323 const struct ovsdb_schema *b)
324{
325 /* This implementation is simple, stupid, and slow, but I doubt that it
326 * will ever require much maintenance. */
327 struct json *ja = ovsdb_schema_to_json(a);
328 struct json *jb = ovsdb_schema_to_json(b);
329 bool equals = json_equal(ja, jb);
330 json_destroy(ja);
331 json_destroy(jb);
332
333 return equals;
334}
1b1d2e6d
BP
335
336struct ovsdb_error * OVS_WARN_UNUSED_RESULT
337ovsdb_schema_check_for_ephemeral_columns(const struct ovsdb_schema *schema)
338{
339 struct shash_node *node;
340 SHASH_FOR_EACH (node, &schema->tables) {
341 struct ovsdb_table_schema *table = node->data;
342 struct shash_node *node2;
343
344 SHASH_FOR_EACH (node2, &table->columns) {
345 struct ovsdb_column *column = node2->data;
346
347 if (column->index >= OVSDB_N_STD_COLUMNS && !column->persistent) {
348 return ovsdb_syntax_error(
349 NULL, NULL, "Table %s column %s is ephemeral but "
350 "clustered databases do not support ephemeral columns.",
351 table->name, column->name);
352 }
353 }
354 }
355 return NULL;
356}
357
358void
359ovsdb_schema_persist_ephemeral_columns(struct ovsdb_schema *schema,
360 const char *filename)
361{
362 int n = 0;
363 const char *example_table = NULL;
364 const char *example_column = NULL;
365
366 struct shash_node *node;
367 SHASH_FOR_EACH (node, &schema->tables) {
368 struct ovsdb_table_schema *table = node->data;
369 struct shash_node *node2;
370
371 SHASH_FOR_EACH (node2, &table->columns) {
372 struct ovsdb_column *column = node2->data;
373
374 if (column->index >= OVSDB_N_STD_COLUMNS && !column->persistent) {
375 column->persistent = true;
376 example_table = table->name;
377 example_column = column->name;
378 n++;
379 }
380 }
381 }
382
383 if (n) {
384 VLOG_WARN("%s: changed %d columns in '%s' database from ephemeral to "
385 "persistent, including '%s' column in '%s' table, because "
386 "clusters do not support ephemeral columns",
387 filename, n, schema->name, example_column, example_table);
388 }
389}
f85f8ebb 390\f
0d0f05b9
BP
391static void
392ovsdb_set_ref_table(const struct shash *tables,
393 struct ovsdb_base_type *base)
394{
fa37affa 395 if (base->type == OVSDB_TYPE_UUID && base->uuid.refTableName) {
0d0f05b9
BP
396 struct ovsdb_table *table;
397
fa37affa
BP
398 table = shash_find_data(tables, base->uuid.refTableName);
399 base->uuid.refTable = table;
0d0f05b9
BP
400 }
401}
402
1b1d2e6d
BP
403/* Creates and returns a new ovsdb based on 'schema' and 'storage' and takes
404 * ownership of both.
405 *
406 * At least one of the arguments must be nonnull. */
f85f8ebb 407struct ovsdb *
1b1d2e6d 408ovsdb_create(struct ovsdb_schema *schema, struct ovsdb_storage *storage)
f85f8ebb
BP
409{
410 struct shash_node *node;
411 struct ovsdb *db;
412
1b1d2e6d
BP
413 db = xzalloc(sizeof *db);
414 db->name = xstrdup(schema
415 ? schema->name
416 : ovsdb_storage_get_name(storage));
f85f8ebb 417 db->schema = schema;
1b1d2e6d 418 db->storage = storage;
009bf21f 419 ovs_list_init(&db->monitors);
417e7e66 420 ovs_list_init(&db->triggers);
bb66a0a6 421 db->run_triggers_now = db->run_triggers = false;
f85f8ebb
BP
422
423 shash_init(&db->tables);
1b1d2e6d
BP
424 if (schema) {
425 SHASH_FOR_EACH (node, &schema->tables) {
426 struct ovsdb_table_schema *ts = node->data;
427 shash_add(&db->tables, node->name, ovsdb_table_create(ts));
428 }
f85f8ebb 429
1b1d2e6d
BP
430 /* Set all the refTables. */
431 SHASH_FOR_EACH (node, &schema->tables) {
432 struct ovsdb_table_schema *table = node->data;
433 struct shash_node *node2;
0d0f05b9 434
1b1d2e6d
BP
435 SHASH_FOR_EACH (node2, &table->columns) {
436 struct ovsdb_column *column = node2->data;
0d0f05b9 437
1b1d2e6d
BP
438 ovsdb_set_ref_table(&db->tables, &column->type.key);
439 ovsdb_set_ref_table(&db->tables, &column->type.value);
440 }
0d0f05b9
BP
441 }
442 }
443
d6db7b3c
LR
444 /* Use RBAC roles table if present. */
445 db->rbac_role = ovsdb_get_table(db, "RBAC_Role");
446
f85f8ebb
BP
447 return db;
448}
449
f85f8ebb
BP
450void
451ovsdb_destroy(struct ovsdb *db)
452{
453 if (db) {
454 struct shash_node *node;
455
120fb2ca 456 /* Close the log. */
1b1d2e6d 457 ovsdb_storage_close(db->storage);
120fb2ca 458
009bf21f
BP
459 /* Remove all the monitors. */
460 ovsdb_monitors_remove(db);
bd06962a 461
695e8150
HZ
462 /* Destroy txn history. */
463 ovsdb_txn_history_destroy(db);
464
1b1d2e6d
BP
465 /* The caller must ensure that no triggers remain. */
466 ovs_assert(ovs_list_is_empty(&db->triggers));
467
f85f8ebb
BP
468 /* Delete all the tables. This also deletes their schemas. */
469 SHASH_FOR_EACH (node, &db->tables) {
470 struct ovsdb_table *table = node->data;
471 ovsdb_table_destroy(table);
472 }
473 shash_destroy(&db->tables);
474
1248fcd2
BP
475 /* The schemas, but not the table that points to them, were deleted in
476 * the previous step, so we need to clear out the table. We can't
477 * destroy the table, because ovsdb_schema_destroy() will do that. */
1b1d2e6d
BP
478 if (db->schema) {
479 shash_clear(&db->schema->tables);
480 ovsdb_schema_destroy(db->schema);
481 }
f85f8ebb 482
1b1d2e6d 483 free(db->name);
f85f8ebb
BP
484 free(db);
485 }
486}
487
0d085684
BP
488/* Adds some memory usage statistics for 'db' into 'usage', for use with
489 * memory_report(). */
490void
491ovsdb_get_memory_usage(const struct ovsdb *db, struct simap *usage)
492{
1b1d2e6d
BP
493 if (!db->schema) {
494 return;
495 }
496
0d085684
BP
497 const struct shash_node *node;
498 unsigned int cells = 0;
499
500 SHASH_FOR_EACH (node, &db->tables) {
501 const struct ovsdb_table *table = node->data;
502 unsigned int n_columns = shash_count(&table->schema->columns);
503 unsigned int n_rows = hmap_count(&table->rows);
504
505 cells += n_rows * n_columns;
506 }
507
508 simap_increase(usage, "cells", cells);
3423cd97
IM
509
510 if (db->storage) {
511 ovsdb_storage_get_memory_usage(db->storage, usage);
512 }
0d085684
BP
513}
514
f85f8ebb
BP
515struct ovsdb_table *
516ovsdb_get_table(const struct ovsdb *db, const char *name)
517{
518 return shash_find_data(&db->tables, name);
519}
1b1d2e6d
BP
520
521struct ovsdb_error * OVS_WARN_UNUSED_RESULT
f38f98a2 522ovsdb_snapshot(struct ovsdb *db, bool trim_memory OVS_UNUSED)
1b1d2e6d
BP
523{
524 if (!db->storage) {
525 return NULL;
526 }
527
528 struct json *schema = ovsdb_schema_to_json(db->schema);
529 struct json *data = ovsdb_to_txn_json(db, "compacting database online");
530 struct ovsdb_error *error = ovsdb_storage_store_snapshot(db->storage,
531 schema, data);
532 json_destroy(schema);
533 json_destroy(data);
f38f98a2
IM
534
535#if HAVE_DECL_MALLOC_TRIM
536 if (!error && trim_memory) {
537 malloc_trim(0);
538 }
539#endif
1b1d2e6d
BP
540 return error;
541}
542
543void
544ovsdb_replace(struct ovsdb *dst, struct ovsdb *src)
545{
546 /* Cancel monitors. */
547 ovsdb_monitor_prereplace_db(dst);
548
549 /* Cancel triggers. */
550 struct ovsdb_trigger *trigger, *next;
551 LIST_FOR_EACH_SAFE (trigger, next, node, &dst->triggers) {
552 ovsdb_trigger_prereplace_db(trigger);
553 }
554
695e8150
HZ
555 /* Destroy txn history. */
556 ovsdb_txn_history_destroy(dst);
557
1b1d2e6d
BP
558 struct ovsdb_schema *tmp_schema = dst->schema;
559 dst->schema = src->schema;
560 src->schema = tmp_schema;
561
562 shash_swap(&dst->tables, &src->tables);
563
564 dst->rbac_role = ovsdb_get_table(dst, "RBAC_Role");
565
566 ovsdb_destroy(src);
567}