summaryrefslogtreecommitdiff
path: root/ovsdb/transaction-forward.c
blob: 963e937957355748c92809f4f12f005211c15223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2021, Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include "transaction-forward.h"

#include "coverage.h"
#include "jsonrpc.h"
#include "openvswitch/hmap.h"
#include "openvswitch/json.h"
#include "openvswitch/list.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/vlog.h"
#include "ovsdb.h"
#include "ovsdb-cs.h"
#include "util.h"

VLOG_DEFINE_THIS_MODULE(transaction_forward);

COVERAGE_DEFINE(txn_forward_cancel);
COVERAGE_DEFINE(txn_forward_complete);
COVERAGE_DEFINE(txn_forward_create);
COVERAGE_DEFINE(txn_forward_sent);

struct ovsdb_txn_forward {
    struct ovs_list new_node;    /* In 'txn_forward_new' of struct ovsdb. */
    struct hmap_node sent_node;  /* In 'txn_forward_sent' of struct ovsdb. */
    struct json *id;             /* 'id' of the forwarded transaction. */
    struct jsonrpc_msg *request; /* Original request. */
    struct jsonrpc_msg *reply;   /* Reply from the server. */
};

struct ovsdb_txn_forward *
ovsdb_txn_forward_create(struct ovsdb *db, const struct jsonrpc_msg *request)
{
    struct ovsdb_txn_forward *txn_fwd = xzalloc(sizeof *txn_fwd);

    COVERAGE_INC(txn_forward_create);
    txn_fwd->request = jsonrpc_msg_clone(request);
    ovs_list_push_back(&db->txn_forward_new, &txn_fwd->new_node);
    hmap_node_nullify(&txn_fwd->sent_node);

    return txn_fwd;
}

static void
ovsdb_txn_forward_unlist(struct ovsdb *db, struct ovsdb_txn_forward *txn_fwd)
{
    if (!ovs_list_is_empty(&txn_fwd->new_node)) {
        ovs_list_remove(&txn_fwd->new_node);
        ovs_list_init(&txn_fwd->new_node);
    }
    if (!hmap_node_is_null(&txn_fwd->sent_node)) {
        hmap_remove(&db->txn_forward_sent, &txn_fwd->sent_node);
        hmap_node_nullify(&txn_fwd->sent_node);
    }
}

void
ovsdb_txn_forward_destroy(struct ovsdb *db, struct ovsdb_txn_forward *txn_fwd)
{
    if (!txn_fwd) {
        return;
    }

    ovsdb_txn_forward_unlist(db, txn_fwd);
    json_destroy(txn_fwd->id);
    jsonrpc_msg_destroy(txn_fwd->request);
    jsonrpc_msg_destroy(txn_fwd->reply);
    free(txn_fwd);
}

bool
ovsdb_txn_forward_is_complete(const struct ovsdb_txn_forward *txn_fwd)
{
    return txn_fwd->reply != NULL;
}

void
ovsdb_txn_forward_complete(struct ovsdb *db, const struct jsonrpc_msg *reply)
{
    struct ovsdb_txn_forward *t;
    size_t hash = json_hash(reply->id, 0);

    HMAP_FOR_EACH_WITH_HASH (t, sent_node, hash, &db->txn_forward_sent) {
        if (json_equal(reply->id, t->id)) {
            COVERAGE_INC(txn_forward_complete);
            t->reply = jsonrpc_msg_clone(reply);

            /* Replacing id with the id of the original request. */
            json_destroy(t->reply->id);
            t->reply->id = json_clone(t->request->id);

            hmap_remove(&db->txn_forward_sent, &t->sent_node);
            hmap_node_nullify(&t->sent_node);

            db->run_triggers_now = db->run_triggers = true;
            return;
        }
    }
}

struct jsonrpc_msg *
ovsdb_txn_forward_steal_reply(struct ovsdb_txn_forward *txn_fwd)
{
    struct jsonrpc_msg *reply = txn_fwd->reply;

    txn_fwd->reply = NULL;
    return reply;
}

void
ovsdb_txn_forward_run(struct ovsdb *db, struct ovsdb_cs *cs)
{
    struct ovsdb_txn_forward *t;

    /* Send all transactions that needs to be forwarded. */
    LIST_FOR_EACH_SAFE (t, new_node, &db->txn_forward_new) {
        if (!ovsdb_cs_may_send_transaction(cs)) {
            break;
        }
        ovs_assert(!strcmp(t->request->method, "transact"));
        t->id = ovsdb_cs_send_transaction(cs, json_clone(t->request->params));
        if (t->id) {
            COVERAGE_INC(txn_forward_sent);
            ovs_list_remove(&t->new_node);
            ovs_list_init(&t->new_node);
            hmap_insert(&db->txn_forward_sent, &t->sent_node,
                        json_hash(t->id, 0));
        }
    }
}

void
ovsdb_txn_forward_wait(struct ovsdb *db, struct ovsdb_cs *cs)
{
    if (ovsdb_cs_may_send_transaction(cs)
        && !ovs_list_is_empty(&db->txn_forward_new)) {
        poll_immediate_wake();
    }
}

void
ovsdb_txn_forward_cancel(struct ovsdb *db, struct ovsdb_txn_forward *txn_fwd)
{
    COVERAGE_INC(txn_forward_cancel);
    jsonrpc_msg_destroy(txn_fwd->reply);
    txn_fwd->reply = jsonrpc_create_error(json_string_create("canceled"),
                                          txn_fwd->request->id);
    ovsdb_txn_forward_unlist(db, txn_fwd);
}

void
ovsdb_txn_forward_cancel_all(struct ovsdb *db, bool sent_only)
{
    struct ovsdb_txn_forward *t;

    HMAP_FOR_EACH_SAFE (t, sent_node, &db->txn_forward_sent) {
        ovsdb_txn_forward_cancel(db, t);
    }

    if (sent_only) {
        return;
    }

    LIST_FOR_EACH_SAFE (t, new_node, &db->txn_forward_new) {
        ovsdb_txn_forward_cancel(db, t);
    }
}