-
Notifications
You must be signed in to change notification settings - Fork 390
/
evpn_route.cc
519 lines (459 loc) · 21.1 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* 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/ecmp_load_balance.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 <controller/controller_route_path.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_, is_multicast);
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::AddOvsPeerMulticastRouteInternal(const Peer *peer,
uint32_t vxlan_id,
const std::string &vn_name,
Ip4Address tsn,
Ip4Address tor_ip,
bool enqueue) {
const VrfEntry *vrf = vrf_entry();
DBRequest nh_req(DBRequest::DB_ENTRY_ADD_CHANGE);
nh_req.key.reset(new TunnelNHKey(vrf->GetName(), tsn, tor_ip,
false, TunnelType::VXLAN));
nh_req.data.reset(new TunnelNHData());
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer,
vrf->GetName(),
MacAddress::BroadcastMac(),
tor_ip,
vxlan_id));
req.data.reset(new MulticastRoute(vn_name, 0, vxlan_id,
TunnelType::VxlanType(),
nh_req, Composite::L2COMP));
if (enqueue) {
EvpnTableEnqueue(agent(), &req);
} else {
EvpnTableProcess(agent(), vrf_name(), req);
}
}
void EvpnAgentRouteTable::AddOvsPeerMulticastRoute(const Peer *peer,
uint32_t vxlan_id,
const std::string &vn_name,
Ip4Address tsn,
Ip4Address tor_ip) {
AddOvsPeerMulticastRouteInternal(peer, vxlan_id, vn_name, tsn, tor_ip, false);
}
void EvpnAgentRouteTable::AddOvsPeerMulticastRouteReq(const Peer *peer,
uint32_t vxlan_id,
const std::string &vn_name,
Ip4Address tsn,
Ip4Address tor_ip) {
AddOvsPeerMulticastRouteInternal(peer, vxlan_id, vn_name, tsn, tor_ip, true);
}
void EvpnAgentRouteTable::AddControllerReceiveRouteReq(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,
const PathPreference &path_pref) {
const BgpPeer *bgp_peer = dynamic_cast<const BgpPeer *>(peer);
assert(bgp_peer != NULL);
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new EvpnRouteKey(peer, vrf_name, mac, ip_addr,
ethernet_tag));
req.data.reset(new ControllerL2ReceiveRoute(vn_name, ethernet_tag,
label, path_pref,
bgp_peer->GetBgpXmppPeerConst()->
unicast_sequence_number(),
bgp_peer->GetBgpXmppPeerConst()));
agent()->fabric_evpn_table()->Enqueue(&req);
}
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,
const PathPreference &pref) {
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, pref));
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,
const PathPreference &pref) {
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, pref));
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(), "");
VnListType vn_list;
vn_list.insert(vn_name);
LocalVmRoute *data = new LocalVmRoute(intf_key, label,
intf->vxlan_id(), false,
vn_list,
InterfaceNHFlags::BRIDGE,
sg_id_list, CommunityList(),
path_pref,
IpAddress(),
EcmpLoadBalance());
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::DeleteOvsPeerMulticastRouteInternal(const Peer *peer,
uint32_t vxlan_id,
const Ip4Address &tor_ip,
bool enqueue) {
const VrfEntry *vrf = vrf_entry();
DBRequest req(DBRequest::DB_ENTRY_DELETE);
req.key.reset(new EvpnRouteKey(peer,
vrf->GetName(),
MacAddress::BroadcastMac(),
tor_ip,
vxlan_id));
req.data.reset(NULL);
if (enqueue) {
EvpnTableEnqueue(agent(), &req);
} else {
EvpnTableProcess(agent(), vrf->GetName(), req);
}
}
void EvpnAgentRouteTable::DeleteOvsPeerMulticastRouteReq(const Peer *peer,
uint32_t vxlan_id,
const Ip4Address &tor_ip) {
DeleteOvsPeerMulticastRouteInternal(peer, vxlan_id, tor_ip, true);
}
void EvpnAgentRouteTable::DeleteOvsPeerMulticastRoute(const Peer *peer,
uint32_t vxlan_id,
const Ip4Address &tor_ip) {
DeleteOvsPeerMulticastRouteInternal(peer, vxlan_id, tor_ip, false);
}
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) {
EvpnRouteEntry *evpn_rt = dynamic_cast<EvpnRouteEntry *>(entry);
if (evpn_rt->publish_to_bridge_route_table()) {
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) {
EvpnRouteEntry *evpn_rt = dynamic_cast<EvpnRouteEntry *>(entry);
if (evpn_rt->publish_to_bridge_route_table()) {
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,
bool is_multicast) :
AgentRoute(vrf, is_multicast), mac_(mac), ip_addr_(ip_addr),
ethernet_tag_(ethernet_tag),
publish_to_inet_route_table_(true),
publish_to_bridge_route_table_(true) {
}
string EvpnRouteEntry::ToString() const {
std::stringstream str;
if (is_multicast()) {
str << mac_.ToString();
} else {
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();
}
const AgentPath *EvpnRouteEntry::FindOvsPath() const {
for(Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
const AgentPath *path = static_cast<const AgentPath *>(it.operator->());
if (path->peer()->GetType() == Peer::OVS_PEER) {
return const_cast<AgentPath *>(path);
}
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// 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;
}