]> git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/ovsdb.c
stream: Allow timeout configuration for open_block.
[mirror_ovs.git] / ovsdb / ovsdb.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2017 Nicira, Inc.
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
20 #include "column.h"
21 #include "file.h"
22 #include "monitor.h"
23 #include "openvswitch/json.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
26 #include "ovsdb-types.h"
27 #include "simap.h"
28 #include "storage.h"
29 #include "table.h"
30 #include "transaction.h"
31 #include "trigger.h"
32
33 #include "openvswitch/vlog.h"
34 VLOG_DEFINE_THIS_MODULE(ovsdb);
35
36 struct ovsdb_schema *
37 ovsdb_schema_create(const char *name, const char *version, const char *cksum)
38 {
39 struct ovsdb_schema *schema;
40
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);
46
47 return schema;
48 }
49
50 struct ovsdb_schema *
51 ovsdb_schema_clone(const struct ovsdb_schema *old)
52 {
53 struct ovsdb_schema *new;
54 struct shash_node *node;
55
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;
59
60 shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
61 }
62 return new;
63 }
64
65 void
66 ovsdb_schema_destroy(struct ovsdb_schema *schema)
67 {
68 struct shash_node *node;
69
70 if (!schema) {
71 return;
72 }
73
74 SHASH_FOR_EACH (node, &schema->tables) {
75 ovsdb_table_schema_destroy(node->data);
76 }
77 shash_destroy(&schema->tables);
78 free(schema->name);
79 free(schema->version);
80 free(schema->cksum);
81 free(schema);
82 }
83
84 struct ovsdb_error *
85 ovsdb_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);
102 json_destroy(json);
103 if (error) {
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
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)
118 {
119 struct ovsdb_table_schema *refTable;
120
121 if (base->type != OVSDB_TYPE_UUID || !base->uuid.refTableName) {
122 return NULL;
123 }
124
125 refTable = shash_find_data(tables, base->uuid.refTableName);
126 if (!refTable) {
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);
131 }
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;
144 }
145
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. */
150 bool
151 ovsdb_parse_version(const char *s, struct ovsdb_version *version)
152 {
153 int n = -1;
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. */
161 bool
162 ovsdb_is_valid_version(const char *s)
163 {
164 struct ovsdb_version version;
165 return ovsdb_parse_version(s, &version);
166 }
167
168 /* Returns the number of tables in 'schema''s root set. */
169 static size_t
170 root_set_size(const struct ovsdb_schema *schema)
171 {
172 struct shash_node *node;
173 size_t n_root = 0;
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
183 struct ovsdb_error *
184 ovsdb_schema_from_json(const struct json *json, struct ovsdb_schema **schemap)
185 {
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;
191 const char *version;
192
193 *schemap = NULL;
194
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);
202 if (error) {
203 return error;
204 }
205
206 if (version_json) {
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);
211 }
212 } else {
213 /* Backward compatibility with old databases. */
214 version = "";
215 }
216
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;
221
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");
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 }
238
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. */
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
277 *schemap = schema;
278 return NULL;
279 }
280
281 struct json *
282 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
283 {
284 struct json *json, *tables;
285 struct shash_node *node;
286 bool default_is_root;
287
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);
292 }
293 if (schema->cksum[0]) {
294 json_object_put_string(json, "cksum", schema->cksum);
295 }
296
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
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,
308 ovsdb_table_schema_to_json(table, default_is_root));
309 }
310 json_object_put(json, "tables", tables);
311
312 return json;
313 }
314
315 /* Returns true if 'a' and 'b' specify equivalent schemas, false if they
316 * differ. */
317 bool
318 ovsdb_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 }
331
332 struct ovsdb_error * OVS_WARN_UNUSED_RESULT
333 ovsdb_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
354 void
355 ovsdb_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 }
386 \f
387 static void
388 ovsdb_set_ref_table(const struct shash *tables,
389 struct ovsdb_base_type *base)
390 {
391 if (base->type == OVSDB_TYPE_UUID && base->uuid.refTableName) {
392 struct ovsdb_table *table;
393
394 table = shash_find_data(tables, base->uuid.refTableName);
395 base->uuid.refTable = table;
396 }
397 }
398
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. */
403 struct ovsdb *
404 ovsdb_create(struct ovsdb_schema *schema, struct ovsdb_storage *storage)
405 {
406 struct shash_node *node;
407 struct ovsdb *db;
408
409 db = xzalloc(sizeof *db);
410 db->name = xstrdup(schema
411 ? schema->name
412 : ovsdb_storage_get_name(storage));
413 db->schema = schema;
414 db->storage = storage;
415 ovs_list_init(&db->monitors);
416 ovs_list_init(&db->triggers);
417 db->run_triggers = false;
418
419 shash_init(&db->tables);
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 }
425
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;
430
431 SHASH_FOR_EACH (node2, &table->columns) {
432 struct ovsdb_column *column = node2->data;
433
434 ovsdb_set_ref_table(&db->tables, &column->type.key);
435 ovsdb_set_ref_table(&db->tables, &column->type.value);
436 }
437 }
438 }
439
440 /* Use RBAC roles table if present. */
441 db->rbac_role = ovsdb_get_table(db, "RBAC_Role");
442
443 return db;
444 }
445
446 void
447 ovsdb_destroy(struct ovsdb *db)
448 {
449 if (db) {
450 struct shash_node *node;
451
452 /* Close the log. */
453 ovsdb_storage_close(db->storage);
454
455 /* Remove all the monitors. */
456 ovsdb_monitors_remove(db);
457
458 /* The caller must ensure that no triggers remain. */
459 ovs_assert(ovs_list_is_empty(&db->triggers));
460
461 /* Delete all the tables. This also deletes their schemas. */
462 SHASH_FOR_EACH (node, &db->tables) {
463 struct ovsdb_table *table = node->data;
464 ovsdb_table_destroy(table);
465 }
466 shash_destroy(&db->tables);
467
468 /* The schemas, but not the table that points to them, were deleted in
469 * the previous step, so we need to clear out the table. We can't
470 * destroy the table, because ovsdb_schema_destroy() will do that. */
471 if (db->schema) {
472 shash_clear(&db->schema->tables);
473 ovsdb_schema_destroy(db->schema);
474 }
475
476 free(db->name);
477 free(db);
478 }
479 }
480
481 /* Adds some memory usage statistics for 'db' into 'usage', for use with
482 * memory_report(). */
483 void
484 ovsdb_get_memory_usage(const struct ovsdb *db, struct simap *usage)
485 {
486 if (!db->schema) {
487 return;
488 }
489
490 const struct shash_node *node;
491 unsigned int cells = 0;
492
493 SHASH_FOR_EACH (node, &db->tables) {
494 const struct ovsdb_table *table = node->data;
495 unsigned int n_columns = shash_count(&table->schema->columns);
496 unsigned int n_rows = hmap_count(&table->rows);
497
498 cells += n_rows * n_columns;
499 }
500
501 simap_increase(usage, "cells", cells);
502 }
503
504 struct ovsdb_table *
505 ovsdb_get_table(const struct ovsdb *db, const char *name)
506 {
507 return shash_find_data(&db->tables, name);
508 }
509
510 struct ovsdb_error * OVS_WARN_UNUSED_RESULT
511 ovsdb_snapshot(struct ovsdb *db)
512 {
513 if (!db->storage) {
514 return NULL;
515 }
516
517 struct json *schema = ovsdb_schema_to_json(db->schema);
518 struct json *data = ovsdb_to_txn_json(db, "compacting database online");
519 struct ovsdb_error *error = ovsdb_storage_store_snapshot(db->storage,
520 schema, data);
521 json_destroy(schema);
522 json_destroy(data);
523 return error;
524 }
525
526 void
527 ovsdb_replace(struct ovsdb *dst, struct ovsdb *src)
528 {
529 /* Cancel monitors. */
530 ovsdb_monitor_prereplace_db(dst);
531
532 /* Cancel triggers. */
533 struct ovsdb_trigger *trigger, *next;
534 LIST_FOR_EACH_SAFE (trigger, next, node, &dst->triggers) {
535 ovsdb_trigger_prereplace_db(trigger);
536 }
537
538 struct ovsdb_schema *tmp_schema = dst->schema;
539 dst->schema = src->schema;
540 src->schema = tmp_schema;
541
542 shash_swap(&dst->tables, &src->tables);
543
544 dst->rbac_role = ovsdb_get_table(dst, "RBAC_Role");
545
546 ovsdb_destroy(src);
547 }