netdev: Allow recv, recv_wait, drain, send, send_wait to be null.
[sliver-openvswitch.git] / lib / ovsdb-idl.c
index 7bbe6bf..42c53b8 100644 (file)
@@ -81,6 +81,7 @@ struct ovsdb_idl_txn {
     struct ovsdb_idl *idl;
     struct hmap txn_rows;
     enum ovsdb_idl_txn_status status;
+    char *error;
     bool dry_run;
     struct ds comment;
 
@@ -90,6 +91,16 @@ struct ovsdb_idl_txn {
     struct json *inc_where;
     unsigned int inc_index;
     int64_t inc_new_value;
+
+    /* Inserted rows. */
+    struct hmap inserted_rows;
+};
+
+struct ovsdb_idl_txn_insert {
+    struct hmap_node hmap_node; /* In struct ovsdb_idl_txn's inserted_rows. */
+    struct uuid dummy;          /* Dummy UUID used locally. */
+    int op_index;               /* Index into transaction's operation array. */
+    struct uuid real;           /* Real UUID used by database server. */
 };
 
 static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@ -202,9 +213,7 @@ ovsdb_idl_clear(struct ovsdb_idl *idl)
 
             if (!ovsdb_idl_row_is_orphan(row)) {
                 ovsdb_idl_row_unparse(row);
-                ovsdb_idl_row_clear_old(row);
             }
-            hmap_remove(&table->rows, &row->hmap_node);
             LIST_FOR_EACH_SAFE (arc, next_arc, struct ovsdb_idl_arc, src_node,
                                 &row->src_arcs) {
                 free(arc);
@@ -212,7 +221,7 @@ ovsdb_idl_clear(struct ovsdb_idl *idl)
             /* No need to do anything with dst_arcs: some node has those arcs
              * as forward arcs and will destroy them itself. */
 
-            free(row);
+            ovsdb_idl_row_destroy(row);
         }
     }
 
@@ -333,7 +342,9 @@ ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
 
     json_destroy(idl->monitor_request_id);
     msg = jsonrpc_create_request(
-        "monitor", json_array_create_2(json_null_create(), monitor_requests),
+        "monitor",
+        json_array_create_3(json_string_create(idl->class->database),
+                            json_null_create(), monitor_requests),
         &idl->monitor_request_id);
     jsonrpc_session_send(idl->session, msg);
 }
@@ -530,10 +541,41 @@ ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
     }
 }
 
+/* When a row A refers to row B through a column with a "refTable" constraint,
+ * but row B does not exist, row B is called an "orphan row".  Orphan rows
+ * should not persist, because the database enforces referential integrity, but
+ * they can appear transiently as changes from the database are received (the
+ * database doesn't try to topologically sort them and circular references mean
+ * it isn't always possible anyhow).
+ *
+ * This function returns true if 'row' is an orphan row, otherwise false.
+ */
 static bool
 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
 {
-    return !row->old;
+    return !row->old && !row->new;
+}
+
+/* Returns true if 'row' is conceptually part of the database as modified by
+ * the current transaction (if any), false otherwise.
+ *
+ * This function will return true if 'row' is not an orphan (see the comment on
+ * ovsdb_idl_row_is_orphan()) and:
+ *
+ *   - 'row' exists in the database and has not been deleted within the
+ *     current transaction (if any).
+ *
+ *   - 'row' was inserted within the current transaction and has not been
+ *     deleted.  (In the latter case you should not have passed 'row' in at
+ *     all, because ovsdb_idl_txn_delete() freed it.)
+ *
+ * This function will return false if 'row' is an orphan or if 'row' was
+ * deleted within the current transaction.
+ */
+static bool
+ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
+{
+    return row->new != NULL;
 }
 
 static void
@@ -617,7 +659,7 @@ ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
 
 /* Force nodes that reference 'row' to reparse. */
 static void
-ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
+ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
 {
     struct ovsdb_idl_arc *arc, *next;
 
@@ -636,7 +678,7 @@ ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row, bool destroy_dsts)
         struct ovsdb_idl_row *ref = arc->src;
 
         ovsdb_idl_row_unparse(ref);
-        ovsdb_idl_row_clear_arcs(ref, destroy_dsts);
+        ovsdb_idl_row_clear_arcs(ref, false);
         ovsdb_idl_row_parse(ref);
     }
 }
@@ -685,7 +727,7 @@ ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
     ovsdb_idl_row_update(row, row_json);
     ovsdb_idl_row_parse(row);
 
-    ovsdb_idl_row_reparse_backrefs(row, false);
+    ovsdb_idl_row_reparse_backrefs(row);
 }
 
 static void
@@ -697,7 +739,7 @@ ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
     if (list_is_empty(&row->dst_arcs)) {
         ovsdb_idl_row_destroy(row);
     } else {
-        ovsdb_idl_row_reparse_backrefs(row, true);
+        ovsdb_idl_row_reparse_backrefs(row);
     }
 }
 
@@ -799,7 +841,7 @@ next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
         struct ovsdb_idl_row *row;
 
         row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
-        if (!ovsdb_idl_row_is_orphan(row)) {
+        if (ovsdb_idl_row_exists(row)) {
             return row;
         }
     }
@@ -855,24 +897,39 @@ ovsdb_idl_txn_create(struct ovsdb_idl *idl)
 
     assert(!idl->txn);
     idl->txn = txn = xmalloc(sizeof *txn);
+    txn->request_id = NULL;
     txn->idl = idl;
-    txn->status = TXN_INCOMPLETE;
     hmap_init(&txn->txn_rows);
+    txn->status = TXN_INCOMPLETE;
+    txn->error = NULL;
     txn->dry_run = false;
     ds_init(&txn->comment);
+
     txn->inc_table = NULL;
     txn->inc_column = NULL;
     txn->inc_where = NULL;
+
+    hmap_init(&txn->inserted_rows);
+
     return txn;
 }
 
+/* Appends 's', which is treated as a printf()-type format string, to the
+ * comments that will be passed to the OVSDB server when 'txn' is committed.
+ * (The comment will be committed to the OVSDB log, which "ovsdb-tool
+ * show-log" can print in a relatively human-readable form.) */
 void
-ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
+ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
 {
+    va_list args;
+
     if (txn->comment.length) {
         ds_put_char(&txn->comment, '\n');
     }
-    ds_put_cstr(&txn->comment, s);
+
+    va_start(args, s);
+    ds_put_format_valist(&txn->comment, s, args);
+    va_end(args);
 }
 
 void
@@ -894,14 +951,23 @@ ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
 void
 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
 {
+    struct ovsdb_idl_txn_insert *insert, *next;
+
+    json_destroy(txn->request_id);
     if (txn->status == TXN_INCOMPLETE) {
         hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
     }
     ovsdb_idl_txn_abort(txn);
     ds_destroy(&txn->comment);
+    free(txn->error);
     free(txn->inc_table);
     free(txn->inc_column);
     json_destroy(txn->inc_where);
+    HMAP_FOR_EACH_SAFE (insert, next, struct ovsdb_idl_txn_insert, hmap_node,
+                        &txn->inserted_rows) {
+        free(insert);
+    }
+    hmap_destroy(&txn->inserted_rows);
     free(txn);
 }
 
@@ -1016,7 +1082,7 @@ ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
                 ovsdb_idl_row_parse(row);
             }
         } else {
-            hmap_remove(&row->table->rows, &row->hmap_node);
+            ovsdb_idl_row_unparse(row);
         }
         ovsdb_idl_row_clear_new(row);
 
@@ -1028,6 +1094,10 @@ ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
 
         hmap_remove(&txn->txn_rows, &row->txn_node);
         hmap_node_nullify(&row->txn_node);
+        if (!row->old) {
+            hmap_remove(&row->table->rows, &row->hmap_node);
+            free(row);
+        }
     }
     hmap_destroy(&txn->txn_rows);
     hmap_init(&txn->txn_rows);
@@ -1044,7 +1114,8 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
         return txn->status;
     }
 
-    operations = json_array_create_empty();
+    operations = json_array_create_1(
+        json_string_create(txn->idl->class->database));
 
     /* Add prerequisites and declarations of new rows. */
     HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
@@ -1075,16 +1146,6 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
                                                     &column->type));
             }
         }
-        if (row->new && !row->old) {
-            struct json *op;
-
-            op = json_object_create();
-            json_array_add(operations, op);
-            json_object_put_string(op, "op", "declare");
-            json_object_put(op, "uuid-name",
-                            json_string_create_nocopy(
-                                uuid_name_from_uuid(&row->uuid)));
-        }
     }
 
     /* Add updates. */
@@ -1112,9 +1173,18 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
             if (row->old) {
                 json_object_put(op, "where", where_uuid_equals(&row->uuid));
             } else {
+                struct ovsdb_idl_txn_insert *insert;
+
                 json_object_put(op, "uuid-name",
                                 json_string_create_nocopy(
                                     uuid_name_from_uuid(&row->uuid)));
+
+                insert = xmalloc(sizeof *insert);
+                insert->dummy = row->uuid;
+                insert->op_index = operations->u.array.n - 1;
+                uuid_zero(&insert->real);
+                hmap_insert(&txn->inserted_rows, &insert->hmap_node,
+                            uuid_hash(&insert->dummy));
             }
             row_json = json_object_create();
             json_object_put(op, "row", row_json);
@@ -1147,7 +1217,7 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
     if (txn->inc_table && any_updates) {
         struct json *op;
 
-        txn->inc_index = operations->u.array.n;
+        txn->inc_index = operations->u.array.n - 1;
 
         op = json_object_create();
         json_object_put_string(op, "op", "mutate");
@@ -1196,13 +1266,30 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
         hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
                     json_hash(txn->request_id, 0));
     } else {
-        txn->status = TXN_INCOMPLETE;
+        txn->status = TXN_TRY_AGAIN;
     }
 
     ovsdb_idl_txn_disassemble(txn);
     return txn->status;
 }
 
+/* Attempts to commit 'txn', blocking until the commit either succeeds or
+ * fails.  Returns the final commit status, which may be any TXN_* value other
+ * than TXN_INCOMPLETE. */
+enum ovsdb_idl_txn_status
+ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
+{
+    enum ovsdb_idl_txn_status status;
+
+    while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
+        ovsdb_idl_run(txn->idl);
+        ovsdb_idl_wait(txn->idl);
+        ovsdb_idl_txn_wait(txn);
+        poll_block();
+    }
+    return status;
+}
+
 int64_t
 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
 {
@@ -1219,6 +1306,52 @@ ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
     }
 }
 
+const char *
+ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *txn)
+{
+    if (txn->status != TXN_ERROR) {
+        return ovsdb_idl_txn_status_to_string(txn->status);
+    } else if (txn->error) {
+        return txn->error;
+    } else {
+        return "no error details available";
+    }
+}
+
+static void
+ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn *txn,
+                             const struct json *json)
+{
+    if (txn->error == NULL) {
+        txn->error = json_to_string(json, JSSF_SORT);
+    }
+}
+
+/* For transaction 'txn' that completed successfully, finds and returns the
+ * permanent UUID that the database assigned to a newly inserted row, given the
+ * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
+ *
+ * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
+ * if it was assigned by that function and then deleted by
+ * ovsdb_idl_txn_delete() within the same transaction.  (Rows that are inserted
+ * and then deleted within a single transaction are never sent to the database
+ * server, so it never assigns them a permanent UUID.) */
+const struct uuid *
+ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *txn,
+                              const struct uuid *uuid)
+{
+    const struct ovsdb_idl_txn_insert *insert;
+
+    assert(txn->status == TXN_SUCCESS || txn->status == TXN_UNCHANGED);
+    HMAP_FOR_EACH_IN_BUCKET (insert, struct ovsdb_idl_txn_insert, hmap_node,
+                             uuid_hash(uuid), &txn->inserted_rows) {
+        if (uuid_equals(uuid, &insert->dummy)) {
+            return &insert->real;
+        }
+    }
+    return NULL;
+}
+
 static void
 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
                        enum ovsdb_idl_txn_status status)
@@ -1255,6 +1388,7 @@ ovsdb_idl_txn_write(const struct ovsdb_idl_row *row_,
     size_t column_idx = column - class->columns;
 
     assert(row->new != NULL);
+    assert(column_idx < class->n_columns);
     if (hmap_node_is_null(&row->txn_node)) {
         hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
                     uuid_hash(&row->uuid));
@@ -1300,10 +1434,13 @@ ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
 }
 
 void
-ovsdb_idl_txn_delete(struct ovsdb_idl_row *row)
+ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
 {
+    struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
+
     assert(row->new != NULL);
     if (!row->old) {
+        ovsdb_idl_row_unparse(row);
         ovsdb_idl_row_clear_new(row);
         assert(!row->prereqs);
         hmap_remove(&row->table->rows, &row->hmap_node);
@@ -1319,7 +1456,7 @@ ovsdb_idl_txn_delete(struct ovsdb_idl_row *row)
     row->new = NULL;
 }
 
-struct ovsdb_idl_row *
+const struct ovsdb_idl_row *
 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
                      const struct ovsdb_idl_table_class *class)
 {
@@ -1388,7 +1525,7 @@ ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
         return false;
     }
 
-    /* We know that this is a JSON objects because the loop in
+    /* We know that this is a JSON object because the loop in
      * ovsdb_idl_txn_process_reply() checked. */
     mutate = json_object(results->elems[txn->inc_index]);
     count = shash_find_data(mutate, "count");
@@ -1426,6 +1563,44 @@ ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
     return true;
 }
 
+static bool
+ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
+                                   const struct json_array *results)
+{
+    static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
+    struct ovsdb_error *error;
+    struct json *json_uuid;
+    union ovsdb_atom uuid;
+    struct shash *reply;
+
+    if (insert->op_index >= results->n) {
+        VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
+                     "for insert (has %u, needs %u)",
+                     results->n, insert->op_index);
+        return false;
+    }
+
+    /* We know that this is a JSON object because the loop in
+     * ovsdb_idl_txn_process_reply() checked. */
+    reply = json_object(results->elems[insert->op_index]);
+    json_uuid = shash_find_data(reply, "uuid");
+    if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
+        return false;
+    }
+
+    error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
+    if (error) {
+        char *s = ovsdb_error_to_string(error);
+        VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
+                     "UUID: %s", s);
+        free(s);
+        return false;
+    }
+
+    insert->real = uuid.uuid;
+
+    return true;
+}
 
 static bool
 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
@@ -1445,47 +1620,58 @@ ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
         VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
         status = TXN_ERROR;
     } else {
+        struct json_array *ops = &msg->result->u.array;
         int hard_errors = 0;
         int soft_errors = 0;
         size_t i;
 
-        for (i = 0; i < msg->result->u.array.n; i++) {
-            struct json *json = msg->result->u.array.elems[i];
+        for (i = 0; i < ops->n; i++) {
+            struct json *op = ops->elems[i];
 
-            if (json->type == JSON_NULL) {
+            if (op->type == JSON_NULL) {
                 /* This isn't an error in itself but indicates that some prior
                  * operation failed, so make sure that we know about it. */
                 soft_errors++;
-            } else if (json->type == JSON_OBJECT) {
+            } else if (op->type == JSON_OBJECT) {
                 struct json *error;
 
-                error = shash_find_data(json_object(json), "error");
+                error = shash_find_data(json_object(op), "error");
                 if (error) {
                     if (error->type == JSON_STRING) {
                         if (!strcmp(error->u.string, "timed out")) {
                             soft_errors++;
                         } else if (strcmp(error->u.string, "aborted")) {
                             hard_errors++;
+                            ovsdb_idl_txn_set_error_json(txn, op);
                         }
                     } else {
                         hard_errors++;
+                        ovsdb_idl_txn_set_error_json(txn, op);
                         VLOG_WARN_RL(&syntax_rl,
                                      "\"error\" in reply is not JSON string");
                     }
                 }
             } else {
                 hard_errors++;
+                ovsdb_idl_txn_set_error_json(txn, op);
                 VLOG_WARN_RL(&syntax_rl,
                              "operation reply is not JSON null or object");
             }
         }
 
-        if (txn->inc_table
-            && !soft_errors
-            && !hard_errors
-            && !ovsdb_idl_txn_process_inc_reply(txn,
-                                                json_array(msg->result))) {
-            hard_errors++;
+        if (!soft_errors && !hard_errors) {
+            struct ovsdb_idl_txn_insert *insert;
+
+            if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
+                hard_errors++;
+            }
+
+            HMAP_FOR_EACH (insert, struct ovsdb_idl_txn_insert, hmap_node,
+                           &txn->inserted_rows) {
+                if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
+                    hard_errors++;
+                }
+            }
         }
 
         status = (hard_errors ? TXN_ERROR
@@ -1504,3 +1690,10 @@ ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
     assert(txn != NULL);
     return txn;
 }
+
+struct ovsdb_idl *
+ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)
+{
+    return txn->idl;
+}
+