-
Notifications
You must be signed in to change notification settings - Fork 390
/
sg.cc
280 lines (246 loc) · 9.12 KB
/
sg.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <algorithm>
#include <boost/uuid/uuid_io.hpp>
#include <base/parse_object.h>
#include <ifmap/ifmap_link.h>
#include <ifmap/ifmap_table.h>
#include <vnc_cfg_types.h>
#include <cmn/agent_cmn.h>
#include <cfg/cfg_init.h>
#include <cfg/cfg_listener.h>
#include <oper/sg.h>
#include <filter/acl.h>
#include <oper/interface_common.h>
#include <oper/mirror_table.h>
#include <oper/agent_sandesh.h>
using namespace autogen;
using namespace std;
SgTable *SgTable::sg_table_;
bool SgEntry::IsLess(const DBEntry &rhs) const {
const SgEntry &a = static_cast<const SgEntry &>(rhs);
return (sg_uuid_ < a.sg_uuid_);
}
string SgEntry::ToString() const {
std::stringstream uuidstring;
uuidstring << sg_uuid_;
return uuidstring.str();
}
DBEntryBase::KeyPtr SgEntry::GetDBRequestKey() const {
SgKey *key = new SgKey(sg_uuid_);
return DBEntryBase::KeyPtr(key);
}
void SgEntry::SetKey(const DBRequestKey *key) {
const SgKey *k = static_cast<const SgKey *>(key);
sg_uuid_ = k->sg_uuid_;
}
std::auto_ptr<DBEntry> SgTable::AllocEntry(const DBRequestKey *k) const {
const SgKey *key = static_cast<const SgKey *>(k);
SgEntry *sg = new SgEntry(key->sg_uuid_);
return std::auto_ptr<DBEntry>(static_cast<DBEntry *>(sg));
}
DBEntry *SgTable::Add(const DBRequest *req) {
SgKey *key = static_cast<SgKey *>(req->key.get());
SgEntry *sg = new SgEntry(key->sg_uuid_);
ChangeHandler(sg, req);
sg->SendObjectLog(AgentLogEvent::ADD);
return sg;
}
bool SgTable::OnChange(DBEntry *entry, const DBRequest *req) {
bool ret = ChangeHandler(entry, req);
SgEntry *sg = static_cast<SgEntry *>(entry);
sg->SendObjectLog(AgentLogEvent::CHANGE);
return ret;
}
bool SgTable::ChangeHandler(DBEntry *entry, const DBRequest *req) {
bool ret = false;
SgEntry *sg = static_cast<SgEntry *>(entry);
SgData *data = static_cast<SgData *>(req->data.get());
if (sg->sg_id_ != data->sg_id_) {
sg->sg_id_ = data->sg_id_;
ret = true;
}
AclKey key(data->egress_acl_id_);
AclDBEntry *acl = static_cast<AclDBEntry *>(Agent::GetInstance()->acl_table()->FindActiveEntry(&key));
if (sg->egress_acl_ != acl) {
sg->egress_acl_ = acl;
ret = true;
}
key = AclKey(data->ingress_acl_id_);
acl = static_cast<AclDBEntry *>(Agent::GetInstance()->acl_table()->FindActiveEntry(&key));
if (sg->ingress_acl_ != acl) {
sg->ingress_acl_ = acl;
ret = true;
}
return ret;
}
bool SgTable::Delete(DBEntry *entry, const DBRequest *req) {
SgEntry *sg = static_cast<SgEntry *>(entry);
sg->SendObjectLog(AgentLogEvent::DELETE);
return true;
}
DBTableBase *SgTable::CreateTable(DB *db, const std::string &name) {
sg_table_ = new SgTable(db, name);
sg_table_->Init();
return sg_table_;
};
bool SgTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u) {
SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject());
assert(cfg);
autogen::IdPermsType id_perms = cfg->id_perms();
CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u);
return true;
}
bool SgTable::IFNodeToReq(IFMapNode *node, DBRequest &req) {
SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject());
assert(cfg);
uuid u;
if (agent()->cfg_listener()->GetCfgDBStateUuid(node, u) == false)
return false;
SgKey *key = new SgKey(u);
SgData *data = NULL;
if (node->IsDeleted()) {
req.oper = DBRequest::DB_ENTRY_DELETE;
} else {
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
uint32_t sg_id;
stringToInteger(cfg->id(), sg_id);
if (sg_id == SgTable::kInvalidSgId) {
OPER_TRACE(Sg, "Ignore SG id 0", UuidToString(u));
return false;
}
uuid egress_acl_uuid = nil_uuid();
uuid ingress_acl_uuid = nil_uuid();
IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
for (DBGraphVertex::adjacency_iterator iter =
node->begin(table->GetGraph());
iter != node->end(table->GetGraph()); ++iter) {
IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
if (Agent::GetInstance()->cfg_listener()->SkipNode(adj_node)) {
continue;
}
if (adj_node->table() == Agent::GetInstance()->cfg()->cfg_acl_table()) {
AccessControlList *acl_cfg = static_cast<AccessControlList *>
(adj_node->GetObject());
assert(acl_cfg);
autogen::IdPermsType id_perms = acl_cfg->id_perms();
if (adj_node->name().find("egress-access-control-list") != std::string::npos) {
CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
egress_acl_uuid);
}
if (adj_node->name().find("ingress-access-control-list") != std::string::npos) {
CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
ingress_acl_uuid);
}
}
}
data = new SgData(sg_id, egress_acl_uuid, ingress_acl_uuid);
}
req.key.reset(key);
req.data.reset(data);
Agent::GetInstance()->sg_table()->Enqueue(&req);
if (node->IsDeleted()) {
return false;
}
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
for (DBGraphVertex::adjacency_iterator iter =
node->begin(table->GetGraph());
iter != node->end(table->GetGraph()); ++iter) {
IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
if (Agent::GetInstance()->cfg_listener()->SkipNode
(adj_node, Agent::GetInstance()->cfg()->cfg_vm_interface_table())) {
continue;
}
if (adj_node->GetObject() == NULL) {
continue;
}
if (Agent::GetInstance()->interface_table()->IFNodeToReq(adj_node, req)) {
Agent::GetInstance()->interface_table()->Enqueue(&req);
}
}
return false;
}
bool SgTable::IFLinkToReq(IFMapLink *link, IFMapNode *node,
const std::string &peer_type, IFMapNode *peer,
DBRequest &req) {
// Add/Delete of link other than VMInterface will most likely need re-eval
// of VN.
if (peer_type != "virtual-machine-interface") {
return IFNodeToReq(node, req);
}
// If peer is VMI, invoke re-eval if peer node is present
if (peer && peer->table() == agent()->cfg()->cfg_vm_interface_table()) {
DBRequest vmi_req;
if (agent()->interface_table()->IFNodeToReq(peer, vmi_req) == true) {
LOG(DEBUG, "SG change sync for Port " << peer->name());
agent()->interface_table()->Enqueue(&vmi_req);
}
return false;
}
return false;
}
bool SgEntry::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
SgListResp *resp = static_cast<SgListResp *>(sresp);
std::string str_uuid = UuidToString(GetSgUuid());
if (name.empty() ||
(str_uuid == name) ||
(integerToString(GetSgId()) == name)) {
SgSandeshData data;
data.set_ref_count(GetRefCount());
data.set_sg_uuid(str_uuid);
data.set_sg_id(GetSgId());
if (GetEgressAcl()) {
data.set_egress_acl_uuid(UuidToString(GetEgressAcl()->GetUuid()));
}
if (GetIngressAcl()) {
data.set_ingress_acl_uuid(UuidToString(GetIngressAcl()->GetUuid()));
}
std::vector<SgSandeshData> &list =
const_cast<std::vector<SgSandeshData>&>(resp->get_sg_list());
list.push_back(data);
return true;
}
return false;
}
void SgEntry::SendObjectLog(AgentLogEvent::type event) const {
SgObjectLogInfo info;
string str;
switch(event) {
case AgentLogEvent::ADD:
str.assign("Addition");
break;
case AgentLogEvent::DELETE:
str.assign("Deletion");
break;
case AgentLogEvent::CHANGE:
str.assign("Modification");
break;
default:
str.assign("");
break;
}
info.set_event(str);
string sg_uuid = UuidToString(GetSgUuid());
info.set_uuid(sg_uuid);
info.set_id(GetSgId());
if (GetEgressAcl()) {
info.set_egress_acl_uuid(UuidToString(GetEgressAcl()->GetUuid()));
}
if (GetIngressAcl()) {
info.set_ingress_acl_uuid(UuidToString(GetIngressAcl()->GetUuid()));
}
info.set_ref_count(GetRefCount());
SG_OBJECT_LOG_LOG("AgentSg", SandeshLevel::SYS_INFO, info);
SG_OBJECT_TRACE_TRACE(OperDBTraceBuf, info);
}
void SgListReq::HandleRequest() const {
AgentSandeshPtr sand(new AgentSgSandesh(context(), get_name()));
sand->DoSandesh(sand);
}
AgentSandeshPtr SgTable::GetAgentSandesh(const AgentSandeshArguments *args,
const std::string &context) {
return AgentSandeshPtr(new AgentSgSandesh(context,
args->GetString("name")));
}