/
bgp_route.cc
423 lines (374 loc) · 13.9 KB
/
bgp_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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_route.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include "bgp/bgp_attr.h"
#include "bgp/bgp_path.h"
#include "bgp/bgp_peer_types.h"
#include "bgp/bgp_table.h"
#include "bgp/extended-community/default_gateway.h"
#include "bgp/extended-community/es_import.h"
#include "bgp/extended-community/esi_label.h"
#include "bgp/extended-community/mac_mobility.h"
#include "bgp/extended-community/site_of_origin.h"
#include "bgp/origin-vn/origin_vn.h"
#include "bgp/routing-instance/routing_instance.h"
#include "bgp/security_group/security_group.h"
#include "bgp/tunnel_encap/tunnel_encap.h"
using std::string;
using std::vector;
BgpRoute::BgpRoute() {
}
BgpRoute::~BgpRoute() {
assert(GetPathList().empty());
}
//
// Return the best path for this route.
//
const BgpPath *BgpRoute::BestPath() const {
const BgpPath *path = static_cast<const BgpPath *>(front());
return path;
}
//
// Insert given path and redo path selection.
//
void BgpRoute::InsertPath(BgpPath *path) {
assert(!IsDeleted());
const Path *prev_front = front();
insert(path);
Sort(&BgpTable::PathSelection, prev_front);
// Update counters.
BgpTable *table = static_cast<BgpTable *>(get_table());
if (table) table->UpdatePathCount(path, +1);
path->UpdatePeerRefCount(+1);
}
//
// Delete given path and redo path selection.
//
void BgpRoute::DeletePath(BgpPath *path) {
const Path *prev_front = front();
remove(path);
Sort(&BgpTable::PathSelection, prev_front);
// Update counters.
BgpTable *table = static_cast<BgpTable *>(get_table());
if (table) table->UpdatePathCount(path, -1);
path->UpdatePeerRefCount(-1);
delete path;
}
//
// Find path added by peer with given path id and path source.
// Skips secondary paths.
//
BgpPath *BgpRoute::FindPath(BgpPath::PathSource src, const IPeer *peer,
uint32_t path_id) {
for (Route::PathList::iterator it = GetPathList().begin();
it != GetPathList().end(); ++it) {
// Skip secondary paths.
if (dynamic_cast<BgpSecondaryPath *>(it.operator->())) {
continue;
}
BgpPath *path = static_cast<BgpPath *>(it.operator->());
if (path->GetPeer() == peer && path->GetPathId() == path_id &&
path->GetSource() == src) {
return path;
}
}
return NULL;
}
//
// Remove path added by peer with given path id and source.
// Skips secondary paths.
// Return true if the path is found and removed, false otherwise.
//
bool BgpRoute::RemovePath(BgpPath::PathSource src, const IPeer *peer,
uint32_t path_id) {
for (Route::PathList::iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
BgpPath *path = static_cast<BgpPath *>(it.operator->());
//
// Skip secondary paths.
//
if (dynamic_cast<BgpSecondaryPath *>(it.operator->())) {
continue;
}
if (path->GetPeer() == peer && path->GetPathId() == path_id &&
path->GetSource() == src) {
DeletePath(path);
return true;
}
}
return false;
}
//
// Remove path added by peer with given path id and source.
// Skips secondary paths.
// Return true if the path is found and removed, false otherwise.
//
bool BgpRoute::RemovePath(const IPeer *peer) {
bool ret = false;
for (Route::PathList::iterator it = GetPathList().begin(), next = it;
it != GetPathList().end(); it = next) {
next++;
BgpPath *path = static_cast<BgpPath *>(it.operator->());
//
// Skip secondary paths.
//
if (dynamic_cast<BgpSecondaryPath *>(it.operator->())) {
continue;
}
if (path->GetPeer() == peer) {
DeletePath(path);
ret = true;
}
}
return ret;
}
//
// Check if the route is usable.
//
bool BgpRoute::IsUsable() const {
if (IsDeleted())
return false;
const BgpPath *path = BestPath();
if (!path || !path->IsFeasible())
return false;
return true;
}
//
// Check if the route is valid.
//
bool BgpRoute::IsValid() const {
return IsUsable();
}
//
// Check if there's a better path with the same forwarding information.
// The forwarding information we look at is the label and the next hop.
// Return true if we find such a path, false otherwise.
//
bool BgpRoute::DuplicateForwardingPath(const BgpPath *in_path) const {
for (Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); ++it) {
const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
// Bail if we reached the input path since the paths are sorted.
if (path == in_path)
return false;
// Check the forwarding information.
if ((path->GetAttr()->nexthop() == in_path->GetAttr()->nexthop()) &&
(path->GetLabel() == in_path->GetLabel())) {
return true;
}
}
return false;
}
//
// Find the secondary path matching secondary replicated info.
//
BgpPath *BgpRoute::FindSecondaryPath(BgpRoute *src_rt,
BgpPath::PathSource src, const IPeer *peer, uint32_t path_id) {
for (Route::PathList::iterator it = GetPathList().begin();
it != GetPathList().end(); ++it) {
BgpSecondaryPath *path = dynamic_cast<BgpSecondaryPath *>(
it.operator->());
if (path && path->src_rt() == src_rt &&
path->GetPeer() == peer && path->GetPathId() == path_id &&
path->GetSource() == src) {
return path;
}
}
return NULL;
}
//
// Remove the secondary path matching secondary replicated info.
// Return true if the path is found and removed, false otherwise.
//
bool BgpRoute::RemoveSecondaryPath(const BgpRoute *src_rt,
BgpPath::PathSource src, const IPeer *peer, uint32_t path_id) {
for (Route::PathList::iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
BgpSecondaryPath *path =
dynamic_cast<BgpSecondaryPath *>(it.operator->());
if (path && path->src_rt() == src_rt &&
path->GetPeer() == peer && path->GetPathId() == path_id &&
path->GetSource() == src) {
DeletePath(path);
return true;
}
}
return false;
}
size_t BgpRoute::count() const {
return GetPathList().size();
}
void BgpRoute::FillRouteInfo(const BgpTable *table,
ShowRouteBrief *show_route) const {
show_route->set_prefix(ToString());
vector<ShowRoutePathBrief> show_route_paths;
for (Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); ++it) {
const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
ShowRoutePathBrief srp;
const IPeer *peer = path->GetPeer();
if (peer) {
srp.set_source(peer->ToString());
}
if (path->GetSource() == BgpPath::BGP_XMPP) {
if (peer) {
srp.set_protocol(peer->IsXmppPeer() ? "XMPP" : "BGP");
} else {
srp.set_protocol("Local");
}
} else if (path->GetSource() == BgpPath::ServiceChain) {
srp.set_protocol("ServiceChain");
} else if (path->GetSource() == BgpPath::StaticRoute) {
srp.set_protocol("StaticRoute");
}
const BgpAttr *attr = path->GetAttr();
srp.set_local_preference(attr->local_pref());
srp.set_next_hop(attr->nexthop().to_string());
srp.set_label(path->GetLabel());
show_route_paths.push_back(srp);
}
show_route->set_paths(show_route_paths);
}
static void FillRoutePathExtCommunityInfo(const BgpTable *table,
const ExtCommunity *extcomm,
ShowRoutePath *show_path) {
const RoutingInstance *ri = table->routing_instance();
const RoutingInstanceMgr *ri_mgr = ri->manager();
const ExtCommunity::ExtCommunityList &v = extcomm->communities();
for (ExtCommunity::ExtCommunityList::const_iterator it = v.begin();
it != v.end(); ++it) {
if (ExtCommunity::is_route_target(*it)) {
RouteTarget rt(*it);
show_path->communities.push_back(rt.ToString());
} else if (ExtCommunity::is_default_gateway(*it)) {
DefaultGateway dgw(*it);
show_path->communities.push_back(dgw.ToString());
} else if (ExtCommunity::is_es_import(*it)) {
EsImport es_import(*it);
show_path->communities.push_back(es_import.ToString());
} else if (ExtCommunity::is_esi_label(*it)) {
EsiLabel esi_label(*it);
show_path->communities.push_back(esi_label.ToString());
} else if (ExtCommunity::is_mac_mobility(*it)) {
MacMobility mm(*it);
show_path->communities.push_back(mm.ToString());
show_path->set_sequence_no(mm.ToString());
} else if (ExtCommunity::is_origin_vn(*it)) {
OriginVn origin_vn(*it);
show_path->communities.push_back(origin_vn.ToString());
int vn_index = origin_vn.vn_index();
show_path->set_origin_vn(
ri_mgr->GetVirtualNetworkByVnIndex(vn_index));
} else if (ExtCommunity::is_security_group(*it)) {
SecurityGroup sg(*it);
show_path->communities.push_back(sg.ToString());
} else if (ExtCommunity::is_route_target(*it)) {
SiteOfOrigin soo(*it);
show_path->communities.push_back(soo.ToString());
} else if (ExtCommunity::is_tunnel_encap(*it)) {
TunnelEncap encap(*it);
show_path->communities.push_back(encap.ToString());
TunnelEncapType::Encap id = encap.tunnel_encap();
show_path->tunnel_encap.push_back(
TunnelEncapType::TunnelEncapToString(id));
} else {
char temp[50];
int len = snprintf(temp, sizeof(temp), "ext community: ");
for (size_t i = 0; i < it->size(); i++) {
len += snprintf(temp+len, sizeof(temp) - len, "%02x", (*it)[i]);
}
show_path->communities.push_back(string(temp));
}
}
}
static void FillOriginVnPathInfo(const OriginVnPath *ovnpath,
ShowRoutePath *show_path) {
const OriginVnPath::OriginVnList &v = ovnpath->origin_vns();
for (OriginVnPath::OriginVnList::const_iterator it = v.begin();
it != v.end(); ++it) {
OriginVn origin_vn(*it);
show_path->origin_vn_path.push_back(origin_vn.ToString());
}
}
static void FillPmsiTunnelInfo(const PmsiTunnel *pmsi_tunnel, bool label_is_vni,
ShowRoutePath *show_path) {
ShowPmsiTunnel spt;
spt.set_type(pmsi_tunnel->pmsi_tunnel().GetTunnelTypeString());
spt.set_ar_type(pmsi_tunnel->pmsi_tunnel().GetTunnelArTypeString());
spt.set_identifier(pmsi_tunnel->identifier.to_string());
spt.set_label(pmsi_tunnel->GetLabel(label_is_vni));
spt.set_flags(pmsi_tunnel->pmsi_tunnel().GetTunnelFlagsStrings());
show_path->set_pmsi_tunnel(spt);
}
void BgpRoute::FillRouteInfo(const BgpTable *table,
ShowRoute *show_route) const {
const RoutingInstance *ri = table->routing_instance();
show_route->set_prefix(ToString());
show_route->set_last_modified(
integerToString(UTCUsecToPTime(last_change_at())));
vector<ShowRoutePath> show_route_paths;
for (Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); ++it) {
const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
ShowRoutePath srp;
const IPeer *peer = path->GetPeer();
if (peer) {
srp.set_source(peer->ToString());
}
if (path->GetSource() == BgpPath::BGP_XMPP) {
if (peer) {
srp.set_protocol(peer->IsXmppPeer() ? "XMPP" : "BGP");
} else {
srp.set_protocol("None");
}
} else if (path->GetSource() == BgpPath::ServiceChain) {
srp.set_protocol("ServiceChain");
} else if (path->GetSource() == BgpPath::StaticRoute) {
srp.set_protocol("StaticRoute");
} else if (path->GetSource() == BgpPath::Local) {
srp.set_protocol("Local");
}
const BgpAttr *attr = path->GetAttr();
if (attr->as_path() != NULL)
srp.set_as_path(attr->as_path()->path().ToString());
srp.set_local_preference(attr->local_pref());
srp.set_next_hop(attr->nexthop().to_string());
srp.set_label(path->GetLabel());
srp.set_flags(path->GetFlags());
srp.set_last_modified(
integerToString(UTCUsecToPTime(path->time_stamp_usecs())));
if (path->IsReplicated()) {
const BgpSecondaryPath *replicated;
replicated = static_cast<const BgpSecondaryPath *>(path);
srp.set_replicated(true);
srp.set_primary_table(replicated->src_table()->name());
} else {
srp.set_replicated(false);
}
if (attr->community()) {
CommunitySpec comm;
comm.communities = attr->community()->communities();
srp.communities.push_back(comm.ToString());
}
if (attr->ext_community()) {
FillRoutePathExtCommunityInfo(table, attr->ext_community(), &srp);
}
if (!table->IsVpnTable() && path->IsVrfOriginated()) {
srp.set_origin_vn(ri->GetVirtualNetworkName());
}
if (attr->origin_vn_path()) {
FillOriginVnPathInfo(attr->origin_vn_path(), &srp);
}
if (attr->pmsi_tunnel()) {
const ExtCommunity *extcomm = attr->ext_community();
bool label_is_vni = extcomm && extcomm->ContainsTunnelEncapVxlan();
FillPmsiTunnelInfo(attr->pmsi_tunnel(), label_is_vni, &srp);
}
show_route_paths.push_back(srp);
}
show_route->set_paths(show_route_paths);
}