-
Notifications
You must be signed in to change notification settings - Fork 390
/
mirror_table.cc
371 lines (308 loc) · 11.8 KB
/
mirror_table.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/bind.hpp>
#include <base/logging.h>
#include <db/db.h>
#include <db/db_entry.h>
#include <db/db_table.h>
#include <cmn/agent_cmn.h>
#include <init/agent_param.h>
#include "oper/route_common.h"
#include "oper/nexthop.h"
#include "oper/tunnel_nh.h"
#include "oper/vrf.h"
#include "oper/mirror_table.h"
#include "oper/agent_sandesh.h"
using namespace std;
using namespace boost::asio;
MirrorTable *MirrorTable::mirror_table_;
MirrorTable::~MirrorTable() {
boost::system::error_code err;
if (udp_sock_.get()) {
udp_sock_->close(err);
}
}
bool MirrorEntry::IsLess(const DBEntry &rhs) const {
const MirrorEntry &a = static_cast<const MirrorEntry &>(rhs);
return (analyzer_name_ < a.GetAnalyzerName());
}
DBEntryBase::KeyPtr MirrorEntry::GetDBRequestKey() const {
MirrorEntryKey *key = new MirrorEntryKey(analyzer_name_);
return DBEntryBase::KeyPtr(key);
}
void MirrorEntry::SetKey(const DBRequestKey *k) {
const MirrorEntryKey *key = static_cast<const MirrorEntryKey *>(k);
analyzer_name_ = key->analyzer_name_;
}
std::auto_ptr<DBEntry> MirrorTable::AllocEntry(const DBRequestKey *k) const {
const MirrorEntryKey *key = static_cast<const MirrorEntryKey *>(k);
MirrorEntry *mirror_entry = new MirrorEntry(key->analyzer_name_);
return std::auto_ptr<DBEntry>(static_cast<DBEntry *>(mirror_entry));
}
DBEntry *MirrorTable::Add(const DBRequest *req) {
const MirrorEntryKey *key = static_cast<const MirrorEntryKey *>(req->key.get());
MirrorEntry *mirror_entry = new MirrorEntry(key->analyzer_name_);
//Get Mirror NH
OnChange(mirror_entry, req);
return mirror_entry;
}
bool MirrorTable::OnChange(DBEntry *entry, const DBRequest *req) {
bool ret = false;
MirrorEntry *mirror_entry = static_cast<MirrorEntry *>(entry);
MirrorEntryData *data = static_cast<MirrorEntryData *>(req->data.get());
mirror_entry->vrf_name_ = data->vrf_name_;
mirror_entry->sip_ = data->sip_;
mirror_entry->sport_ = data->sport_;
mirror_entry->dip_ = data->dip_;
mirror_entry->dport_ = data->dport_;
DBRequest nh_req;
nh_req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MirrorNHKey *nh_key = new MirrorNHKey(data->vrf_name_, data->sip_,
data->sport_, data->dip_, data->dport_);
nh_req.key.reset(nh_key);
nh_req.data.reset(NULL);
agent()->nexthop_table()->Process(nh_req);
/* For service-chain based mirroring, vrf will always be empty. In this
* case we should create MirrorNH even when VRF is NULL */
bool check_for_vrf = false;
VrfEntry *vrf = NULL;
if (!data->vrf_name_.empty()) {
VrfKey key(data->vrf_name_);
vrf = static_cast<VrfEntry *>(agent()->vrf_table()->
FindActiveEntry(&key));
check_for_vrf = true;
}
NextHop *nh = static_cast<NextHop *>
(agent()->nexthop_table()->FindActiveEntry(nh_key));
if (nh == NULL || (check_for_vrf && vrf == NULL)) {
//Make the mirror NH point to discard
//and change the nexthop once the VRF is
//available
AddUnresolved(mirror_entry);
DiscardNH key;
nh = static_cast<NextHop *>
(agent()->nexthop_table()->FindActiveEntry(&key));
} else {
AddResolvedVrfMirrorEntry(mirror_entry);
}
if (mirror_entry->nh_ != nh) {
mirror_entry->nh_ = nh;
mirror_entry->vrf_ =
agent()->vrf_table()->FindVrfFromName(data->vrf_name_);
ret = true;
}
return ret;
}
bool MirrorTable::Delete(DBEntry *entry, const DBRequest *request) {
MirrorEntry *mirror_entry = static_cast<MirrorEntry *>(entry);
RemoveUnresolved(mirror_entry);
DeleteResolvedVrfMirrorEntry(mirror_entry);
return true;
}
void MirrorTable::Add(VrfMirrorEntryList &vrf_entry_map, MirrorEntry *entry) {
VrfMirrorEntryList::iterator it = vrf_entry_map.find(entry->vrf_name_);
if (it != vrf_entry_map.end()) {
MirrorEntryList::const_iterator list_it = it->second.begin();
for (; list_it != it->second.end(); list_it++) {
if (*list_it == entry) {
//Entry already present
return;
}
}
it->second.push_back(entry);
return;
}
MirrorEntryList list;
list.push_back(entry);
vrf_entry_map.insert(VrfMirrorEntry(entry->vrf_name_, list));
}
void MirrorTable::Delete(VrfMirrorEntryList &list, MirrorEntry *entry) {
VrfMirrorEntryList::iterator it = list.find(entry->vrf_name_);
if (it == list.end()) {
return;
}
MirrorEntryList::iterator list_it = it->second.begin();
for(;list_it != it->second.end(); list_it++) {
if (*list_it == entry) {
it->second.erase(list_it);
break;
}
}
}
void MirrorTable::ResyncMirrorEntry(VrfMirrorEntryList &list,
const VrfEntry *vrf) {
VrfMirrorEntryList::iterator it = list.find(vrf->GetName());
if (it == list.end()) {
return;
}
MirrorEntryList::iterator list_it = it->second.begin();
for(;list_it != it->second.end(); list_it++) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MirrorEntryKey *key = new MirrorEntryKey((*list_it)->GetAnalyzerName());
key->sub_op_ = AgentKey::RESYNC;
MirrorEntryData *data = new MirrorEntryData((*list_it)->vrf_name(),
*((*list_it)->GetSip()),
(*list_it)->GetSPort(),
*((*list_it)->GetDip()),
(*list_it)->GetDPort());
req.key.reset(key);
req.data.reset(data);
Enqueue(&req);
}
list.erase(it);
}
void MirrorTable::AddUnresolved(MirrorEntry *entry) {
Add(unresolved_entry_list_, entry);
}
void MirrorTable::RemoveUnresolved(MirrorEntry *entry) {
Delete(unresolved_entry_list_, entry);
}
void MirrorTable::AddResolvedVrfMirrorEntry(MirrorEntry *entry) {
Add(resolved_entry_list_, entry);
}
void MirrorTable::DeleteResolvedVrfMirrorEntry(MirrorEntry *entry) {
Delete(resolved_entry_list_, entry);
}
void MirrorTable::ResyncResolvedMirrorEntry(const VrfEntry *vrf) {
ResyncMirrorEntry(resolved_entry_list_, vrf);
}
void MirrorTable::ResyncUnresolvedMirrorEntry(const VrfEntry *vrf) {
ResyncMirrorEntry(unresolved_entry_list_, vrf);
}
void MirrorTable::AddMirrorEntry(const std::string &analyzer_name,
const std::string &vrf_name,
const IpAddress &sip, uint16_t sport,
const IpAddress &dip, uint16_t dport) {
DBRequest req;
if (dip.is_v6() && vrf_name == mirror_table_->agent()->fabric_vrf_name()) {
LOG(ERROR, "Ipv6 as destination not supported on Fabric VRF: " <<
dip.to_string());
return;
}
// First enqueue request to add Mirror NH
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MirrorNHKey *nh_key = new MirrorNHKey(vrf_name, sip, sport, dip, dport);
req.key.reset(nh_key);
req.data.reset(NULL);
mirror_table_->agent()->nexthop_table()->Enqueue(&req);
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
MirrorEntryKey *key = new MirrorEntryKey(analyzer_name);
MirrorEntryData *data = new MirrorEntryData(vrf_name, sip,
sport, dip, dport);
req.key.reset(key);
req.data.reset(data);
mirror_table_->Enqueue(&req);
}
void MirrorTable::DelMirrorEntry(const std::string &analyzer_name) {
DBRequest req;
req.oper = DBRequest::DB_ENTRY_DELETE;
MirrorEntryKey *key = new MirrorEntryKey(analyzer_name);
req.key.reset(key);
req.data.reset(NULL);
mirror_table_->Enqueue(&req);
}
void MirrorTable::OnZeroRefcount(AgentDBEntry *e) {
const MirrorEntry *mirr_entry = static_cast<const MirrorEntry *>(e);
DelMirrorEntry(mirr_entry->GetAnalyzerName());
}
DBTableBase *MirrorTable::CreateTable(DB *db, const std::string &name) {
mirror_table_ = new MirrorTable(db, name);
mirror_table_->Init();
return mirror_table_;
};
void MirrorTable::Initialize() {
VrfListenerInit();
}
void MirrorTable::VrfListenerInit() {
vrf_listener_id_ = agent()->vrf_table()->
Register(boost::bind(&MirrorTable::VrfNotify,
this, _1, _2));
}
void MirrorTable::VrfNotify(DBTablePartBase *base, DBEntryBase *entry) {
const VrfEntry *vrf = static_cast<VrfEntry *>(entry);
if (vrf->IsDeleted()) {
//VRF is getting deleted remove all the mirror nexthop
ResyncResolvedMirrorEntry(vrf);
return;
}
ResyncUnresolvedMirrorEntry(vrf);
}
void MirrorTable::ReadHandler(const boost::system::error_code &ec,
size_t bytes_transferred) {
if (ec) {
LOG(ERROR, "Error reading from Mirror sock. Error : " <<
boost::system::system_error(ec).what());
return;
}
udp_sock_->async_receive(boost::asio::buffer(rx_buff_, sizeof(rx_buff_)),
boost::bind(&MirrorTable::ReadHandler, this,
placeholders::error,
placeholders::bytes_transferred));
}
void MirrorTable::MirrorSockInit(void) {
EventManager *event_mgr;
event_mgr = agent()->event_manager();
boost::asio::io_service &io = *event_mgr->io_service();
ip::udp::endpoint ep(ip::udp::v4(),
agent()->params()->mirror_client_port());
udp_sock_.reset(new ip::udp::socket(io));
boost::system::error_code ec;
udp_sock_->open(ip::udp::v4(), ec);
assert(ec.value() == 0);
udp_sock_->bind(ep, ec);
if (ec.value() != 0) {
ep.port(0);
udp_sock_->bind(ep, ec);
assert(ec.value() == 0);
}
ip::udp::endpoint sock_ep = udp_sock_->local_endpoint(ec);
assert(ec.value() == 0);
agent()->set_mirror_port(sock_ep.port());
udp_sock_->async_receive(boost::asio::buffer(rx_buff_, sizeof(rx_buff_)),
boost::bind(&MirrorTable::ReadHandler, this,
placeholders::error,
placeholders::bytes_transferred));
}
VrfEntry *MirrorTable::FindVrfEntry(const string &vrf_name) const {
return agent()->vrf_table()->FindVrfFromName(vrf_name);
}
void MirrorTable::Shutdown() {
agent()->vrf_table()->Unregister(vrf_listener_id_);
}
uint32_t MirrorEntry::vrf_id() const {
return vrf_ ? vrf_->vrf_id() : uint32_t(-1);
}
const VrfEntry *MirrorEntry::GetVrf() const {
return vrf_ ? vrf_.get() : NULL;
}
void MirrorEntry::set_mirror_entrySandeshData(MirrorEntrySandeshData &data) const {
data.set_analyzer_name(GetAnalyzerName());
data.set_sip(GetSip()->to_string());
data.set_dip(GetDip()->to_string());
data.set_vrf(GetVrf() ? GetVrf()->GetName() : "");
data.set_sport(GetSPort());
data.set_dport(GetDPort());
data.set_ref_count(GetRefCount());
nh_->SetNHSandeshData(data.nh);
}
bool MirrorEntry::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
MirrorEntryResp *resp = static_cast<MirrorEntryResp *>(sresp);
MirrorEntrySandeshData data;
set_mirror_entrySandeshData(data);
std::vector<MirrorEntrySandeshData> &list =
const_cast<std::vector<MirrorEntrySandeshData>&>
(resp->get_mirror_entry_list());
list.push_back(data);
return true;
}
void MirrorEntryReq::HandleRequest() const {
AgentSandeshPtr sand(new AgentMirrorSandesh(context(), get_analyzer_name()));
sand->DoSandesh(sand);
}
AgentSandeshPtr MirrorTable::GetAgentSandesh(const AgentSandeshArguments *args,
const std::string &context) {
return AgentSandeshPtr(new AgentMirrorSandesh(context,
args->GetString("analyzer_name")));
}