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