-
Notifications
You must be signed in to change notification settings - Fork 390
/
evpn_route.cc
390 lines (340 loc) · 15 KB
/
evpn_route.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/uuid/uuid_io.hpp>
#include <cmn/agent_cmn.h>
#include <route/route.h>
#include <oper/route_common.h>
#include <oper/vrf.h>
#include <oper/tunnel_nh.h>
#include <oper/mpls.h>
#include <oper/mirror_table.h>
#include <controller/controller_export.h>
#include <controller/controller_peer.h>
#include <oper/agent_sandesh.h>
#include <pkt/pkt_init.h>
#include <pkt/pkt_handler.h>
using namespace std;
using namespace boost::asio;
/////////////////////////////////////////////////////////////////////////////
// Utility functions
/////////////////////////////////////////////////////////////////////////////
static void EvpnTableEnqueue(Agent *agent, DBRequest *req) {
AgentRouteTable *table = agent->fabric_evpn_table();
if (table) {
table->Enqueue(req);
}
}
static void EvpnTableProcess(Agent *agent, const string &vrf_name,
DBRequest &req) {
AgentRouteTable *table =
agent->vrf_table()->GetEvpnRouteTable(vrf_name);
if (table) {
table->Process(req);
}
}
/////////////////////////////////////////////////////////////////////////////
// EvpnRouteKey methods
/////////////////////////////////////////////////////////////////////////////
string EvpnRouteKey::ToString() const {
std::stringstream str;
str << ethernet_tag_;
str << "-";
str << dmac_.ToString();
str << "-";
str << ip_addr_.to_string();
return str.str();
}
EvpnRouteKey *EvpnRouteKey::Clone() const {
return new EvpnRouteKey(peer_, vrf_name_, dmac_, ip_addr_,
ethernet_tag_);
}
AgentRoute *
EvpnRouteKey::AllocRouteEntry(VrfEntry *vrf, bool is_multicast) const
{
EvpnRouteEntry *entry = new EvpnRouteEntry(vrf, dmac_, ip_addr_,
ethernet_tag_);
return static_cast<AgentRoute *>(entry);
}
/////////////////////////////////////////////////////////////////////////////
// EvpnAgentRouteTable methods
/////////////////////////////////////////////////////////////////////////////
DBTableBase *EvpnAgentRouteTable::CreateTable(DB *db,
const std::string &name) {
AgentRouteTable *table = new EvpnAgentRouteTable(db, name);
table->Init();
return table;
}
EvpnRouteEntry *EvpnAgentRouteTable::FindRoute(const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag) {
EvpnRouteKey key(NULL, vrf_name(), mac, ip_addr, ethernet_tag);
EvpnRouteEntry *route =
static_cast<EvpnRouteEntry *>(FindActiveEntry(&key));
return route;
}
EvpnRouteEntry *EvpnAgentRouteTable::FindRoute(const Agent *agent,
const string &vrf_name,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag) {
VrfEntry *vrf = agent->vrf_table()->FindVrfFromName(vrf_name);
if (vrf == NULL)
return NULL;
EvpnAgentRouteTable *table = static_cast<EvpnAgentRouteTable *>
(vrf->GetEvpnRouteTable());
return table->FindRoute(mac, ip_addr, ethernet_tag);
}
/////////////////////////////////////////////////////////////////////////////
// EvpnAgentRouteTable utility methods to add/delete routes
/////////////////////////////////////////////////////////////////////////////
void EvpnAgentRouteTable::AddReceiveRouteReq(const Peer *peer,
const string &vrf_name,
uint32_t label,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag,
const string &vn_name) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
req.data.reset(new L2ReceiveRoute(vn_name, ethernet_tag, label));
agent()->fabric_evpn_table()->Enqueue(&req);
}
void EvpnAgentRouteTable::AddReceiveRoute(const Peer *peer,
const string &vrf_name,
uint32_t label,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag,
const string &vn_name) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
req.data.reset(new L2ReceiveRoute(vn_name, ethernet_tag, label));
Process(req);
}
void EvpnAgentRouteTable::AddLocalVmRouteReq(const Peer *peer,
const string &vrf_name,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag,
LocalVmRoute *data) {
assert(peer);
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
data->set_tunnel_bmap(TunnelType::AllType());
req.data.reset(data);
EvpnTableEnqueue(Agent::GetInstance(), &req);
}
void EvpnAgentRouteTable::AddLocalVmRoute(const Peer *peer,
const string &vrf_name,
const MacAddress &mac,
const VmInterface *intf,
const IpAddress &ip,
uint32_t label,
const string &vn_name,
const SecurityGroupList &sg_id_list,
const PathPreference &path_pref,
uint32_t ethernet_tag) {
assert(peer);
Agent *agent = static_cast<AgentDBTable *>(intf->get_table())->agent();
VmInterfaceKey intf_key(AgentKey::ADD_DEL_CHANGE, intf->GetUuid(), "");
LocalVmRoute *data = new LocalVmRoute(intf_key, label,
intf->vxlan_id(), false,
vn_name,
InterfaceNHFlags::BRIDGE,
sg_id_list, path_pref,
IpAddress());
data->set_tunnel_bmap(TunnelType::AllType());
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip, ethernet_tag));
req.data.reset(data);
EvpnTableProcess(agent, vrf_name, req);
}
void EvpnAgentRouteTable::DelLocalVmRoute(const Peer *peer,
const string &vrf_name,
const MacAddress &mac,
const VmInterface *intf,
const IpAddress &ip,
uint32_t ethernet_tag) {
assert(peer);
Agent *agent = static_cast<AgentDBTable *>(intf->get_table())->agent();
DBRequest req(DBRequest::DB_ENTRY_DELETE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, IpAddress(ip),
ethernet_tag));
req.data.reset(NULL);
EvpnTableProcess(agent, vrf_name, req);
}
void EvpnAgentRouteTable::ResyncVmRoute(const Peer *peer,
const string &vrf_name,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag,
AgentRouteData *data) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
EvpnRouteKey *key = new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag);
key->sub_op_ = AgentKey::RESYNC;
req.key.reset(key);
req.data.reset(data);
EvpnTableProcess(Agent::GetInstance(), vrf_name, req);
}
void EvpnAgentRouteTable::AddRemoteVmRouteReq(const Peer *peer,
const string &vrf_name,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag,
AgentRouteData *data) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
req.data.reset(data);
EvpnTableEnqueue(Agent::GetInstance(), &req);
}
void EvpnAgentRouteTable::AddRemoteVmRoute(const Peer *peer,
const string &vrf_name,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag,
AgentRouteData *data) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
req.data.reset(data);
EvpnTableProcess(Agent::GetInstance(), vrf_name, req);
}
void EvpnAgentRouteTable::DeleteReq(const Peer *peer, const string &vrf_name,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag,
AgentRouteData *data) {
DBRequest req(DBRequest::DB_ENTRY_DELETE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
req.data.reset(data);
EvpnTableEnqueue(Agent::GetInstance(), &req);
}
void EvpnAgentRouteTable::Delete(const Peer *peer, const string &vrf_name,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag) {
DBRequest req(DBRequest::DB_ENTRY_DELETE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
req.data.reset(NULL);
EvpnTableProcess(Agent::GetInstance(), vrf_name, req);
}
//Notify L2 route corresponding to MAC in evpn route.
void EvpnAgentRouteTable::UpdateDependants(AgentRoute *entry) {
BridgeAgentRouteTable *table = static_cast<BridgeAgentRouteTable *>
(vrf_entry()->GetBridgeRouteTable());
table->AddBridgeRoute(entry);
}
//Delete path from L2 route corresponding to MAC+IP in evpn route.
void EvpnAgentRouteTable::PreRouteDelete(AgentRoute *entry) {
BridgeAgentRouteTable *table = static_cast<BridgeAgentRouteTable *>
(vrf_entry()->GetBridgeRouteTable());
table->DeleteBridgeRoute(entry);
}
/////////////////////////////////////////////////////////////////////////////
// EvpnRouteEntry methods
/////////////////////////////////////////////////////////////////////////////
EvpnRouteEntry::EvpnRouteEntry(VrfEntry *vrf,
const MacAddress &mac,
const IpAddress &ip_addr,
uint32_t ethernet_tag) :
AgentRoute(vrf, false), mac_(mac), ip_addr_(ip_addr),
ethernet_tag_(ethernet_tag) {
}
string EvpnRouteEntry::ToString() const {
std::stringstream str;
str << ethernet_tag_;
str << "-";
str << mac_.ToString();
str << "-";
str << ip_addr_.to_string();
return str.str();
}
int EvpnRouteEntry::CompareTo(const Route &rhs) const {
const EvpnRouteEntry &a = static_cast<const EvpnRouteEntry &>(rhs);
if (ethernet_tag_ < a.ethernet_tag_)
return -1;
if (ethernet_tag_ > a.ethernet_tag_)
return 1;
int cmp = mac_.CompareTo(a.mac_);
if (cmp != 0)
return cmp;
if (ip_addr_ < a.ip_addr_)
return -1;
if (ip_addr_ > a.ip_addr_)
return 1;
return 0;
}
DBEntryBase::KeyPtr EvpnRouteEntry::GetDBRequestKey() const {
EvpnRouteKey *key =
new EvpnRouteKey(Agent::GetInstance()->local_vm_peer(),
vrf()->GetName(), mac_, ip_addr_,
ethernet_tag_);
return DBEntryBase::KeyPtr(key);
}
void EvpnRouteEntry::SetKey(const DBRequestKey *key) {
const EvpnRouteKey *k = static_cast<const EvpnRouteKey *>(key);
SetVrf(Agent::GetInstance()->vrf_table()->FindVrfFromName(k->vrf_name()));
mac_ = k->GetMac();
ip_addr_ = k->ip_addr();
ethernet_tag_ = k->ethernet_tag();
}
const uint32_t EvpnRouteEntry::GetVmIpPlen() const {
if (ip_addr_.is_v4())
return 32;
if (ip_addr_.is_v6())
return 128;
assert(0);
}
uint32_t EvpnRouteEntry::GetActiveLabel() const {
return GetActivePath()->GetActiveLabel();
}
/////////////////////////////////////////////////////////////////////////////
// Sandesh related methods
/////////////////////////////////////////////////////////////////////////////
void EvpnRouteReq::HandleRequest() const {
VrfEntry *vrf =
Agent::GetInstance()->vrf_table()->FindVrfFromId(get_vrf_index());
if (!vrf) {
ErrorResp *resp = new ErrorResp();
resp->set_context(context());
resp->Response();
return;
}
AgentSandeshPtr sand(new AgentEvpnRtSandesh(vrf, context(), "",
get_stale()));
sand->DoSandesh(sand);
}
AgentSandeshPtr EvpnAgentRouteTable::GetAgentSandesh
(const AgentSandeshArguments *args, const std::string &context) {
return AgentSandeshPtr(new AgentEvpnRtSandesh(vrf_entry(), context, "",
false));
}
bool EvpnRouteEntry::DBEntrySandesh(Sandesh *sresp, bool stale) const {
EvpnRouteResp *resp = static_cast<EvpnRouteResp *>(sresp);
RouteEvpnSandeshData data;
data.set_mac(ToString());
data.set_ip_addr(ip_addr_.to_string());
for (Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
const AgentPath *path = static_cast<const AgentPath *>(it.operator->());
if (path) {
if (stale && !path->is_stale())
continue;
PathSandeshData pdata;
path->SetSandeshData(pdata);
data.path_list.push_back(pdata);
}
}
std::vector<RouteEvpnSandeshData> &list =
const_cast<std::vector<RouteEvpnSandeshData>&>(resp->get_route_list());
list.push_back(data);
return true;
}