-
Notifications
You must be signed in to change notification settings - Fork 390
/
ovsdb_client_connection_state.cc
221 lines (197 loc) · 7.53 KB
/
ovsdb_client_connection_state.cc
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*/
#include <base/string_util.h>
#include "ovsdb_client_connection_state.h"
#include "ha_stale_dev_vn.h"
using namespace OVSDB;
using namespace process;
using namespace std;
ConnectionStateEntry::ConnectionStateEntry(ConnectionStateTable *table,
const std::string &device_name,
const boost::uuids::uuid &u) :
table_(table), device_name_(device_name), device_uuid_(u),
device_entry_(NULL), ha_stale_dev_vn_table_(NULL) {
refcount_ = 0;
}
ConnectionStateEntry::~ConnectionStateEntry() {
if (ha_stale_dev_vn_table_ != NULL) {
ha_stale_dev_vn_table_->DeleteTable();
ha_stale_dev_vn_table_ = NULL;
}
}
bool ConnectionStateEntry::IsConnectionActive() {
if (idl_list_.empty()) {
return false;
}
IdlList::iterator it = idl_list_.begin();
for (; it != idl_list_.end(); ++it) {
// if there is any non deleted IDL available
// return true
if (!(*it)->IsDeleted()) {
return true;
}
}
return false;
}
void OVSDB::intrusive_ptr_add_ref(ConnectionStateEntry *p) {
assert(p->device_entry_ != NULL || !p->idl_list_.empty());
p->refcount_++;
}
void OVSDB::intrusive_ptr_release(ConnectionStateEntry *p) {
int count = --p->refcount_;
if (count == 0 && p->idl_list_.empty() && p->device_entry_ == NULL) {
// delete the entry and free the state
p->table_->UpdateConnectionInfo(p, true);
p->table_->entry_map_.erase(p->device_name_);
delete p;
}
}
ConnectionStateTable::ConnectionStateTable(Agent *agent,
OvsPeerManager *manager)
: agent_(agent), table_(agent->physical_device_table()),
manager_(manager) {
id_ = table_->Register(
boost::bind(&ConnectionStateTable::PhysicalDeviceNotify,
this, _1, _2));
}
ConnectionStateTable::~ConnectionStateTable() {
table_->Unregister(id_);
}
void ConnectionStateTable::AddIdlToConnectionState(const std::string &dev_name,
OvsdbClientIdl *idl) {
ConnectionStateEntry *state = new ConnectionStateEntry(this, dev_name,
boost::uuids::nil_uuid());
pair<EntryMap::iterator, bool> ret;
ret = entry_map_.insert(pair<string, ConnectionStateEntry*>(dev_name,
state));
if (ret.second == false) {
// entry already existed, delete allocated memory
delete state;
}
ret.first->second->idl_list_.insert(idl);
UpdateConnectionInfo(ret.first->second, false);
}
void ConnectionStateTable::DelIdlToConnectionState(const std::string &dev_name,
OvsdbClientIdl *idl) {
EntryMap::iterator it = entry_map_.find(dev_name);
if (it == entry_map_.end()) {
return;
}
it->second->idl_list_.erase(idl);
if (it->second->idl_list_.empty()) {
if (it->second->device_entry_ == NULL && it->second->refcount_ == 0) {
UpdateConnectionInfo(it->second, true);
delete it->second;
entry_map_.erase(it);
} else {
UpdateConnectionInfo(it->second, false);
}
}
}
ConnectionStateEntry *ConnectionStateTable::Find(const std::string &dev_name) {
EntryMap::iterator it = entry_map_.find(dev_name);
if (it == entry_map_.end()) {
return NULL;
}
return it->second;
}
void ConnectionStateTable::PhysicalDeviceNotify(DBTablePartBase *part,
DBEntryBase *e) {
PhysicalDevice *dev = static_cast<PhysicalDevice *>(e);
ConnectionStateEntry *state =
static_cast<ConnectionStateEntry *>(dev->GetState(table_, id_));
if (dev->IsDeleted()) {
if (state) {
state->device_entry_ = NULL;
if (state->idl_list_.empty() && state->refcount_ == 0) {
UpdateConnectionInfo(state, true);
entry_map_.erase(state->device_name_);
delete state;
} else {
UpdateConnectionInfo(state, false);
}
dev->ClearState(table_, id_);
if (state->ha_stale_dev_vn_table_ != NULL) {
state->ha_stale_dev_vn_table_->DeleteTable();
state->ha_stale_dev_vn_table_ = NULL;
}
}
return;
}
if (!state) {
if (dev->name().empty())
return;
state = new ConnectionStateEntry(this, dev->name(), dev->uuid());
pair<EntryMap::iterator, bool> ret;
ret = entry_map_.insert(pair<string, ConnectionStateEntry*>(dev->name(),
state));
if (ret.second == false) {
// entry already existed, delete allocated memory
delete state;
}
state = ret.first->second;
ret.first->second->device_uuid_ = dev->uuid();
ret.first->second->device_entry_ = dev;
dev->SetState(table_, id_, ret.first->second);
UpdateConnectionInfo(ret.first->second, false);
}
if (state->ha_stale_dev_vn_table_ == NULL) {
state->ha_stale_dev_vn_table_ =
new HaStaleDevVnTable(agent_, manager_, state,
state->device_name_);
}
}
void ConnectionStateTable::UpdateConnectionInfo(ConnectionStateEntry *entry,
bool deleted) {
NotifyUve(entry, deleted);
if (agent_->connection_state() == NULL)
return;
if (deleted) {
agent_->connection_state()->Delete(ConnectionType::TOR,
entry->device_name_);
} else {
ConnectionStatus::type status = ConnectionStatus::UP;
string message;
boost::asio::ip::tcp::endpoint ep;
if (!entry->idl_list_.empty()) {
ConnectionStateEntry::IdlList::iterator it =
entry->idl_list_.begin();
ep.address((*it)->remote_ip());
ep.port((*it)->remote_port());
}
if (entry->device_entry_ == NULL) {
status = ConnectionStatus::DOWN;
message = "Config Not Available";
} else if (entry->idl_list_.empty()) {
status = ConnectionStatus::DOWN;
message = "ToR Not Available";
} else {
message = "Active sessions = " +
integerToString(entry->idl_list_.size());
}
agent_->connection_state()->Update(ConnectionType::TOR,
entry->device_name_, status,
ep, message);
}
}
void ConnectionStateTable::NotifyUve(ConnectionStateEntry *entry,
bool deleted) {
/* If device uuid is not available we cannot notify Uve Module */
if (entry->device_uuid_ == boost::uuids::nil_uuid())
return;
if (agent_->uve() == NULL || agent_->uve()->prouter_uve_table() == NULL)
return;
ProuterUveTable *ptable = agent_->uve()->prouter_uve_table();
if (deleted) {
ptable->UpdateMastership(entry->device_uuid_, false);
} else {
bool mastership = true;
if (entry->device_entry_ == NULL) {
mastership = false;
} else if (entry->idl_list_.empty()) {
mastership = false;
}
ptable->UpdateMastership(entry->device_uuid_, mastership);
}
}