]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/ovsdb.c
Eliminate "whitelist" and "blacklist" terms.
[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
0d0f05b9 20#include "column.h"
120fb2ca 21#include "file.h"
009bf21f 22#include "monitor.h"
ee89ea7b 23#include "openvswitch/json.h"
f85f8ebb
BP
24#include "ovsdb-error.h"
25#include "ovsdb-parser.h"
0d0f05b9 26#include "ovsdb-types.h"
0d085684 27#include "simap.h"
1b1d2e6d 28#include "storage.h"
f85f8ebb
BP
29#include "table.h"
30#include "transaction.h"
53178986 31#include "trigger.h"
f85f8ebb 32
1b1d2e6d
BP
33#include "openvswitch/vlog.h"
34VLOG_DEFINE_THIS_MODULE(ovsdb);
35
f85f8ebb 36struct ovsdb_schema *
6aa09313 37ovsdb_schema_create(const char *name, const char *version, const char *cksum)
f85f8ebb
BP
38{
39 struct ovsdb_schema *schema;
40
41 schema = xzalloc(sizeof *schema);
42 schema->name = xstrdup(name);
8159b984 43 schema->version = xstrdup(version);
6aa09313 44 schema->cksum = xstrdup(cksum);
f85f8ebb
BP
45 shash_init(&schema->tables);
46
47 return schema;
48}
49
58985e09
BP
50struct ovsdb_schema *
51ovsdb_schema_clone(const struct ovsdb_schema *old)
52{
53 struct ovsdb_schema *new;
54 struct shash_node *node;
55
6aa09313 56 new = ovsdb_schema_create(old->name, old->version, old->cksum);
58985e09
BP
57 SHASH_FOR_EACH (node, &old->tables) {
58 const struct ovsdb_table_schema *ts = node->data;
59
60 shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
61 }
62 return new;
63}
64
f85f8ebb
BP
65void
66ovsdb_schema_destroy(struct ovsdb_schema *schema)
67{
68 struct shash_node *node;
69
271915d3
BP
70 if (!schema) {
71 return;
72 }
73
f85f8ebb
BP
74 SHASH_FOR_EACH (node, &schema->tables) {
75 ovsdb_table_schema_destroy(node->data);
76 }
77 shash_destroy(&schema->tables);
f85f8ebb 78 free(schema->name);
8159b984 79 free(schema->version);
6aa09313 80 free(schema->cksum);
f85f8ebb
BP
81 free(schema);
82}
83
84struct ovsdb_error *
85ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
86{
87 struct ovsdb_schema *schema;
88 struct ovsdb_error *error;
89 struct json *json;
90
91 *schemap = NULL;
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));
97 json_destroy(json);
98 return error;
99 }
100
101 error = ovsdb_schema_from_json(json, &schema);
e084f690 102 json_destroy(json);
f85f8ebb 103 if (error) {
f85f8ebb
BP
104 return ovsdb_wrap_error(error,
105 "failed to parse \"%s\" as ovsdb schema",
106 file_name);
107 }
108
109 *schemap = schema;
110 return NULL;
111}
112
cab50449 113static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
42a49b96 114ovsdb_schema_check_ref_table(struct ovsdb_column *column,
0d0f05b9
BP
115 const struct shash *tables,
116 const struct ovsdb_base_type *base,
117 const char *base_name)
118{
42a49b96
BP
119 struct ovsdb_table_schema *refTable;
120
fa37affa 121 if (base->type != OVSDB_TYPE_UUID || !base->uuid.refTableName) {
42a49b96
BP
122 return NULL;
123 }
124
fa37affa 125 refTable = shash_find_data(tables, base->uuid.refTableName);
42a49b96 126 if (!refTable) {
0d0f05b9
BP
127 return ovsdb_syntax_error(NULL, NULL,
128 "column %s %s refers to undefined table %s",
129 column->name, base_name,
fa37affa 130 base->uuid.refTableName);
0d0f05b9 131 }
42a49b96
BP
132
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;
141 }
142
143 return NULL;
0d0f05b9
BP
144}
145
1b1d2e6d
BP
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
149 * false. */
150bool
151ovsdb_parse_version(const char *s, struct ovsdb_version *version)
8159b984
BP
152{
153 int n = -1;
1b1d2e6d
BP
154 return (ovs_scan(s, "%u.%u.%u%n", &version->x, &version->y, &version->z,
155 &n)
156 && n != -1 && s[n] == '\0');
157}
158
159/* Returns true if 's' is a version string in the format "<x>.<y>.<z>",
160 * otherwie false. */
161bool
162ovsdb_is_valid_version(const char *s)
163{
164 struct ovsdb_version version;
165 return ovsdb_parse_version(s, &version);
8159b984
BP
166}
167
c5f341ab
BP
168/* Returns the number of tables in 'schema''s root set. */
169static size_t
170root_set_size(const struct ovsdb_schema *schema)
171{
172 struct shash_node *node;
85dcedff 173 size_t n_root = 0;
c5f341ab
BP
174
175 SHASH_FOR_EACH (node, &schema->tables) {
176 struct ovsdb_table_schema *table = node->data;
177
178 n_root += table->is_root;
179 }
180 return n_root;
181}
182
f85f8ebb 183struct ovsdb_error *
53178986 184ovsdb_schema_from_json(const struct json *json, struct ovsdb_schema **schemap)
f85f8ebb
BP
185{
186 struct ovsdb_schema *schema;
6aa09313 187 const struct json *name, *tables, *version_json, *cksum;
f85f8ebb
BP
188 struct ovsdb_error *error;
189 struct shash_node *node;
190 struct ovsdb_parser parser;
8159b984 191 const char *version;
f85f8ebb
BP
192
193 *schemap = NULL;
194
195 ovsdb_parser_init(&parser, json, "database schema");
196 name = ovsdb_parser_member(&parser, "name", OP_ID);
8159b984
BP
197 version_json = ovsdb_parser_member(&parser, "version",
198 OP_STRING | OP_OPTIONAL);
6aa09313 199 cksum = ovsdb_parser_member(&parser, "cksum", OP_STRING | OP_OPTIONAL);
f85f8ebb
BP
200 tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
201 error = ovsdb_parser_finish(&parser);
202 if (error) {
203 return error;
204 }
205
8159b984
BP
206 if (version_json) {
207 version = json_string(version_json);
1b1d2e6d 208 if (!ovsdb_is_valid_version(version)) {
8159b984
BP
209 return ovsdb_syntax_error(json, NULL, "schema version \"%s\" not "
210 "in format x.y.z", version);
211 }
212 } else {
213 /* Backward compatibility with old databases. */
214 version = "";
215 }
216
6aa09313
BP
217 schema = ovsdb_schema_create(json_string(name), version,
218 cksum ? json_string(cksum) : "");
f85f8ebb
BP
219 SHASH_FOR_EACH (node, json_object(tables)) {
220 struct ovsdb_table_schema *table;
221
222 if (node->name[0] == '_') {
223 error = ovsdb_syntax_error(json, NULL, "names beginning with "
224 "\"_\" are reserved");
b966380b
BP
225 } else if (!ovsdb_parser_is_id(node->name)) {
226 error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
f85f8ebb
BP
227 } else {
228 error = ovsdb_table_schema_from_json(node->data, node->name,
229 &table);
230 }
231 if (error) {
232 ovsdb_schema_destroy(schema);
233 return error;
234 }
235
236 shash_add(&schema->tables, table->name, table);
237 }
0d0f05b9 238
42a49b96
BP
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;
246
247 table->is_root = true;
248 }
249 }
250
251 /* Validate that all refTables refer to the names of tables that exist.
252 *
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. */
0d0f05b9
BP
256 SHASH_FOR_EACH (node, &schema->tables) {
257 struct ovsdb_table_schema *table = node->data;
258 struct shash_node *node2;
259
260 SHASH_FOR_EACH (node2, &table->columns) {
261 struct ovsdb_column *column = node2->data;
262
263 error = ovsdb_schema_check_ref_table(column, &schema->tables,
264 &column->type.key, "key");
265 if (!error) {
266 error = ovsdb_schema_check_ref_table(column, &schema->tables,
267 &column->type.value,
268 "value");
269 }
270 if (error) {
271 ovsdb_schema_destroy(schema);
272 return error;
273 }
274 }
275 }
276
f85f8ebb 277 *schemap = schema;
e3c17733 278 return NULL;
f85f8ebb
BP
279}
280
281struct json *
282ovsdb_schema_to_json(const struct ovsdb_schema *schema)
283{
284 struct json *json, *tables;
285 struct shash_node *node;
c5f341ab 286 bool default_is_root;
f85f8ebb
BP
287
288 json = json_object_create();
289 json_object_put_string(json, "name", schema->name);
8159b984
BP
290 if (schema->version[0]) {
291 json_object_put_string(json, "version", schema->version);
292 }
6aa09313
BP
293 if (schema->cksum[0]) {
294 json_object_put_string(json, "cksum", schema->cksum);
295 }
f85f8ebb 296
c5f341ab
BP
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);
302
f85f8ebb
BP
303 tables = json_object_create();
304
305 SHASH_FOR_EACH (node, &schema->tables) {
306 struct ovsdb_table_schema *table = node->data;
307 json_object_put(tables, table->name,
c5f341ab 308 ovsdb_table_schema_to_json(table, default_is_root));
f85f8ebb
BP
309 }
310 json_object_put(json, "tables", tables);
311
312 return json;
313}
403e3a25
BP
314
315/* Returns true if 'a' and 'b' specify equivalent schemas, false if they
316 * differ. */
317bool
318ovsdb_schema_equal(const struct ovsdb_schema *a,
319 const struct ovsdb_schema *b)
320{
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);
326 json_destroy(ja);
327 json_destroy(jb);
328
329 return equals;
330}
1b1d2e6d
BP
331
332struct ovsdb_error * OVS_WARN_UNUSED_RESULT
333ovsdb_schema_check_for_ephemeral_columns(const struct ovsdb_schema *schema)
334{
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;
339
340 SHASH_FOR_EACH (node2, &table->columns) {
341 struct ovsdb_column *column = node2->data;
342
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);
348 }
349 }
350 }
351 return NULL;
352}
353
354void
355ovsdb_schema_persist_ephemeral_columns(struct ovsdb_schema *schema,
356 const char *filename)
357{
358 int n = 0;
359 const char *example_table = NULL;
360 const char *example_column = NULL;
361
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;
366
367 SHASH_FOR_EACH (node2, &table->columns) {
368 struct ovsdb_column *column = node2->data;
369
370 if (column->index >= OVSDB_N_STD_COLUMNS && !column->persistent) {
371 column->persistent = true;
372 example_table = table->name;
373 example_column = column->name;
374 n++;
375 }
376 }
377 }
378
379 if (n) {
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);
384 }
385}
f85f8ebb 386\f
0d0f05b9
BP
387static void
388ovsdb_set_ref_table(const struct shash *tables,
389 struct ovsdb_base_type *base)
390{
fa37affa 391 if (base->type == OVSDB_TYPE_UUID && base->uuid.refTableName) {
0d0f05b9
BP
392 struct ovsdb_table *table;
393
fa37affa
BP
394 table = shash_find_data(tables, base->uuid.refTableName);
395 base->uuid.refTable = table;
0d0f05b9
BP
396 }
397}
398
1b1d2e6d
BP
399/* Creates and returns a new ovsdb based on 'schema' and 'storage' and takes
400 * ownership of both.
401 *
402 * At least one of the arguments must be nonnull. */
f85f8ebb 403struct ovsdb *
1b1d2e6d 404ovsdb_create(struct ovsdb_schema *schema, struct ovsdb_storage *storage)
f85f8ebb
BP
405{
406 struct shash_node *node;
407 struct ovsdb *db;
408
1b1d2e6d
BP
409 db = xzalloc(sizeof *db);
410 db->name = xstrdup(schema
411 ? schema->name
412 : ovsdb_storage_get_name(storage));
f85f8ebb 413 db->schema = schema;
1b1d2e6d 414 db->storage = storage;
009bf21f 415 ovs_list_init(&db->monitors);
417e7e66 416 ovs_list_init(&db->triggers);
bb66a0a6 417 db->run_triggers_now = db->run_triggers = false;
f85f8ebb
BP
418
419 shash_init(&db->tables);
1b1d2e6d
BP
420 if (schema) {
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));
424 }
f85f8ebb 425
1b1d2e6d
BP
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;
0d0f05b9 430
1b1d2e6d
BP
431 SHASH_FOR_EACH (node2, &table->columns) {
432 struct ovsdb_column *column = node2->data;
0d0f05b9 433
1b1d2e6d
BP
434 ovsdb_set_ref_table(&db->tables, &column->type.key);
435 ovsdb_set_ref_table(&db->tables, &column->type.value);
436 }
0d0f05b9
BP
437 }
438 }
439
d6db7b3c
LR
440 /* Use RBAC roles table if present. */
441 db->rbac_role = ovsdb_get_table(db, "RBAC_Role");
442
f85f8ebb
BP
443 return db;
444}
445
f85f8ebb
BP
446void
447ovsdb_destroy(struct ovsdb *db)
448{
449 if (db) {
450 struct shash_node *node;
451
120fb2ca 452 /* Close the log. */
1b1d2e6d 453 ovsdb_storage_close(db->storage);
120fb2ca 454
009bf21f
BP
455 /* Remove all the monitors. */
456 ovsdb_monitors_remove(db);
bd06962a 457
695e8150
HZ
458 /* Destroy txn history. */
459 ovsdb_txn_history_destroy(db);
460
1b1d2e6d
BP
461 /* The caller must ensure that no triggers remain. */
462 ovs_assert(ovs_list_is_empty(&db->triggers));
463
f85f8ebb
BP
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);
468 }
469 shash_destroy(&db->tables);
470
1248fcd2
BP
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. */
1b1d2e6d
BP
474 if (db->schema) {
475 shash_clear(&db->schema->tables);
476 ovsdb_schema_destroy(db->schema);
477 }
f85f8ebb 478
1b1d2e6d 479 free(db->name);
f85f8ebb
BP
480 free(db);
481 }
482}
483
0d085684
BP
484/* Adds some memory usage statistics for 'db' into 'usage', for use with
485 * memory_report(). */
486void
487ovsdb_get_memory_usage(const struct ovsdb *db, struct simap *usage)
488{
1b1d2e6d
BP
489 if (!db->schema) {
490 return;
491 }
492
0d085684
BP
493 const struct shash_node *node;
494 unsigned int cells = 0;
495
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);
500
501 cells += n_rows * n_columns;
502 }
503
504 simap_increase(usage, "cells", cells);
3423cd97
IM
505
506 if (db->storage) {
507 ovsdb_storage_get_memory_usage(db->storage, usage);
508 }
0d085684
BP
509}
510
f85f8ebb
BP
511struct ovsdb_table *
512ovsdb_get_table(const struct ovsdb *db, const char *name)
513{
514 return shash_find_data(&db->tables, name);
515}
1b1d2e6d
BP
516
517struct ovsdb_error * OVS_WARN_UNUSED_RESULT
518ovsdb_snapshot(struct ovsdb *db)
519{
520 if (!db->storage) {
521 return NULL;
522 }
523
524 struct json *schema = ovsdb_schema_to_json(db->schema);
525 struct json *data = ovsdb_to_txn_json(db, "compacting database online");
526 struct ovsdb_error *error = ovsdb_storage_store_snapshot(db->storage,
527 schema, data);
528 json_destroy(schema);
529 json_destroy(data);
530 return error;
531}
532
533void
534ovsdb_replace(struct ovsdb *dst, struct ovsdb *src)
535{
536 /* Cancel monitors. */
537 ovsdb_monitor_prereplace_db(dst);
538
539 /* Cancel triggers. */
540 struct ovsdb_trigger *trigger, *next;
541 LIST_FOR_EACH_SAFE (trigger, next, node, &dst->triggers) {
542 ovsdb_trigger_prereplace_db(trigger);
543 }
544
695e8150
HZ
545 /* Destroy txn history. */
546 ovsdb_txn_history_destroy(dst);
547
1b1d2e6d
BP
548 struct ovsdb_schema *tmp_schema = dst->schema;
549 dst->schema = src->schema;
550 src->schema = tmp_schema;
551
552 shash_swap(&dst->tables, &src->tables);
553
554 dst->rbac_role = ovsdb_get_table(dst, "RBAC_Role");
555
556 ovsdb_destroy(src);
557}