]> git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/execution.c
Merge "master" into "next".
[mirror_ovs.git] / ovsdb / execution.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
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 <assert.h>
19 #include <limits.h>
20
21 #include "column.h"
22 #include "condition.h"
23 #include "file.h"
24 #include "json.h"
25 #include "mutation.h"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
29 #include "ovsdb.h"
30 #include "query.h"
31 #include "row.h"
32 #include "table.h"
33 #include "timeval.h"
34 #include "transaction.h"
35
36 struct ovsdb_execution {
37 struct ovsdb *db;
38 struct ovsdb_txn *txn;
39 struct ovsdb_symbol_table *symtab;
40 bool durable;
41
42 /* Triggers. */
43 long long int elapsed_msec;
44 long long int timeout_msec;
45 };
46
47 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
48 struct ovsdb_parser *,
49 struct json *result);
50
51 static ovsdb_operation_executor ovsdb_execute_insert;
52 static ovsdb_operation_executor ovsdb_execute_select;
53 static ovsdb_operation_executor ovsdb_execute_update;
54 static ovsdb_operation_executor ovsdb_execute_mutate;
55 static ovsdb_operation_executor ovsdb_execute_delete;
56 static ovsdb_operation_executor ovsdb_execute_wait;
57 static ovsdb_operation_executor ovsdb_execute_commit;
58 static ovsdb_operation_executor ovsdb_execute_abort;
59 static ovsdb_operation_executor ovsdb_execute_comment;
60
61 static ovsdb_operation_executor *
62 lookup_executor(const char *name)
63 {
64 struct ovsdb_operation {
65 const char *name;
66 ovsdb_operation_executor *executor;
67 };
68
69 static const struct ovsdb_operation operations[] = {
70 { "insert", ovsdb_execute_insert },
71 { "select", ovsdb_execute_select },
72 { "update", ovsdb_execute_update },
73 { "mutate", ovsdb_execute_mutate },
74 { "delete", ovsdb_execute_delete },
75 { "wait", ovsdb_execute_wait },
76 { "commit", ovsdb_execute_commit },
77 { "abort", ovsdb_execute_abort },
78 { "comment", ovsdb_execute_comment },
79 };
80
81 size_t i;
82
83 for (i = 0; i < ARRAY_SIZE(operations); i++) {
84 const struct ovsdb_operation *c = &operations[i];
85 if (!strcmp(c->name, name)) {
86 return c->executor;
87 }
88 }
89 return NULL;
90 }
91
92 struct json *
93 ovsdb_execute(struct ovsdb *db, const struct json *params,
94 long long int elapsed_msec, long long int *timeout_msec)
95 {
96 struct ovsdb_execution x;
97 struct ovsdb_error *error;
98 struct json *results;
99 size_t n_operations;
100 size_t i;
101
102 if (params->type != JSON_ARRAY
103 || !params->u.array.n
104 || params->u.array.elems[0]->type != JSON_STRING
105 || strcmp(params->u.array.elems[0]->u.string, db->schema->name)) {
106 struct ovsdb_error *error;
107
108 if (params->type != JSON_ARRAY) {
109 error = ovsdb_syntax_error(params, NULL, "array expected");
110 } else {
111 error = ovsdb_syntax_error(params, NULL, "database name expected "
112 "as first parameter");
113 }
114
115 results = ovsdb_error_to_json(error);
116 ovsdb_error_destroy(error);
117 return results;
118 }
119
120 x.db = db;
121 x.txn = ovsdb_txn_create(db);
122 x.symtab = ovsdb_symbol_table_create();
123 x.durable = false;
124 x.elapsed_msec = elapsed_msec;
125 x.timeout_msec = LLONG_MAX;
126 results = NULL;
127
128 results = json_array_create_empty();
129 n_operations = params->u.array.n - 1;
130 error = NULL;
131 for (i = 1; i <= n_operations; i++) {
132 struct json *operation = params->u.array.elems[i];
133 struct ovsdb_error *parse_error;
134 struct ovsdb_parser parser;
135 struct json *result;
136 const struct json *op;
137
138 /* Parse and execute operation. */
139 ovsdb_parser_init(&parser, operation,
140 "ovsdb operation %zu of %zu", i + 1, n_operations);
141 op = ovsdb_parser_member(&parser, "op", OP_ID);
142 result = json_object_create();
143 if (op) {
144 const char *op_name = json_string(op);
145 ovsdb_operation_executor *executor = lookup_executor(op_name);
146 if (executor) {
147 error = executor(&x, &parser, result);
148 } else {
149 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
150 op_name);
151 }
152 } else {
153 assert(ovsdb_parser_has_error(&parser));
154 }
155
156 /* A parse error overrides any other error.
157 * An error overrides any other result. */
158 parse_error = ovsdb_parser_finish(&parser);
159 if (parse_error) {
160 ovsdb_error_destroy(error);
161 error = parse_error;
162 }
163 if (error) {
164 json_destroy(result);
165 result = ovsdb_error_to_json(error);
166 }
167 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
168 && timeout_msec) {
169 ovsdb_txn_abort(x.txn);
170 *timeout_msec = x.timeout_msec;
171
172 json_destroy(result);
173 json_destroy(results);
174 results = NULL;
175 goto exit;
176 }
177
178 /* Add result to array. */
179 json_array_add(results, result);
180 if (error) {
181 break;
182 }
183 }
184
185 if (!error) {
186 error = ovsdb_txn_commit(x.txn, x.durable);
187 if (error) {
188 json_array_add(results, ovsdb_error_to_json(error));
189 }
190 } else {
191 ovsdb_txn_abort(x.txn);
192 }
193
194 while (json_array(results)->n < n_operations) {
195 json_array_add(results, json_null_create());
196 }
197
198 exit:
199 ovsdb_error_destroy(error);
200 ovsdb_symbol_table_destroy(x.symtab);
201
202 return results;
203 }
204
205 struct ovsdb_error *
206 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
207 struct json *result OVS_UNUSED)
208 {
209 const struct json *durable;
210
211 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
212 if (durable && json_boolean(durable)) {
213 x->durable = true;
214 }
215 return NULL;
216 }
217
218 static struct ovsdb_error *
219 ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED,
220 struct ovsdb_parser *parser OVS_UNUSED,
221 struct json *result OVS_UNUSED)
222 {
223 return ovsdb_error("aborted", "aborted by request");
224 }
225
226 static struct ovsdb_table *
227 parse_table(struct ovsdb_execution *x,
228 struct ovsdb_parser *parser, const char *member)
229 {
230 struct ovsdb_table *table;
231 const char *table_name;
232 const struct json *json;
233
234 json = ovsdb_parser_member(parser, member, OP_ID);
235 if (!json) {
236 return NULL;
237 }
238 table_name = json_string(json);
239
240 table = shash_find_data(&x->db->tables, table_name);
241 if (!table) {
242 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
243 }
244 return table;
245 }
246
247 static WARN_UNUSED_RESULT struct ovsdb_error *
248 parse_row(struct ovsdb_parser *parser, const char *member,
249 const struct ovsdb_table *table,
250 struct ovsdb_symbol_table *symtab,
251 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
252 {
253 struct ovsdb_error *error;
254 const struct json *json;
255 struct ovsdb_row *row;
256
257 *rowp = NULL;
258
259 if (!table) {
260 return OVSDB_BUG("null table");
261 }
262 json = ovsdb_parser_member(parser, member, OP_OBJECT);
263 if (!json) {
264 return OVSDB_BUG("null row member");
265 }
266
267 row = ovsdb_row_create(table);
268 error = ovsdb_row_from_json(row, json, symtab, columns);
269 if (error) {
270 ovsdb_row_destroy(row);
271 return error;
272 } else {
273 *rowp = row;
274 return NULL;
275 }
276 }
277
278 struct ovsdb_error *
279 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
280 struct json *result)
281 {
282 struct ovsdb_table *table;
283 struct ovsdb_row *row = NULL;
284 const struct json *uuid_name;
285 struct ovsdb_error *error;
286 struct uuid row_uuid;
287
288 table = parse_table(x, parser, "table");
289 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
290 error = ovsdb_parser_get_error(parser);
291
292 if (uuid_name) {
293 struct ovsdb_symbol *symbol;
294
295 symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
296 if (symbol->used) {
297 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
298 "This \"uuid-name\" appeared on an "
299 "earlier \"insert\" operation.");
300 }
301 row_uuid = symbol->uuid;
302 symbol->used = true;
303 } else {
304 uuid_generate(&row_uuid);
305 }
306
307 if (!error) {
308 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
309 }
310 if (!error) {
311 /* Check constraints for columns not included in "row", in case the
312 * default values do not satisfy the constraints. We could check only
313 * the columns that have their default values by supplying an
314 * ovsdb_column_set to parse_row() above, but I suspect that this is
315 * cheaper. */
316 const struct shash_node *node;
317
318 SHASH_FOR_EACH (node, &table->schema->columns) {
319 const struct ovsdb_column *column = node->data;
320 const struct ovsdb_datum *datum = &row->fields[column->index];
321
322 /* If there are 0 keys or pairs, there's nothing to check.
323 * If there is 1, it might be a default value.
324 * If there are more, it can't be a default value, so the value has
325 * already been checked. */
326 if (datum->n == 1) {
327 error = ovsdb_datum_check_constraints(datum, &column->type);
328 if (error) {
329 ovsdb_row_destroy(row);
330 break;
331 }
332 }
333 }
334 }
335 if (!error) {
336 *ovsdb_row_get_uuid_rw(row) = row_uuid;
337 ovsdb_txn_row_insert(x->txn, row);
338 json_object_put(result, "uuid",
339 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
340 &ovsdb_type_uuid));
341 }
342 return error;
343 }
344
345 struct ovsdb_error *
346 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
347 struct json *result)
348 {
349 struct ovsdb_table *table;
350 const struct json *where, *columns_json, *sort_json;
351 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
352 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
353 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
354 struct ovsdb_error *error;
355
356 table = parse_table(x, parser, "table");
357 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
358 columns_json = ovsdb_parser_member(parser, "columns",
359 OP_ARRAY | OP_OPTIONAL);
360 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
361
362 error = ovsdb_parser_get_error(parser);
363 if (!error) {
364 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
365 &condition);
366 }
367 if (!error) {
368 error = ovsdb_column_set_from_json(columns_json, table, &columns);
369 }
370 if (!error) {
371 error = ovsdb_column_set_from_json(sort_json, table, &sort);
372 }
373 if (!error) {
374 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
375
376 ovsdb_query_distinct(table, &condition, &columns, &rows);
377 ovsdb_row_set_sort(&rows, &sort);
378 json_object_put(result, "rows",
379 ovsdb_row_set_to_json(&rows, &columns));
380
381 ovsdb_row_set_destroy(&rows);
382 }
383
384 ovsdb_column_set_destroy(&columns);
385 ovsdb_column_set_destroy(&sort);
386 ovsdb_condition_destroy(&condition);
387
388 return error;
389 }
390
391 struct update_row_cbdata {
392 size_t n_matches;
393 struct ovsdb_txn *txn;
394 const struct ovsdb_row *row;
395 const struct ovsdb_column_set *columns;
396 };
397
398 static bool
399 update_row_cb(const struct ovsdb_row *row, void *ur_)
400 {
401 struct update_row_cbdata *ur = ur_;
402
403 ur->n_matches++;
404 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
405 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
406 ur->row, ur->columns);
407 }
408
409 return true;
410 }
411
412 struct ovsdb_error *
413 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
414 struct json *result)
415 {
416 struct ovsdb_table *table;
417 const struct json *where;
418 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
419 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
420 struct ovsdb_row *row = NULL;
421 struct update_row_cbdata ur;
422 struct ovsdb_error *error;
423
424 table = parse_table(x, parser, "table");
425 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
426 error = ovsdb_parser_get_error(parser);
427 if (!error) {
428 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
429 }
430 if (!error) {
431 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
432 &condition);
433 }
434 if (!error) {
435 ur.n_matches = 0;
436 ur.txn = x->txn;
437 ur.row = row;
438 ur.columns = &columns;
439 ovsdb_query(table, &condition, update_row_cb, &ur);
440 json_object_put(result, "count", json_integer_create(ur.n_matches));
441 }
442
443 ovsdb_row_destroy(row);
444 ovsdb_column_set_destroy(&columns);
445 ovsdb_condition_destroy(&condition);
446
447 return error;
448 }
449
450 struct mutate_row_cbdata {
451 size_t n_matches;
452 struct ovsdb_txn *txn;
453 const struct ovsdb_mutation_set *mutations;
454 };
455
456 static bool
457 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
458 {
459 struct mutate_row_cbdata *mr = mr_;
460
461 mr->n_matches++;
462 ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
463 mr->mutations);
464
465 return true;
466 }
467
468 struct ovsdb_error *
469 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
470 struct json *result)
471 {
472 struct ovsdb_table *table;
473 const struct json *where;
474 const struct json *mutations_json;
475 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
476 struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
477 struct ovsdb_row *row = NULL;
478 struct mutate_row_cbdata mr;
479 struct ovsdb_error *error;
480
481 table = parse_table(x, parser, "table");
482 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
483 mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
484 error = ovsdb_parser_get_error(parser);
485 if (!error) {
486 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
487 x->symtab, &mutations);
488 }
489 if (!error) {
490 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
491 &condition);
492 }
493 if (!error) {
494 mr.n_matches = 0;
495 mr.txn = x->txn;
496 mr.mutations = &mutations;
497 ovsdb_query(table, &condition, mutate_row_cb, &mr);
498 json_object_put(result, "count", json_integer_create(mr.n_matches));
499 }
500
501 ovsdb_row_destroy(row);
502 ovsdb_mutation_set_destroy(&mutations);
503 ovsdb_condition_destroy(&condition);
504
505 return error;
506 }
507
508 struct delete_row_cbdata {
509 size_t n_matches;
510 const struct ovsdb_table *table;
511 struct ovsdb_txn *txn;
512 };
513
514 static bool
515 delete_row_cb(const struct ovsdb_row *row, void *dr_)
516 {
517 struct delete_row_cbdata *dr = dr_;
518
519 dr->n_matches++;
520 ovsdb_txn_row_delete(dr->txn, row);
521
522 return true;
523 }
524
525 struct ovsdb_error *
526 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
527 struct json *result)
528 {
529 struct ovsdb_table *table;
530 const struct json *where;
531 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
532 struct ovsdb_error *error;
533
534 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
535 table = parse_table(x, parser, "table");
536 error = ovsdb_parser_get_error(parser);
537 if (!error) {
538 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
539 &condition);
540 }
541 if (!error) {
542 struct delete_row_cbdata dr;
543
544 dr.n_matches = 0;
545 dr.table = table;
546 dr.txn = x->txn;
547 ovsdb_query(table, &condition, delete_row_cb, &dr);
548
549 json_object_put(result, "count", json_integer_create(dr.n_matches));
550 }
551
552 ovsdb_condition_destroy(&condition);
553
554 return error;
555 }
556
557 struct wait_auxdata {
558 struct ovsdb_row_hash *actual;
559 struct ovsdb_row_hash *expected;
560 bool *equal;
561 };
562
563 static bool
564 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
565 {
566 struct wait_auxdata *aux = aux_;
567
568 if (ovsdb_row_hash_contains(aux->expected, row)) {
569 ovsdb_row_hash_insert(aux->actual, row);
570 return true;
571 } else {
572 /* The query row isn't in the expected result set, so the actual and
573 * expected results sets definitely differ and we can short-circuit the
574 * rest of the query. */
575 *aux->equal = false;
576 return false;
577 }
578 }
579
580 static struct ovsdb_error *
581 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
582 struct json *result OVS_UNUSED)
583 {
584 struct ovsdb_table *table;
585 const struct json *timeout, *where, *columns_json, *until, *rows;
586 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
587 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
588 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
589 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
590 struct ovsdb_error *error;
591 struct wait_auxdata aux;
592 long long int timeout_msec = 0;
593 size_t i;
594
595 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
596 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
597 columns_json = ovsdb_parser_member(parser, "columns",
598 OP_ARRAY | OP_OPTIONAL);
599 until = ovsdb_parser_member(parser, "until", OP_STRING);
600 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
601 table = parse_table(x, parser, "table");
602 error = ovsdb_parser_get_error(parser);
603 if (!error) {
604 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
605 &condition);
606 }
607 if (!error) {
608 error = ovsdb_column_set_from_json(columns_json, table, &columns);
609 }
610 if (!error) {
611 if (timeout) {
612 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
613 if (timeout_msec < 0) {
614 error = ovsdb_syntax_error(timeout, NULL,
615 "timeout must be nonnegative");
616 } else if (timeout_msec < x->timeout_msec) {
617 x->timeout_msec = timeout_msec;
618 }
619 } else {
620 timeout_msec = LLONG_MAX;
621 }
622 if (strcmp(json_string(until), "==")
623 && strcmp(json_string(until), "!=")) {
624 error = ovsdb_syntax_error(until, NULL,
625 "\"until\" must be \"==\" or \"!=\"");
626 }
627 }
628 if (!error) {
629 /* Parse "rows" into 'expected'. */
630 ovsdb_row_hash_init(&expected, &columns);
631 for (i = 0; i < rows->u.array.n; i++) {
632 struct ovsdb_error *error;
633 struct ovsdb_row *row;
634
635 row = ovsdb_row_create(table);
636 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
637 NULL);
638 if (error) {
639 break;
640 }
641
642 if (!ovsdb_row_hash_insert(&expected, row)) {
643 /* XXX Perhaps we should abort with an error or log a
644 * warning. */
645 ovsdb_row_destroy(row);
646 }
647 }
648 }
649 if (!error) {
650 /* Execute query. */
651 bool equal = true;
652 ovsdb_row_hash_init(&actual, &columns);
653 aux.actual = &actual;
654 aux.expected = &expected;
655 aux.equal = &equal;
656 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
657 if (equal) {
658 /* We know that every row in 'actual' is also in 'expected'. We
659 * also know that all of the rows in 'actual' are distinct and that
660 * all of the rows in 'expected' are distinct. Therefore, if
661 * 'actual' and 'expected' have the same number of rows, then they
662 * have the same content. */
663 size_t n_actual = ovsdb_row_hash_count(&actual);
664 size_t n_expected = ovsdb_row_hash_count(&expected);
665 equal = n_actual == n_expected;
666 }
667 if (!strcmp(json_string(until), "==") != equal) {
668 if (timeout && x->elapsed_msec >= timeout_msec) {
669 if (x->elapsed_msec) {
670 error = ovsdb_error("timed out",
671 "\"wait\" timed out after %lld ms",
672 x->elapsed_msec);
673 } else {
674 error = ovsdb_error("timed out", "\"wait\" timed out");
675 }
676 } else {
677 /* ovsdb_execute() will change this, if triggers really are
678 * supported. */
679 error = ovsdb_error("not supported", "triggers not supported");
680 }
681 }
682 }
683
684
685 ovsdb_row_hash_destroy(&expected, true);
686 ovsdb_row_hash_destroy(&actual, false);
687 ovsdb_column_set_destroy(&columns);
688 ovsdb_condition_destroy(&condition);
689
690 return error;
691 }
692
693 static struct ovsdb_error *
694 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
695 struct json *result OVS_UNUSED)
696 {
697 const struct json *comment;
698
699 comment = ovsdb_parser_member(parser, "comment", OP_STRING);
700 if (!comment) {
701 return NULL;
702 }
703 ovsdb_txn_add_comment(x->txn, json_string(comment));
704
705 return NULL;
706 }