1 /* Copyright (c) 2009, 2010, 2011, 2012 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.
22 #include "condition.h"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
35 #include "transaction.h"
37 struct ovsdb_execution {
39 const struct ovsdb_session *session;
40 struct ovsdb_txn *txn;
41 struct ovsdb_symbol_table *symtab;
45 long long int elapsed_msec;
46 long long int timeout_msec;
49 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
50 struct ovsdb_parser *,
53 static ovsdb_operation_executor ovsdb_execute_insert;
54 static ovsdb_operation_executor ovsdb_execute_select;
55 static ovsdb_operation_executor ovsdb_execute_update;
56 static ovsdb_operation_executor ovsdb_execute_mutate;
57 static ovsdb_operation_executor ovsdb_execute_delete;
58 static ovsdb_operation_executor ovsdb_execute_wait;
59 static ovsdb_operation_executor ovsdb_execute_commit;
60 static ovsdb_operation_executor ovsdb_execute_abort;
61 static ovsdb_operation_executor ovsdb_execute_comment;
62 static ovsdb_operation_executor ovsdb_execute_assert;
64 static ovsdb_operation_executor *
65 lookup_executor(const char *name)
67 struct ovsdb_operation {
69 ovsdb_operation_executor *executor;
72 static const struct ovsdb_operation operations[] = {
73 { "insert", ovsdb_execute_insert },
74 { "select", ovsdb_execute_select },
75 { "update", ovsdb_execute_update },
76 { "mutate", ovsdb_execute_mutate },
77 { "delete", ovsdb_execute_delete },
78 { "wait", ovsdb_execute_wait },
79 { "commit", ovsdb_execute_commit },
80 { "abort", ovsdb_execute_abort },
81 { "comment", ovsdb_execute_comment },
82 { "assert", ovsdb_execute_assert },
87 for (i = 0; i < ARRAY_SIZE(operations); i++) {
88 const struct ovsdb_operation *c = &operations[i];
89 if (!strcmp(c->name, name)) {
97 ovsdb_execute(struct ovsdb *db, const struct ovsdb_session *session,
98 const struct json *params,
99 long long int elapsed_msec, long long int *timeout_msec)
101 struct ovsdb_execution x;
102 struct ovsdb_error *error;
103 struct json *results;
107 if (params->type != JSON_ARRAY
108 || !params->u.array.n
109 || params->u.array.elems[0]->type != JSON_STRING
110 || strcmp(params->u.array.elems[0]->u.string, db->schema->name)) {
111 if (params->type != JSON_ARRAY) {
112 error = ovsdb_syntax_error(params, NULL, "array expected");
114 error = ovsdb_syntax_error(params, NULL, "database name expected "
115 "as first parameter");
118 results = ovsdb_error_to_json(error);
119 ovsdb_error_destroy(error);
125 x.txn = ovsdb_txn_create(db);
126 x.symtab = ovsdb_symbol_table_create();
128 x.elapsed_msec = elapsed_msec;
129 x.timeout_msec = LLONG_MAX;
132 results = json_array_create_empty();
133 n_operations = params->u.array.n - 1;
135 for (i = 1; i <= n_operations; i++) {
136 struct json *operation = params->u.array.elems[i];
137 struct ovsdb_error *parse_error;
138 struct ovsdb_parser parser;
140 const struct json *op;
142 /* Parse and execute operation. */
143 ovsdb_parser_init(&parser, operation,
144 "ovsdb operation %zu of %zu", i, n_operations);
145 op = ovsdb_parser_member(&parser, "op", OP_ID);
146 result = json_object_create();
148 const char *op_name = json_string(op);
149 ovsdb_operation_executor *executor = lookup_executor(op_name);
151 error = executor(&x, &parser, result);
153 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
157 assert(ovsdb_parser_has_error(&parser));
160 /* A parse error overrides any other error.
161 * An error overrides any other result. */
162 parse_error = ovsdb_parser_finish(&parser);
164 ovsdb_error_destroy(error);
168 json_destroy(result);
169 result = ovsdb_error_to_json(error);
171 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
173 ovsdb_txn_abort(x.txn);
174 *timeout_msec = x.timeout_msec;
176 json_destroy(result);
177 json_destroy(results);
182 /* Add result to array. */
183 json_array_add(results, result);
190 error = ovsdb_txn_commit(x.txn, x.durable);
192 json_array_add(results, ovsdb_error_to_json(error));
195 ovsdb_txn_abort(x.txn);
198 while (json_array(results)->n < n_operations) {
199 json_array_add(results, json_null_create());
203 ovsdb_error_destroy(error);
204 ovsdb_symbol_table_destroy(x.symtab);
209 static struct ovsdb_error *
210 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
211 struct json *result OVS_UNUSED)
213 const struct json *durable;
215 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
216 if (durable && json_boolean(durable)) {
222 static struct ovsdb_error *
223 ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED,
224 struct ovsdb_parser *parser OVS_UNUSED,
225 struct json *result OVS_UNUSED)
227 return ovsdb_error("aborted", "aborted by request");
230 static struct ovsdb_table *
231 parse_table(struct ovsdb_execution *x,
232 struct ovsdb_parser *parser, const char *member)
234 struct ovsdb_table *table;
235 const char *table_name;
236 const struct json *json;
238 json = ovsdb_parser_member(parser, member, OP_ID);
242 table_name = json_string(json);
244 table = shash_find_data(&x->db->tables, table_name);
246 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
251 static WARN_UNUSED_RESULT struct ovsdb_error *
252 parse_row(const struct json *json, const struct ovsdb_table *table,
253 struct ovsdb_symbol_table *symtab,
254 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
256 struct ovsdb_error *error;
257 struct ovsdb_row *row;
262 return OVSDB_BUG("null table");
265 return OVSDB_BUG("null row");
268 row = ovsdb_row_create(table);
269 error = ovsdb_row_from_json(row, json, symtab, columns);
271 ovsdb_row_destroy(row);
279 static struct ovsdb_error *
280 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
283 struct ovsdb_table *table;
284 struct ovsdb_row *row = NULL;
285 const struct json *uuid_name, *row_json;
286 struct ovsdb_error *error;
287 struct uuid row_uuid;
289 table = parse_table(x, parser, "table");
290 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
291 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
292 error = ovsdb_parser_get_error(parser);
298 struct ovsdb_symbol *symbol;
300 symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
301 if (symbol->created) {
302 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
303 "This \"uuid-name\" appeared on an "
304 "earlier \"insert\" operation.");
306 row_uuid = symbol->uuid;
307 symbol->created = true;
309 uuid_generate(&row_uuid);
313 error = parse_row(row_json, table, x->symtab, &row, NULL);
316 /* Check constraints for columns not included in "row", in case the
317 * default values do not satisfy the constraints. We could check only
318 * the columns that have their default values by supplying an
319 * ovsdb_column_set to parse_row() above, but I suspect that this is
321 const struct shash_node *node;
323 SHASH_FOR_EACH (node, &table->schema->columns) {
324 const struct ovsdb_column *column = node->data;
325 const struct ovsdb_datum *datum = &row->fields[column->index];
327 /* If there are 0 keys or pairs, there's nothing to check.
328 * If there is 1, it might be a default value.
329 * If there are more, it can't be a default value, so the value has
330 * already been checked. */
332 error = ovsdb_datum_check_constraints(datum, &column->type);
334 ovsdb_row_destroy(row);
341 *ovsdb_row_get_uuid_rw(row) = row_uuid;
342 ovsdb_txn_row_insert(x->txn, row);
343 json_object_put(result, "uuid",
344 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
350 static struct ovsdb_error *
351 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
354 struct ovsdb_table *table;
355 const struct json *where, *columns_json, *sort_json;
356 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
357 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
358 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
359 struct ovsdb_error *error;
361 table = parse_table(x, parser, "table");
362 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
363 columns_json = ovsdb_parser_member(parser, "columns",
364 OP_ARRAY | OP_OPTIONAL);
365 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
367 error = ovsdb_parser_get_error(parser);
369 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
373 error = ovsdb_column_set_from_json(columns_json, table->schema,
377 error = ovsdb_column_set_from_json(sort_json, table->schema, &sort);
380 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
382 ovsdb_query_distinct(table, &condition, &columns, &rows);
383 ovsdb_row_set_sort(&rows, &sort);
384 json_object_put(result, "rows",
385 ovsdb_row_set_to_json(&rows, &columns));
387 ovsdb_row_set_destroy(&rows);
390 ovsdb_column_set_destroy(&columns);
391 ovsdb_column_set_destroy(&sort);
392 ovsdb_condition_destroy(&condition);
397 struct update_row_cbdata {
399 struct ovsdb_txn *txn;
400 const struct ovsdb_row *row;
401 const struct ovsdb_column_set *columns;
405 update_row_cb(const struct ovsdb_row *row, void *ur_)
407 struct update_row_cbdata *ur = ur_;
410 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
411 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
412 ur->row, ur->columns);
418 static struct ovsdb_error *
419 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
422 struct ovsdb_table *table;
423 const struct json *where, *row_json;
424 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
425 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
426 struct ovsdb_row *row = NULL;
427 struct update_row_cbdata ur;
428 struct ovsdb_error *error;
430 table = parse_table(x, parser, "table");
431 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
432 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
433 error = ovsdb_parser_get_error(parser);
435 error = parse_row(row_json, table, x->symtab, &row, &columns);
440 for (i = 0; i < columns.n_columns; i++) {
441 const struct ovsdb_column *column = columns.columns[i];
443 if (!column->mutable) {
444 error = ovsdb_syntax_error(parser->json,
445 "constraint violation",
446 "Cannot update immutable column %s "
448 column->name, table->schema->name);
454 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
461 ur.columns = &columns;
462 ovsdb_query(table, &condition, update_row_cb, &ur);
463 json_object_put(result, "count", json_integer_create(ur.n_matches));
466 ovsdb_row_destroy(row);
467 ovsdb_column_set_destroy(&columns);
468 ovsdb_condition_destroy(&condition);
473 struct mutate_row_cbdata {
475 struct ovsdb_txn *txn;
476 const struct ovsdb_mutation_set *mutations;
477 struct ovsdb_error **error;
481 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
483 struct mutate_row_cbdata *mr = mr_;
486 *mr->error = ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
488 return *mr->error == NULL;
491 static struct ovsdb_error *
492 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
495 struct ovsdb_table *table;
496 const struct json *where;
497 const struct json *mutations_json;
498 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
499 struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
500 struct ovsdb_row *row = NULL;
501 struct mutate_row_cbdata mr;
502 struct ovsdb_error *error;
504 table = parse_table(x, parser, "table");
505 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
506 mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
507 error = ovsdb_parser_get_error(parser);
509 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
510 x->symtab, &mutations);
513 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
519 mr.mutations = &mutations;
521 ovsdb_query(table, &condition, mutate_row_cb, &mr);
522 json_object_put(result, "count", json_integer_create(mr.n_matches));
525 ovsdb_row_destroy(row);
526 ovsdb_mutation_set_destroy(&mutations);
527 ovsdb_condition_destroy(&condition);
532 struct delete_row_cbdata {
534 const struct ovsdb_table *table;
535 struct ovsdb_txn *txn;
539 delete_row_cb(const struct ovsdb_row *row, void *dr_)
541 struct delete_row_cbdata *dr = dr_;
544 ovsdb_txn_row_delete(dr->txn, row);
549 static struct ovsdb_error *
550 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
553 struct ovsdb_table *table;
554 const struct json *where;
555 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
556 struct ovsdb_error *error;
558 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
559 table = parse_table(x, parser, "table");
560 error = ovsdb_parser_get_error(parser);
562 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
566 struct delete_row_cbdata dr;
571 ovsdb_query(table, &condition, delete_row_cb, &dr);
573 json_object_put(result, "count", json_integer_create(dr.n_matches));
576 ovsdb_condition_destroy(&condition);
581 struct wait_auxdata {
582 struct ovsdb_row_hash *actual;
583 struct ovsdb_row_hash *expected;
588 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
590 struct wait_auxdata *aux = aux_;
592 if (ovsdb_row_hash_contains(aux->expected, row)) {
593 ovsdb_row_hash_insert(aux->actual, row);
596 /* The query row isn't in the expected result set, so the actual and
597 * expected results sets definitely differ and we can short-circuit the
598 * rest of the query. */
604 static struct ovsdb_error *
605 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
606 struct json *result OVS_UNUSED)
608 struct ovsdb_table *table;
609 const struct json *timeout, *where, *columns_json, *until, *rows;
610 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
611 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
612 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
613 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
614 struct ovsdb_error *error;
615 struct wait_auxdata aux;
616 long long int timeout_msec = 0;
619 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
620 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
621 columns_json = ovsdb_parser_member(parser, "columns",
622 OP_ARRAY | OP_OPTIONAL);
623 until = ovsdb_parser_member(parser, "until", OP_STRING);
624 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
625 table = parse_table(x, parser, "table");
626 error = ovsdb_parser_get_error(parser);
628 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
632 error = ovsdb_column_set_from_json(columns_json, table->schema,
637 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
638 if (timeout_msec < 0) {
639 error = ovsdb_syntax_error(timeout, NULL,
640 "timeout must be nonnegative");
641 } else if (timeout_msec < x->timeout_msec) {
642 x->timeout_msec = timeout_msec;
645 timeout_msec = LLONG_MAX;
647 if (strcmp(json_string(until), "==")
648 && strcmp(json_string(until), "!=")) {
649 error = ovsdb_syntax_error(until, NULL,
650 "\"until\" must be \"==\" or \"!=\"");
654 /* Parse "rows" into 'expected'. */
655 ovsdb_row_hash_init(&expected, &columns);
656 for (i = 0; i < rows->u.array.n; i++) {
657 struct ovsdb_row *row;
659 row = ovsdb_row_create(table);
660 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
666 if (!ovsdb_row_hash_insert(&expected, row)) {
667 /* XXX Perhaps we should abort with an error or log a
669 ovsdb_row_destroy(row);
676 ovsdb_row_hash_init(&actual, &columns);
677 aux.actual = &actual;
678 aux.expected = &expected;
680 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
682 /* We know that every row in 'actual' is also in 'expected'. We
683 * also know that all of the rows in 'actual' are distinct and that
684 * all of the rows in 'expected' are distinct. Therefore, if
685 * 'actual' and 'expected' have the same number of rows, then they
686 * have the same content. */
687 size_t n_actual = ovsdb_row_hash_count(&actual);
688 size_t n_expected = ovsdb_row_hash_count(&expected);
689 equal = n_actual == n_expected;
691 if (!strcmp(json_string(until), "==") != equal) {
692 if (timeout && x->elapsed_msec >= timeout_msec) {
693 if (x->elapsed_msec) {
694 error = ovsdb_error("timed out",
695 "\"wait\" timed out after %lld ms",
698 error = ovsdb_error("timed out", "\"wait\" timed out");
701 /* ovsdb_execute() will change this, if triggers really are
703 error = ovsdb_error("not supported", "triggers not supported");
709 ovsdb_row_hash_destroy(&expected, true);
710 ovsdb_row_hash_destroy(&actual, false);
711 ovsdb_column_set_destroy(&columns);
712 ovsdb_condition_destroy(&condition);
717 static struct ovsdb_error *
718 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
719 struct json *result OVS_UNUSED)
721 const struct json *comment;
723 comment = ovsdb_parser_member(parser, "comment", OP_STRING);
727 ovsdb_txn_add_comment(x->txn, json_string(comment));
732 static struct ovsdb_error *
733 ovsdb_execute_assert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
734 struct json *result OVS_UNUSED)
736 const struct json *lock_name;
738 lock_name = ovsdb_parser_member(parser, "lock", OP_ID);
744 const struct ovsdb_lock_waiter *waiter;
746 waiter = ovsdb_session_get_lock_waiter(x->session,
747 json_string(lock_name));
748 if (waiter && ovsdb_lock_waiter_is_owner(waiter)) {
753 return ovsdb_error("not owner", "Asserted lock %s not held.",
754 json_string(lock_name));