1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 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.
21 #include "condition.h"
25 #include "ovsdb-data.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
34 #include "transaction.h"
36 struct ovsdb_execution
{
38 const struct ovsdb_session
*session
;
39 struct ovsdb_txn
*txn
;
40 struct ovsdb_symbol_table
*symtab
;
44 long long int elapsed_msec
;
45 long long int timeout_msec
;
48 typedef struct ovsdb_error
*ovsdb_operation_executor(struct ovsdb_execution
*,
49 struct ovsdb_parser
*,
52 static ovsdb_operation_executor ovsdb_execute_insert
;
53 static ovsdb_operation_executor ovsdb_execute_select
;
54 static ovsdb_operation_executor ovsdb_execute_update
;
55 static ovsdb_operation_executor ovsdb_execute_mutate
;
56 static ovsdb_operation_executor ovsdb_execute_delete
;
57 static ovsdb_operation_executor ovsdb_execute_wait
;
58 static ovsdb_operation_executor ovsdb_execute_commit
;
59 static ovsdb_operation_executor ovsdb_execute_abort
;
60 static ovsdb_operation_executor ovsdb_execute_comment
;
61 static ovsdb_operation_executor ovsdb_execute_assert
;
63 static ovsdb_operation_executor
*
64 lookup_executor(const char *name
)
66 struct ovsdb_operation
{
68 ovsdb_operation_executor
*executor
;
71 static const struct ovsdb_operation operations
[] = {
72 { "insert", ovsdb_execute_insert
},
73 { "select", ovsdb_execute_select
},
74 { "update", ovsdb_execute_update
},
75 { "mutate", ovsdb_execute_mutate
},
76 { "delete", ovsdb_execute_delete
},
77 { "wait", ovsdb_execute_wait
},
78 { "commit", ovsdb_execute_commit
},
79 { "abort", ovsdb_execute_abort
},
80 { "comment", ovsdb_execute_comment
},
81 { "assert", ovsdb_execute_assert
},
86 for (i
= 0; i
< ARRAY_SIZE(operations
); i
++) {
87 const struct ovsdb_operation
*c
= &operations
[i
];
88 if (!strcmp(c
->name
, name
)) {
96 ovsdb_execute(struct ovsdb
*db
, const struct ovsdb_session
*session
,
97 const struct json
*params
,
98 long long int elapsed_msec
, long long int *timeout_msec
)
100 struct ovsdb_execution x
;
101 struct ovsdb_error
*error
;
102 struct json
*results
;
106 if (params
->type
!= JSON_ARRAY
107 || !params
->u
.array
.n
108 || params
->u
.array
.elems
[0]->type
!= JSON_STRING
109 || strcmp(params
->u
.array
.elems
[0]->u
.string
, db
->schema
->name
)) {
110 if (params
->type
!= JSON_ARRAY
) {
111 error
= ovsdb_syntax_error(params
, NULL
, "array expected");
113 error
= ovsdb_syntax_error(params
, NULL
, "database name expected "
114 "as first parameter");
117 results
= ovsdb_error_to_json(error
);
118 ovsdb_error_destroy(error
);
124 x
.txn
= ovsdb_txn_create(db
);
125 x
.symtab
= ovsdb_symbol_table_create();
127 x
.elapsed_msec
= elapsed_msec
;
128 x
.timeout_msec
= LLONG_MAX
;
131 results
= json_array_create_empty();
132 n_operations
= params
->u
.array
.n
- 1;
134 for (i
= 1; i
<= n_operations
; i
++) {
135 struct json
*operation
= params
->u
.array
.elems
[i
];
136 struct ovsdb_error
*parse_error
;
137 struct ovsdb_parser parser
;
139 const struct json
*op
;
141 /* Parse and execute operation. */
142 ovsdb_parser_init(&parser
, operation
,
143 "ovsdb operation %"PRIuSIZE
" of %"PRIuSIZE
, i
, n_operations
);
144 op
= ovsdb_parser_member(&parser
, "op", OP_ID
);
145 result
= json_object_create();
147 const char *op_name
= json_string(op
);
148 ovsdb_operation_executor
*executor
= lookup_executor(op_name
);
150 error
= executor(&x
, &parser
, result
);
152 ovsdb_parser_raise_error(&parser
, "No operation \"%s\"",
156 ovs_assert(ovsdb_parser_has_error(&parser
));
159 /* A parse error overrides any other error.
160 * An error overrides any other result. */
161 parse_error
= ovsdb_parser_finish(&parser
);
163 ovsdb_error_destroy(error
);
167 json_destroy(result
);
168 result
= ovsdb_error_to_json(error
);
170 if (error
&& !strcmp(ovsdb_error_get_tag(error
), "not supported")
172 ovsdb_txn_abort(x
.txn
);
173 *timeout_msec
= x
.timeout_msec
;
175 json_destroy(result
);
176 json_destroy(results
);
181 /* Add result to array. */
182 json_array_add(results
, result
);
189 error
= ovsdb_txn_commit(x
.txn
, x
.durable
);
191 json_array_add(results
, ovsdb_error_to_json(error
));
194 ovsdb_txn_abort(x
.txn
);
197 while (json_array(results
)->n
< n_operations
) {
198 json_array_add(results
, json_null_create());
202 ovsdb_error_destroy(error
);
203 ovsdb_symbol_table_destroy(x
.symtab
);
208 static struct ovsdb_error
*
209 ovsdb_execute_commit(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
210 struct json
*result OVS_UNUSED
)
212 const struct json
*durable
;
214 durable
= ovsdb_parser_member(parser
, "durable", OP_BOOLEAN
);
215 if (durable
&& json_boolean(durable
)) {
221 static struct ovsdb_error
*
222 ovsdb_execute_abort(struct ovsdb_execution
*x OVS_UNUSED
,
223 struct ovsdb_parser
*parser OVS_UNUSED
,
224 struct json
*result OVS_UNUSED
)
226 return ovsdb_error("aborted", "aborted by request");
229 static struct ovsdb_table
*
230 parse_table(struct ovsdb_execution
*x
,
231 struct ovsdb_parser
*parser
, const char *member
)
233 struct ovsdb_table
*table
;
234 const char *table_name
;
235 const struct json
*json
;
237 json
= ovsdb_parser_member(parser
, member
, OP_ID
);
241 table_name
= json_string(json
);
243 table
= shash_find_data(&x
->db
->tables
, table_name
);
245 ovsdb_parser_raise_error(parser
, "No table named %s.", table_name
);
250 static OVS_WARN_UNUSED_RESULT
struct ovsdb_error
*
251 parse_row(const struct json
*json
, const struct ovsdb_table
*table
,
252 struct ovsdb_symbol_table
*symtab
,
253 struct ovsdb_row
**rowp
, struct ovsdb_column_set
*columns
)
255 struct ovsdb_error
*error
;
256 struct ovsdb_row
*row
;
261 return OVSDB_BUG("null table");
264 return OVSDB_BUG("null row");
267 row
= ovsdb_row_create(table
);
268 error
= ovsdb_row_from_json(row
, json
, symtab
, columns
);
270 ovsdb_row_destroy(row
);
278 static struct ovsdb_error
*
279 ovsdb_execute_insert(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
282 struct ovsdb_table
*table
;
283 struct ovsdb_row
*row
= NULL
;
284 const struct json
*uuid_name
, *row_json
;
285 struct ovsdb_error
*error
;
286 struct uuid row_uuid
;
288 table
= parse_table(x
, parser
, "table");
289 uuid_name
= ovsdb_parser_member(parser
, "uuid-name", OP_ID
| OP_OPTIONAL
);
290 row_json
= ovsdb_parser_member(parser
, "row", OP_OBJECT
);
291 error
= ovsdb_parser_get_error(parser
);
297 struct ovsdb_symbol
*symbol
;
299 symbol
= ovsdb_symbol_table_insert(x
->symtab
, json_string(uuid_name
));
300 if (symbol
->created
) {
301 return ovsdb_syntax_error(uuid_name
, "duplicate uuid-name",
302 "This \"uuid-name\" appeared on an "
303 "earlier \"insert\" operation.");
305 row_uuid
= symbol
->uuid
;
306 symbol
->created
= true;
308 uuid_generate(&row_uuid
);
312 error
= parse_row(row_json
, table
, x
->symtab
, &row
, NULL
);
315 /* Check constraints for columns not included in "row", in case the
316 * default values do not satisfy the constraints. We could check only
317 * the columns that have their default values by supplying an
318 * ovsdb_column_set to parse_row() above, but I suspect that this is
320 const struct shash_node
*node
;
322 SHASH_FOR_EACH (node
, &table
->schema
->columns
) {
323 const struct ovsdb_column
*column
= node
->data
;
324 const struct ovsdb_datum
*datum
= &row
->fields
[column
->index
];
326 /* If there are 0 keys or pairs, there's nothing to check.
327 * If there is 1, it might be a default value.
328 * If there are more, it can't be a default value, so the value has
329 * already been checked. */
331 error
= ovsdb_datum_check_constraints(datum
, &column
->type
);
333 ovsdb_row_destroy(row
);
340 *ovsdb_row_get_uuid_rw(row
) = row_uuid
;
341 ovsdb_txn_row_insert(x
->txn
, row
);
342 json_object_put(result
, "uuid",
343 ovsdb_datum_to_json(&row
->fields
[OVSDB_COL_UUID
],
349 static struct ovsdb_error
*
350 ovsdb_execute_select(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
353 struct ovsdb_table
*table
;
354 const struct json
*where
, *columns_json
, *sort_json
;
355 struct ovsdb_condition condition
= OVSDB_CONDITION_INITIALIZER
;
356 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
357 struct ovsdb_column_set sort
= OVSDB_COLUMN_SET_INITIALIZER
;
358 struct ovsdb_error
*error
;
360 table
= parse_table(x
, parser
, "table");
361 where
= ovsdb_parser_member(parser
, "where", OP_ARRAY
);
362 columns_json
= ovsdb_parser_member(parser
, "columns",
363 OP_ARRAY
| OP_OPTIONAL
);
364 sort_json
= ovsdb_parser_member(parser
, "sort", OP_ARRAY
| OP_OPTIONAL
);
366 error
= ovsdb_parser_get_error(parser
);
368 error
= ovsdb_condition_from_json(table
->schema
, where
, x
->symtab
,
372 error
= ovsdb_column_set_from_json(columns_json
, table
->schema
,
376 error
= ovsdb_column_set_from_json(sort_json
, table
->schema
, &sort
);
379 struct ovsdb_row_set rows
= OVSDB_ROW_SET_INITIALIZER
;
381 ovsdb_query_distinct(table
, &condition
, &columns
, &rows
);
382 ovsdb_row_set_sort(&rows
, &sort
);
383 json_object_put(result
, "rows",
384 ovsdb_row_set_to_json(&rows
, &columns
));
386 ovsdb_row_set_destroy(&rows
);
389 ovsdb_column_set_destroy(&columns
);
390 ovsdb_column_set_destroy(&sort
);
391 ovsdb_condition_destroy(&condition
);
396 struct update_row_cbdata
{
398 struct ovsdb_txn
*txn
;
399 const struct ovsdb_row
*row
;
400 const struct ovsdb_column_set
*columns
;
404 update_row_cb(const struct ovsdb_row
*row
, void *ur_
)
406 struct update_row_cbdata
*ur
= ur_
;
409 if (!ovsdb_row_equal_columns(row
, ur
->row
, ur
->columns
)) {
410 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur
->txn
, row
),
411 ur
->row
, ur
->columns
);
417 static struct ovsdb_error
*
418 ovsdb_execute_update(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
421 struct ovsdb_table
*table
;
422 const struct json
*where
, *row_json
;
423 struct ovsdb_condition condition
= OVSDB_CONDITION_INITIALIZER
;
424 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
425 struct ovsdb_row
*row
= NULL
;
426 struct update_row_cbdata ur
;
427 struct ovsdb_error
*error
;
429 table
= parse_table(x
, parser
, "table");
430 where
= ovsdb_parser_member(parser
, "where", OP_ARRAY
);
431 row_json
= ovsdb_parser_member(parser
, "row", OP_OBJECT
);
432 error
= ovsdb_parser_get_error(parser
);
434 error
= parse_row(row_json
, table
, x
->symtab
, &row
, &columns
);
439 for (i
= 0; i
< columns
.n_columns
; i
++) {
440 const struct ovsdb_column
*column
= columns
.columns
[i
];
442 if (!column
->mutable) {
443 error
= ovsdb_syntax_error(parser
->json
,
444 "constraint violation",
445 "Cannot update immutable column %s "
447 column
->name
, table
->schema
->name
);
453 error
= ovsdb_condition_from_json(table
->schema
, where
, x
->symtab
,
460 ur
.columns
= &columns
;
461 ovsdb_query(table
, &condition
, update_row_cb
, &ur
);
462 json_object_put(result
, "count", json_integer_create(ur
.n_matches
));
465 ovsdb_row_destroy(row
);
466 ovsdb_column_set_destroy(&columns
);
467 ovsdb_condition_destroy(&condition
);
472 struct mutate_row_cbdata
{
474 struct ovsdb_txn
*txn
;
475 const struct ovsdb_mutation_set
*mutations
;
476 struct ovsdb_error
**error
;
480 mutate_row_cb(const struct ovsdb_row
*row
, void *mr_
)
482 struct mutate_row_cbdata
*mr
= mr_
;
485 *mr
->error
= ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr
->txn
, row
),
487 return *mr
->error
== NULL
;
490 static struct ovsdb_error
*
491 ovsdb_execute_mutate(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
494 struct ovsdb_table
*table
;
495 const struct json
*where
;
496 const struct json
*mutations_json
;
497 struct ovsdb_condition condition
= OVSDB_CONDITION_INITIALIZER
;
498 struct ovsdb_mutation_set mutations
= OVSDB_MUTATION_SET_INITIALIZER
;
499 struct ovsdb_row
*row
= NULL
;
500 struct mutate_row_cbdata mr
;
501 struct ovsdb_error
*error
;
503 table
= parse_table(x
, parser
, "table");
504 where
= ovsdb_parser_member(parser
, "where", OP_ARRAY
);
505 mutations_json
= ovsdb_parser_member(parser
, "mutations", OP_ARRAY
);
506 error
= ovsdb_parser_get_error(parser
);
508 error
= ovsdb_mutation_set_from_json(table
->schema
, mutations_json
,
509 x
->symtab
, &mutations
);
512 error
= ovsdb_condition_from_json(table
->schema
, where
, x
->symtab
,
518 mr
.mutations
= &mutations
;
520 ovsdb_query(table
, &condition
, mutate_row_cb
, &mr
);
521 json_object_put(result
, "count", json_integer_create(mr
.n_matches
));
524 ovsdb_row_destroy(row
);
525 ovsdb_mutation_set_destroy(&mutations
);
526 ovsdb_condition_destroy(&condition
);
531 struct delete_row_cbdata
{
533 const struct ovsdb_table
*table
;
534 struct ovsdb_txn
*txn
;
538 delete_row_cb(const struct ovsdb_row
*row
, void *dr_
)
540 struct delete_row_cbdata
*dr
= dr_
;
543 ovsdb_txn_row_delete(dr
->txn
, row
);
548 static struct ovsdb_error
*
549 ovsdb_execute_delete(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
552 struct ovsdb_table
*table
;
553 const struct json
*where
;
554 struct ovsdb_condition condition
= OVSDB_CONDITION_INITIALIZER
;
555 struct ovsdb_error
*error
;
557 where
= ovsdb_parser_member(parser
, "where", OP_ARRAY
);
558 table
= parse_table(x
, parser
, "table");
559 error
= ovsdb_parser_get_error(parser
);
561 error
= ovsdb_condition_from_json(table
->schema
, where
, x
->symtab
,
565 struct delete_row_cbdata dr
;
570 ovsdb_query(table
, &condition
, delete_row_cb
, &dr
);
572 json_object_put(result
, "count", json_integer_create(dr
.n_matches
));
575 ovsdb_condition_destroy(&condition
);
580 struct wait_auxdata
{
581 struct ovsdb_row_hash
*actual
;
582 struct ovsdb_row_hash
*expected
;
587 ovsdb_execute_wait_query_cb(const struct ovsdb_row
*row
, void *aux_
)
589 struct wait_auxdata
*aux
= aux_
;
591 if (ovsdb_row_hash_contains(aux
->expected
, row
)) {
592 ovsdb_row_hash_insert(aux
->actual
, row
);
595 /* The query row isn't in the expected result set, so the actual and
596 * expected results sets definitely differ and we can short-circuit the
597 * rest of the query. */
603 static struct ovsdb_error
*
604 ovsdb_execute_wait(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
605 struct json
*result OVS_UNUSED
)
607 struct ovsdb_table
*table
;
608 const struct json
*timeout
, *where
, *columns_json
, *until
, *rows
;
609 struct ovsdb_condition condition
= OVSDB_CONDITION_INITIALIZER
;
610 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
611 struct ovsdb_row_hash expected
= OVSDB_ROW_HASH_INITIALIZER(expected
);
612 struct ovsdb_row_hash actual
= OVSDB_ROW_HASH_INITIALIZER(actual
);
613 struct ovsdb_error
*error
;
614 struct wait_auxdata aux
;
615 long long int timeout_msec
= 0;
618 timeout
= ovsdb_parser_member(parser
, "timeout", OP_NUMBER
| OP_OPTIONAL
);
619 where
= ovsdb_parser_member(parser
, "where", OP_ARRAY
);
620 columns_json
= ovsdb_parser_member(parser
, "columns",
621 OP_ARRAY
| OP_OPTIONAL
);
622 until
= ovsdb_parser_member(parser
, "until", OP_STRING
);
623 rows
= ovsdb_parser_member(parser
, "rows", OP_ARRAY
);
624 table
= parse_table(x
, parser
, "table");
625 error
= ovsdb_parser_get_error(parser
);
627 error
= ovsdb_condition_from_json(table
->schema
, where
, x
->symtab
,
631 error
= ovsdb_column_set_from_json(columns_json
, table
->schema
,
636 timeout_msec
= MIN(LLONG_MAX
, json_real(timeout
));
637 if (timeout_msec
< 0) {
638 error
= ovsdb_syntax_error(timeout
, NULL
,
639 "timeout must be nonnegative");
640 } else if (timeout_msec
< x
->timeout_msec
) {
641 x
->timeout_msec
= timeout_msec
;
644 timeout_msec
= LLONG_MAX
;
648 if (strcmp(json_string(until
), "==")
649 && strcmp(json_string(until
), "!=")) {
650 error
= ovsdb_syntax_error(until
, NULL
,
651 "\"until\" must be \"==\" or \"!=\"");
655 /* Parse "rows" into 'expected'. */
656 ovsdb_row_hash_init(&expected
, &columns
);
657 for (i
= 0; i
< rows
->u
.array
.n
; i
++) {
658 struct ovsdb_row
*row
;
660 row
= ovsdb_row_create(table
);
661 error
= ovsdb_row_from_json(row
, rows
->u
.array
.elems
[i
], x
->symtab
,
664 ovsdb_row_destroy(row
);
668 if (!ovsdb_row_hash_insert(&expected
, row
)) {
669 /* XXX Perhaps we should abort with an error or log a
671 ovsdb_row_destroy(row
);
678 ovsdb_row_hash_init(&actual
, &columns
);
679 aux
.actual
= &actual
;
680 aux
.expected
= &expected
;
682 ovsdb_query(table
, &condition
, ovsdb_execute_wait_query_cb
, &aux
);
684 /* We know that every row in 'actual' is also in 'expected'. We
685 * also know that all of the rows in 'actual' are distinct and that
686 * all of the rows in 'expected' are distinct. Therefore, if
687 * 'actual' and 'expected' have the same number of rows, then they
688 * have the same content. */
689 size_t n_actual
= ovsdb_row_hash_count(&actual
);
690 size_t n_expected
= ovsdb_row_hash_count(&expected
);
691 equal
= n_actual
== n_expected
;
693 if (!strcmp(json_string(until
), "==") != equal
) {
694 if (timeout
&& x
->elapsed_msec
>= timeout_msec
) {
695 if (x
->elapsed_msec
) {
696 error
= ovsdb_error("timed out",
697 "\"wait\" timed out after %lld ms",
700 error
= ovsdb_error("timed out", "\"wait\" timed out");
703 /* ovsdb_execute() will change this, if triggers really are
705 error
= ovsdb_error("not supported", "triggers not supported");
711 ovsdb_row_hash_destroy(&expected
, true);
712 ovsdb_row_hash_destroy(&actual
, false);
713 ovsdb_column_set_destroy(&columns
);
714 ovsdb_condition_destroy(&condition
);
719 static struct ovsdb_error
*
720 ovsdb_execute_comment(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
721 struct json
*result OVS_UNUSED
)
723 const struct json
*comment
;
725 comment
= ovsdb_parser_member(parser
, "comment", OP_STRING
);
729 ovsdb_txn_add_comment(x
->txn
, json_string(comment
));
734 static struct ovsdb_error
*
735 ovsdb_execute_assert(struct ovsdb_execution
*x
, struct ovsdb_parser
*parser
,
736 struct json
*result OVS_UNUSED
)
738 const struct json
*lock_name
;
740 lock_name
= ovsdb_parser_member(parser
, "lock", OP_ID
);
746 const struct ovsdb_lock_waiter
*waiter
;
748 waiter
= ovsdb_session_get_lock_waiter(x
->session
,
749 json_string(lock_name
));
750 if (waiter
&& ovsdb_lock_waiter_is_owner(waiter
)) {
755 return ovsdb_error("not owner", "Asserted lock %s not held.",
756 json_string(lock_name
));