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