]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/execution.c
ovsdb-monitor: Refactor ovsdb monitor implementation.
[mirror_ovs.git] / ovsdb / execution.c
CommitLineData
1b1d2e6d 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2017 Nicira, Inc.
f85f8ebb
BP
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
1b1d2e6d
BP
18#include "ovsdb.h"
19
f85f8ebb
BP
20#include <limits.h>
21
22#include "column.h"
23#include "condition.h"
bd06962a 24#include "file.h"
ee89ea7b 25#include "openvswitch/json.h"
e9f8f936 26#include "mutation.h"
f85f8ebb
BP
27#include "ovsdb-data.h"
28#include "ovsdb-error.h"
29#include "ovsdb-parser.h"
f85f8ebb 30#include "query.h"
d6db7b3c 31#include "rbac.h"
f85f8ebb 32#include "row.h"
da897f41 33#include "server.h"
f85f8ebb
BP
34#include "table.h"
35#include "timeval.h"
36#include "transaction.h"
37
38struct ovsdb_execution {
39 struct ovsdb *db;
da897f41 40 const struct ovsdb_session *session;
f85f8ebb
BP
41 struct ovsdb_txn *txn;
42 struct ovsdb_symbol_table *symtab;
43 bool durable;
d6db7b3c
LR
44 const char *role;
45 const char *id;
f85f8ebb
BP
46
47 /* Triggers. */
48 long long int elapsed_msec;
49 long long int timeout_msec;
50};
51
52typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
53 struct ovsdb_parser *,
54 struct json *result);
55
f85f8ebb
BP
56static ovsdb_operation_executor ovsdb_execute_insert;
57static ovsdb_operation_executor ovsdb_execute_select;
58static ovsdb_operation_executor ovsdb_execute_update;
e9f8f936 59static ovsdb_operation_executor ovsdb_execute_mutate;
f85f8ebb
BP
60static ovsdb_operation_executor ovsdb_execute_delete;
61static ovsdb_operation_executor ovsdb_execute_wait;
62static ovsdb_operation_executor ovsdb_execute_commit;
63static ovsdb_operation_executor ovsdb_execute_abort;
d171b584 64static ovsdb_operation_executor ovsdb_execute_comment;
da897f41 65static ovsdb_operation_executor ovsdb_execute_assert;
f85f8ebb
BP
66
67static ovsdb_operation_executor *
e51879e9 68lookup_executor(const char *name, bool *read_only)
f85f8ebb
BP
69{
70 struct ovsdb_operation {
71 const char *name;
e51879e9 72 bool read_only;
f85f8ebb
BP
73 ovsdb_operation_executor *executor;
74 };
75
76 static const struct ovsdb_operation operations[] = {
e51879e9
AZ
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 },
f85f8ebb
BP
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)) {
e51879e9 94 *read_only = c->read_only;
f85f8ebb
BP
95 return c->executor;
96 }
97 }
98 return NULL;
99}
100
1b1d2e6d
BP
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.) */
109struct ovsdb_txn *
110ovsdb_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)
f85f8ebb
BP
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
1b1d2e6d 122 *durable = false;
9cb53f26 123 if (params->type != JSON_ARRAY
fa37affa
BP
124 || !params->array.n
125 || params->array.elems[0]->type != JSON_STRING
126 || strcmp(params->array.elems[0]->string, db->schema->name)) {
9cb53f26
BP
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
1b1d2e6d
BP
134 *resultsp = ovsdb_error_to_json_free(error);
135 return NULL;
f85f8ebb
BP
136 }
137
138 x.db = db;
da897f41 139 x.session = session;
f85f8ebb
BP
140 x.txn = ovsdb_txn_create(db);
141 x.symtab = ovsdb_symbol_table_create();
142 x.durable = false;
d6db7b3c
LR
143 x.role = role;
144 x.id = id;
f85f8ebb
BP
145 x.elapsed_msec = elapsed_msec;
146 x.timeout_msec = LLONG_MAX;
147 results = NULL;
148
149 results = json_array_create_empty();
fa37affa 150 n_operations = params->array.n - 1;
f85f8ebb 151 error = NULL;
9cb53f26 152 for (i = 1; i <= n_operations; i++) {
fa37affa 153 struct json *operation = params->array.elems[i];
f85f8ebb
BP
154 struct ovsdb_error *parse_error;
155 struct ovsdb_parser parser;
156 struct json *result;
157 const struct json *op;
e51879e9
AZ
158 const char *op_name = NULL;
159 bool ro = false;
f85f8ebb
BP
160
161 /* Parse and execute operation. */
162 ovsdb_parser_init(&parser, operation,
e51879e9
AZ
163 "ovsdb operation %"PRIuSIZE" of %"PRIuSIZE, i,
164 n_operations);
f85f8ebb
BP
165 op = ovsdb_parser_member(&parser, "op", OP_ID);
166 result = json_object_create();
167 if (op) {
e51879e9
AZ
168 op_name = json_string(op);
169 ovsdb_operation_executor *executor = lookup_executor(op_name, &ro);
f85f8ebb
BP
170 if (executor) {
171 error = executor(&x, &parser, result);
172 } else {
5764c0ed
BP
173 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
174 op_name);
f85f8ebb
BP
175 }
176 } else {
cb22974d 177 ovs_assert(ovsdb_parser_has_error(&parser));
f85f8ebb
BP
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 }
e51879e9 187 /* Create read-only violation error if there is one. */
bc7bcc40
BP
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 }
e51879e9 200 }
f85f8ebb
BP
201 if (error) {
202 json_destroy(result);
f85f8ebb 203 json_array_add(results, ovsdb_error_to_json(error));
1b1d2e6d
BP
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;
f85f8ebb 212 }
1b1d2e6d 213 json_array_add(results, result);
f85f8ebb 214 }
f85f8ebb
BP
215 while (json_array(results)->n < n_operations) {
216 json_array_add(results, json_null_create());
217 }
218
e084f690 219exit:
1b1d2e6d
BP
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;
f85f8ebb
BP
228 ovsdb_symbol_table_destroy(x.symtab);
229
1b1d2e6d
BP
230 return x.txn;
231}
232
233struct json *
234ovsdb_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 }
f85f8ebb
BP
253 return results;
254}
255
d3d8f1f7 256static struct ovsdb_error *
f85f8ebb 257ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
c69ee87c 258 struct json *result OVS_UNUSED)
f85f8ebb
BP
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
269static struct ovsdb_error *
c69ee87c
BP
270ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED,
271 struct ovsdb_parser *parser OVS_UNUSED,
272 struct json *result OVS_UNUSED)
f85f8ebb
BP
273{
274 return ovsdb_error("aborted", "aborted by request");
275}
276
f85f8ebb
BP
277static struct ovsdb_table *
278parse_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
cab50449 298static OVS_WARN_UNUSED_RESULT struct ovsdb_error *
19b48a81 299parse_row(const struct json *json, const struct ovsdb_table *table,
fbf925e4 300 struct ovsdb_symbol_table *symtab,
f85f8ebb
BP
301 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
302{
303 struct ovsdb_error *error;
f85f8ebb
BP
304 struct ovsdb_row *row;
305
306 *rowp = NULL;
307
308 if (!table) {
309 return OVSDB_BUG("null table");
310 }
f85f8ebb 311 if (!json) {
19b48a81 312 return OVSDB_BUG("null row");
f85f8ebb
BP
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
d3d8f1f7 326static struct ovsdb_error *
f85f8ebb
BP
327ovsdb_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;
19b48a81 332 const struct json *uuid_name, *row_json;
f85f8ebb 333 struct ovsdb_error *error;
6e30ca63 334 struct uuid row_uuid;
f85f8ebb
BP
335
336 table = parse_table(x, parser, "table");
337 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
19b48a81 338 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
f85f8ebb 339 error = ovsdb_parser_get_error(parser);
19b48a81
BP
340 if (error) {
341 return error;
342 }
6e30ca63 343
6e30ca63 344 if (uuid_name) {
2d2d6d4a
BP
345 struct ovsdb_symbol *symbol;
346
fbf925e4 347 symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
e9387de4 348 if (symbol->created) {
fbf925e4
BP
349 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
350 "This \"uuid-name\" appeared on an "
351 "earlier \"insert\" operation.");
2d2d6d4a 352 }
fbf925e4 353 row_uuid = symbol->uuid;
e9387de4 354 symbol->created = true;
2d2d6d4a
BP
355 } else {
356 uuid_generate(&row_uuid);
6e30ca63
BP
357 }
358
f85f8ebb 359 if (!error) {
19b48a81 360 error = parse_row(row_json, table, x->symtab, &row, NULL);
f85f8ebb 361 }
bd76d25d
BP
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) {
bd76d25d
BP
381 break;
382 }
383 }
384 }
385 }
d6db7b3c
LR
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
f85f8ebb 393 if (!error) {
6e30ca63 394 *ovsdb_row_get_uuid_rw(row) = row_uuid;
f85f8ebb
BP
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));
38272efe
YS
399 } else {
400 ovsdb_row_destroy(row);
f85f8ebb
BP
401 }
402 return error;
403}
404
d3d8f1f7 405static struct ovsdb_error *
f85f8ebb
BP
406ovsdb_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;
f0d7ae19 411 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER(&condition);
f85f8ebb
BP
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) {
1cb29ab0
BP
428 error = ovsdb_column_set_from_json(columns_json, table->schema,
429 &columns);
f85f8ebb
BP
430 }
431 if (!error) {
1cb29ab0 432 error = ovsdb_column_set_from_json(sort_json, table->schema, &sort);
f85f8ebb
BP
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
452struct 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;
d6db7b3c
LR
457 const char *role;
458 const char *id;
f85f8ebb
BP
459};
460
461static bool
462update_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
d3d8f1f7 475static struct ovsdb_error *
f85f8ebb
BP
476ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
477 struct json *result)
478{
479 struct ovsdb_table *table;
19b48a81 480 const struct json *where, *row_json;
f0d7ae19 481 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER(&condition);
f85f8ebb
BP
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);
19b48a81 489 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
f85f8ebb
BP
490 error = ovsdb_parser_get_error(parser);
491 if (!error) {
19b48a81 492 error = parse_row(row_json, table, x->symtab, &row, &columns);
f85f8ebb 493 }
341c4e59
BP
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 }
f85f8ebb
BP
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;
d6db7b3c
LR
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 }
f85f8ebb
BP
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
e9f8f936
BP
538struct mutate_row_cbdata {
539 size_t n_matches;
540 struct ovsdb_txn *txn;
541 const struct ovsdb_mutation_set *mutations;
b7585d1d 542 struct ovsdb_error **error;
e9f8f936
BP
543};
544
545static bool
546mutate_row_cb(const struct ovsdb_row *row, void *mr_)
547{
548 struct mutate_row_cbdata *mr = mr_;
549
550 mr->n_matches++;
b7585d1d
BP
551 *mr->error = ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
552 mr->mutations);
553 return *mr->error == NULL;
e9f8f936
BP
554}
555
d3d8f1f7 556static struct ovsdb_error *
e9f8f936
BP
557ovsdb_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;
f0d7ae19 563 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER(&condition);
e9f8f936
BP
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;
b7585d1d 585 mr.error = &error;
d6db7b3c
LR
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 }
e9f8f936
BP
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
f85f8ebb
BP
605struct delete_row_cbdata {
606 size_t n_matches;
607 const struct ovsdb_table *table;
608 struct ovsdb_txn *txn;
609};
610
611static bool
612delete_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
d3d8f1f7 622static struct ovsdb_error *
f85f8ebb
BP
623ovsdb_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;
f0d7ae19 628 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER(&condition);
f85f8ebb
BP
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;
f85f8ebb 644
d6db7b3c
LR
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 }
f85f8ebb
BP
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
661struct wait_auxdata {
662 struct ovsdb_row_hash *actual;
663 struct ovsdb_row_hash *expected;
664 bool *equal;
665};
666
667static bool
668ovsdb_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
684static struct ovsdb_error *
685ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
c69ee87c 686 struct json *result OVS_UNUSED)
f85f8ebb
BP
687{
688 struct ovsdb_table *table;
689 const struct json *timeout, *where, *columns_json, *until, *rows;
f0d7ae19 690 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER(&condition);
f85f8ebb
BP
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) {
1cb29ab0
BP
712 error = ovsdb_column_set_from_json(columns_json, table->schema,
713 &columns);
f85f8ebb
BP
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 }
3c5ce2c2
TG
727 }
728 if (!error) {
f85f8ebb
BP
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);
fa37affa 738 for (i = 0; i < rows->array.n; i++) {
f85f8ebb
BP
739 struct ovsdb_row *row;
740
741 row = ovsdb_row_create(table);
fa37affa 742 error = ovsdb_row_from_json(row, rows->array.elems[i], x->symtab,
f85f8ebb
BP
743 NULL);
744 if (error) {
ea30de0e 745 ovsdb_row_destroy(row);
f85f8ebb
BP
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 {
cd423a77
RM
781 error = ovsdb_error("timed out",
782 "\"where\" clause test failed");
f85f8ebb
BP
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}
2d2d6d4a 800
d171b584
BP
801static struct ovsdb_error *
802ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
c69ee87c 803 struct json *result OVS_UNUSED)
d171b584
BP
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}
da897f41
BP
815
816static struct ovsdb_error *
817ovsdb_execute_assert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
818 struct json *result OVS_UNUSED)
819{
820 const struct json *lock_name;
821
454ec971 822 lock_name = ovsdb_parser_member(parser, "lock", OP_ID);
da897f41
BP
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}