Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "bgp/bgp_config_ifmap.h"
7 : #include "bgp/bgp_route.h"
8 :
9 : #include "bgp/bgp_peer.h"
10 : #include "bgp/bgp_server.h"
11 : #include "bgp/bgp_table.h"
12 : #include "bgp/extended-community/default_gateway.h"
13 : #include "bgp/extended-community/es_import.h"
14 : #include "bgp/extended-community/esi_label.h"
15 : #include "bgp/extended-community/etree.h"
16 : #include "bgp/extended-community/load_balance.h"
17 : #include "bgp/extended-community/mac_mobility.h"
18 : #include "bgp/extended-community/local_sequence_number.h"
19 : #include "bgp/extended-community/multicast_flags.h"
20 : #include "bgp/extended-community/router_mac.h"
21 : #include "bgp/extended-community/site_of_origin.h"
22 : #include "bgp/extended-community/source_as.h"
23 : #include "bgp/extended-community/sub_cluster.h"
24 : #include "bgp/extended-community/tag.h"
25 : #include "bgp/extended-community/vrf_route_import.h"
26 : #include "bgp/origin-vn/origin_vn.h"
27 : #include "bgp/routing-instance/routepath_replicator.h"
28 : #include "bgp/routing-instance/routing_instance.h"
29 : #include "bgp/security_group/security_group.h"
30 : #include "bgp/tunnel_encap/tunnel_encap.h"
31 : #include "bgp/large-community/tag.h"
32 :
33 : using std::string;
34 : using std::vector;
35 : using std::ostringstream;
36 :
37 145607 : BgpRoute::BgpRoute() {
38 145549 : }
39 :
40 145566 : BgpRoute::~BgpRoute() {
41 145566 : assert(GetPathList().empty());
42 145533 : }
43 :
44 : //
45 : // Return the best path for this route.
46 : // Skip aliased paths.
47 : //
48 333704 : const BgpPath *BgpRoute::BestPath() const {
49 333704 : for (Route::PathList::const_iterator it = GetPathList().begin();
50 666317 : it != GetPathList().end(); ++it) {
51 333022 : const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
52 333022 : if (path->GetFlags() & BgpPath::AliasedPath)
53 0 : continue;
54 333159 : return path;
55 : }
56 8 : return NULL;
57 : }
58 :
59 : //
60 : // Insert given path and redo path selection.
61 : //
62 39647 : void BgpRoute::InsertPath(BgpPath *path) {
63 39647 : assert(!IsDeleted());
64 39647 : const Path *prev_front = front();
65 :
66 39655 : BgpTable *table = static_cast<BgpTable *>(get_table());
67 : // 'table' is not expected to be null, suspect it is checked here because of
68 : // unit tests.
69 39654 : if (table) {
70 : // Default Tunnel Encapsulation processing is done if configured for
71 : // the address family on the peer.
72 : // If routing policy is supported on the table(family)it is handled in
73 : // ProcessRoutingPolicy where other changes may be made to the
74 : // attributes. Also, when there are routing policy configuration changes
75 : // the attributes needs to be re-evaluated using the original attributes
76 : // and Default Tunnel Encapsulation also needs to be re-applied, this
77 : // will be handled seamlessly since ProcessRoutingPolicy will be called
78 : // in that case.
79 : // Note that currently routing policies are not supported on VPN address
80 : // families, however this is ensuring that it will be handed correctly
81 : // in future. Additionally, for labeled inet routes, routing policies
82 : // are supported so the Default Tunnel Encapsulation needs to be handled
83 : // in ProcessRoutingPolicy.
84 : // If the table(family) does not support routing policies the Default
85 : // Tunnel Encapsulation processing is handled here. The original and
86 : // modified attributes are saved and are not expected to be modified
87 : // further. Note that the configuration is not applied if the path
88 : // already has tunnel encapsulation specified.
89 :
90 39654 : if (table->IsRoutingPolicySupported()) {
91 28348 : if (!path->IsReplicated()) {
92 : // Add sub-cluster extended community to all routes
93 : // originated within a sub-cluster
94 958 : uint32_t subcluster_id = SubClusterId();
95 956 : if (subcluster_id) {
96 0 : path->AddExtCommunitySubCluster(subcluster_id);
97 : }
98 : }
99 28344 : RoutingInstance *rtinstance = table->routing_instance();
100 28344 : rtinstance->ProcessRoutingPolicy(this, path);
101 : } else {
102 11302 : IPeer *peer = path->GetPeer();
103 11318 : if (peer) {
104 : // Take snapshot of original attribute
105 10786 : BgpAttr *out_attr = new BgpAttr(*(path->GetOriginalAttr()));
106 10790 : peer->ProcessPathTunnelEncapsulation(path, out_attr,
107 : table->server()->extcomm_db(), table);
108 : BgpAttrPtr modified_attr =
109 10783 : table->server()->attr_db()->Locate(out_attr);
110 : // Update the path with new set of attributes
111 10790 : path->SetAttr(modified_attr, path->GetOriginalAttr());
112 10790 : }
113 : }
114 : }
115 39671 : insert(path);
116 :
117 39665 : Sort(&BgpTable::PathSelection, prev_front);
118 :
119 : // Update counters.
120 39660 : if (table)
121 39660 : table->UpdatePathCount(path, +1);
122 39668 : path->UpdatePeerRefCount(+1, table ? table->family() : Address::UNSPEC);
123 39669 : }
124 :
125 : //
126 : // Delete given path and redo path selection.
127 : //
128 39598 : void BgpRoute::DeletePath(BgpPath *path) {
129 39598 : const Path *prev_front = front();
130 :
131 39574 : remove(path);
132 39625 : Sort(&BgpTable::PathSelection, prev_front);
133 :
134 : // Update counters.
135 39569 : BgpTable *table = static_cast<BgpTable *>(get_table());
136 39573 : if (table)
137 39572 : table->UpdatePathCount(path, -1);
138 39641 : path->UpdatePeerRefCount(-1, table ? table->family() : Address::UNSPEC);
139 :
140 39649 : delete path;
141 39650 : }
142 :
143 : //
144 : // Find first path with given path source.
145 : // Skips secondary, aliased and resolved paths.
146 : //
147 0 : const BgpPath *BgpRoute::FindPath(BgpPath::PathSource src) const {
148 0 : for (Route::PathList::const_iterator it = GetPathList().begin();
149 0 : it != GetPathList().end(); ++it) {
150 : // Skip secondary paths.
151 0 : if (dynamic_cast<const BgpSecondaryPath *>(it.operator->())) {
152 0 : continue;
153 : }
154 :
155 0 : const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
156 0 : if (path->GetFlags() & BgpPath::AliasedPath) {
157 0 : continue;
158 : }
159 0 : if (path->GetFlags() & BgpPath::ResolvedPath) {
160 0 : continue;
161 : }
162 0 : if (path->GetSource() == src) {
163 0 : return path;
164 : }
165 : }
166 0 : return NULL;
167 : }
168 :
169 : //
170 : // Find path added by BGP_XMPP peer.
171 : // Skips non BGP_XMPP, secondary, aliased and resolved paths.
172 : //
173 15064 : BgpPath *BgpRoute::FindPath(const IPeer *peer, bool include_secondary) {
174 15064 : for (Route::PathList::iterator it = GetPathList().begin();
175 32696 : it != GetPathList().end(); ++it) {
176 6148 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
177 6148 : if (path->GetSource() != BgpPath::BGP_XMPP) {
178 0 : continue;
179 : }
180 6150 : if (path->GetFlags() & BgpPath::AliasedPath) {
181 0 : continue;
182 : }
183 6150 : if (path->GetFlags() & BgpPath::ResolvedPath) {
184 0 : continue;
185 : }
186 12300 : if (!include_secondary &&
187 6150 : dynamic_cast<BgpSecondaryPath *>(it.operator->())) {
188 0 : continue;
189 : }
190 6150 : if (path->GetPeer() == peer) {
191 4864 : return path;
192 : }
193 : }
194 10200 : return NULL;
195 : }
196 :
197 0 : const BgpPath *BgpRoute::FindPath(const IPeer *peer,
198 : bool include_secondary) const {
199 0 : return const_cast<BgpRoute *>(this)->FindPath(peer, include_secondary);
200 : }
201 :
202 : //
203 : // Find path with given nexthop address.
204 : // Skips non BGP_XMPP, aliased and resolved paths.
205 : //
206 0 : BgpPath *BgpRoute::FindPath(const IpAddress &nexthop) {
207 0 : for (Route::PathList::iterator it = GetPathList().begin();
208 0 : it != GetPathList().end(); ++it) {
209 0 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
210 0 : if (path->GetSource() != BgpPath::BGP_XMPP) {
211 0 : continue;
212 : }
213 0 : if (path->IsResolved() || path->IsAliased()) {
214 0 : continue;
215 : }
216 0 : if (path->GetAttr()->nexthop() == nexthop) {
217 0 : return path;
218 : }
219 : }
220 0 : return NULL;
221 : }
222 :
223 : //
224 : // Find path added by peer with given path id and path source.
225 : // Skips secondary, aliased and resolved paths.
226 : //
227 20 : BgpPath *BgpRoute::FindPath(BgpPath::PathSource src, const IPeer *peer,
228 : uint32_t path_id) {
229 20 : for (Route::PathList::iterator it = GetPathList().begin();
230 40 : it != GetPathList().end(); ++it) {
231 : // Skip secondary paths.
232 0 : if (dynamic_cast<BgpSecondaryPath *>(it.operator->())) {
233 0 : continue;
234 : }
235 :
236 0 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
237 0 : if (path->GetFlags() & BgpPath::AliasedPath) {
238 0 : continue;
239 : }
240 0 : if (path->GetFlags() & BgpPath::ResolvedPath) {
241 0 : continue;
242 : }
243 0 : if (path->GetPeer() == peer && path->GetPathId() == path_id &&
244 0 : path->GetSource() == src) {
245 0 : return path;
246 : }
247 : }
248 20 : return NULL;
249 : }
250 :
251 : //
252 : // Find path added given source and path id - peer must be NULL.
253 : // Skips secondary paths and resolved paths.
254 : //
255 20 : BgpPath *BgpRoute::FindPath(BgpPath::PathSource src, uint32_t path_id) {
256 20 : return FindPath(src, NULL, path_id);
257 : }
258 :
259 : //
260 : // Remove path added by peer with given path id and source.
261 : // Skips secondary paths.
262 : // Return true if the path is found and removed, false otherwise.
263 : //
264 20 : bool BgpRoute::RemovePath(BgpPath::PathSource src, const IPeer *peer,
265 : uint32_t path_id) {
266 20 : for (Route::PathList::iterator it = GetPathList().begin();
267 40 : it != GetPathList().end(); it++) {
268 20 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
269 :
270 : //
271 : // Skip secondary paths.
272 : //
273 20 : if (dynamic_cast<BgpSecondaryPath *>(it.operator->())) {
274 0 : continue;
275 : }
276 :
277 40 : if (path->GetPeer() == peer && path->GetPathId() == path_id &&
278 20 : path->GetSource() == src) {
279 20 : DeletePath(path);
280 20 : return true;
281 : }
282 : }
283 0 : return false;
284 : }
285 :
286 : //
287 : // Remove path added given source and path id - peer must be NULL.
288 : // Skips secondary paths.
289 : // Return true if the path is found and removed, false otherwise.
290 : //
291 20 : bool BgpRoute::RemovePath(BgpPath::PathSource src, uint32_t path_id) {
292 20 : return RemovePath(src, NULL, path_id);
293 : }
294 :
295 : //
296 : // Remove path added by peer with given path id and source.
297 : // Skips secondary paths.
298 : // Return true if the path is found and removed, false otherwise.
299 : //
300 0 : bool BgpRoute::RemovePath(const IPeer *peer) {
301 0 : bool ret = false;
302 :
303 0 : for (Route::PathList::iterator it = GetPathList().begin(), next = it;
304 0 : it != GetPathList().end(); it = next) {
305 0 : next++;
306 0 : BgpPath *path = static_cast<BgpPath *>(it.operator->());
307 :
308 : //
309 : // Skip secondary paths.
310 : //
311 0 : if (dynamic_cast<BgpSecondaryPath *>(it.operator->())) {
312 0 : continue;
313 : }
314 :
315 0 : if (path->GetPeer() == peer) {
316 0 : DeletePath(path);
317 0 : ret = true;
318 : }
319 : }
320 0 : return ret;
321 : }
322 :
323 : //
324 : // Check if the route is usable.
325 : //
326 87595 : bool BgpRoute::IsUsable() const {
327 87595 : if (IsDeleted())
328 34741 : return false;
329 :
330 52826 : const BgpPath *path = BestPath();
331 52759 : if (!path || !path->IsFeasible())
332 0 : return false;
333 :
334 52755 : return true;
335 : }
336 :
337 : //
338 : // Check if the route is valid.
339 : //
340 640 : bool BgpRoute::IsValid() const {
341 640 : return IsUsable();
342 : }
343 :
344 : //
345 : // Check if there's a better path with the same forwarding information.
346 : //
347 : // Return true if we find such a path, false otherwise.
348 : //
349 : // The forwarding information we look at is just the next hop. We don't
350 : // consider the label since there's could be transient cases where we
351 : // have 2 paths with the same next hop and different labels. We don't
352 : // want to treat these as unique paths.
353 : //
354 0 : bool BgpRoute::DuplicateForwardingPath(const BgpPath *in_path) const {
355 0 : for (Route::PathList::const_iterator it = GetPathList().begin();
356 0 : it != GetPathList().end(); ++it) {
357 0 : const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
358 :
359 : // Bail if we reached the input path since the paths are sorted.
360 0 : if (path == in_path)
361 0 : return false;
362 :
363 : // Check the forwarding information.
364 0 : if (path->GetAttr()->nexthop() == in_path->GetAttr()->nexthop())
365 0 : return true;
366 : }
367 :
368 0 : return false;
369 : }
370 :
371 : //
372 : // Find the secondary path matching secondary replicated info.
373 : //
374 68429 : BgpPath *BgpRoute::FindSecondaryPath(BgpRoute *src_rt,
375 : BgpPath::PathSource src, const IPeer *peer, uint32_t path_id) {
376 68429 : for (Route::PathList::iterator it = GetPathList().begin();
377 136853 : it != GetPathList().end(); ++it) {
378 42053 : BgpSecondaryPath *path = dynamic_cast<BgpSecondaryPath *>(
379 42053 : it.operator->());
380 : // Skip resolved paths. They'll be taken care of by the path
381 : // resolver infrastructure.
382 80661 : if (path && (src_rt->table()->IsVpnTable()) &&
383 38615 : (path->GetFlags() & BgpPath::ResolvedPath)) {
384 0 : continue;
385 : }
386 84118 : if (path && path->src_rt() == src_rt &&
387 168199 : path->GetPeer() == peer && path->GetPathId() == path_id &&
388 42027 : path->GetSource() == src) {
389 42052 : return path;
390 : }
391 : }
392 26366 : return NULL;
393 : }
394 :
395 : //
396 : // Remove the secondary path matching secondary replicated info.
397 : // Return true if the path is found and removed, false otherwise.
398 : //
399 28480 : bool BgpRoute::RemoveSecondaryPath(const BgpRoute *src_rt,
400 : BgpPath::PathSource src, const IPeer *peer, uint32_t path_id) {
401 28479 : for (Route::PathList::iterator it = GetPathList().begin();
402 56941 : it != GetPathList().end(); it++) {
403 : BgpSecondaryPath *path =
404 28470 : dynamic_cast<BgpSecondaryPath *>(it.operator->());
405 : // Skip resolved paths. They'll be taken care of by the path
406 : // resolver infrastructure.
407 53511 : if (path && (src_rt->table()->IsVpnTable()) &&
408 25044 : (path->GetFlags() & BgpPath::ResolvedPath)) {
409 0 : continue;
410 : }
411 56979 : if (path && path->src_rt() == src_rt &&
412 113919 : path->GetPeer() == peer && path->GetPathId() == path_id &&
413 28456 : path->GetSource() == src) {
414 28479 : DeletePath(path);
415 28525 : return true;
416 : }
417 : }
418 :
419 0 : return false;
420 : }
421 :
422 26341 : size_t BgpRoute::count() const {
423 26341 : return GetPathList().size();
424 : }
425 :
426 42079 : BgpTable *BgpRoute::table() {
427 42079 : return dynamic_cast<BgpTable *>(get_table_partition()->parent());
428 : }
429 :
430 82162 : const BgpTable *BgpRoute::table() const {
431 82162 : return dynamic_cast<BgpTable *>(get_table_partition()->parent());
432 : }
433 :
434 0 : void BgpRoute::FillRouteInfo(const BgpTable *table,
435 : ShowRouteBrief *show_route) const {
436 0 : show_route->set_prefix(ToString());
437 0 : vector<ShowRoutePathBrief> show_route_paths;
438 0 : for (Route::PathList::const_iterator it = GetPathList().begin();
439 0 : it != GetPathList().end(); ++it) {
440 0 : const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
441 0 : ShowRoutePathBrief srp;
442 0 : const IPeer *peer = path->GetPeer();
443 0 : if (peer) {
444 0 : srp.set_source(peer->ToString());
445 : }
446 :
447 0 : srp.set_protocol(path->GetSourceString());
448 :
449 0 : const BgpAttr *attr = path->GetAttr();
450 0 : srp.set_local_preference(attr->local_pref());
451 0 : srp.set_med(attr->med());
452 0 : srp.set_next_hop(attr->nexthop().to_string());
453 0 : srp.set_label(path->GetLabel());
454 0 : show_route_paths.push_back(srp);
455 0 : }
456 0 : show_route->set_paths(show_route_paths);
457 0 : }
458 :
459 0 : static void FillRoutePathClusterListInfo(const ClusterList *clist,
460 : ShowRoutePath *show_path) {
461 0 : const vector<uint32_t> &list = clist->cluster_list().cluster_list;
462 0 : vector<string> cluster_list = vector<string>();
463 0 : for (vector<uint32_t>::const_iterator it = list.begin(); it != list.end();
464 0 : ++it) {
465 0 : cluster_list.push_back(Ip4Address(*it).to_string());
466 : }
467 0 : show_path->set_cluster_list(cluster_list);
468 0 : }
469 :
470 0 : static void FillRoutePathCommunityInfo(const Community *comm,
471 : ShowRoutePath *show_path, vector<string> *communities) {
472 0 : comm->BuildStringList(communities);
473 0 : }
474 :
475 0 : static void FillRoutePathExtCommunityInfo(const BgpTable *table,
476 : const ExtCommunity *extcomm,
477 : ShowRoutePath *show_path, vector<string> *communities) {
478 0 : const RoutingInstance *ri = table->routing_instance();
479 0 : const RoutingInstanceMgr *ri_mgr = ri->manager();
480 0 : vector<string> tunnel_encap = vector<string>();
481 :
482 0 : const ExtCommunity::ExtCommunityList &v = extcomm->communities();
483 0 : for (ExtCommunity::ExtCommunityList::const_iterator it = v.begin();
484 0 : it != v.end(); ++it) {
485 0 : if (ExtCommunity::is_route_target(*it)) {
486 0 : RouteTarget rt(*it);
487 0 : communities->push_back(rt.ToString());
488 0 : } else if (ExtCommunity::is_default_gateway(*it)) {
489 0 : DefaultGateway dgw(*it);
490 0 : communities->push_back(dgw.ToString());
491 0 : } else if (ExtCommunity::is_es_import(*it)) {
492 0 : EsImport es_import(*it);
493 0 : communities->push_back(es_import.ToString());
494 0 : } else if (ExtCommunity::is_esi_label(*it)) {
495 0 : EsiLabel esi_label(*it);
496 0 : communities->push_back(esi_label.ToString());
497 0 : } else if (ExtCommunity::is_mac_mobility(*it)) {
498 0 : MacMobility mm(*it);
499 0 : communities->push_back(mm.ToString());
500 0 : show_path->set_sequence_no(integerToString(mm.sequence_number()));
501 0 : } else if (ExtCommunity::is_local_sequence_number(*it)) {
502 0 : LocalSequenceNumber lsn(*it);
503 0 : communities->push_back(lsn.ToString());
504 0 : } else if (ExtCommunity::is_etree(*it)) {
505 0 : ETree etree(*it);
506 0 : communities->push_back(etree.ToString());
507 0 : } else if (ExtCommunity::is_router_mac(*it)) {
508 0 : RouterMac router_mac(*it);
509 0 : communities->push_back(router_mac.ToString());
510 0 : } else if (ExtCommunity::is_origin_vn(*it)) {
511 0 : OriginVn origin_vn(*it);
512 0 : communities->push_back(origin_vn.ToString());
513 0 : int vn_index = origin_vn.vn_index();
514 0 : show_path->set_origin_vn(
515 0 : ri_mgr->GetVirtualNetworkByVnIndex(vn_index));
516 0 : } else if (ExtCommunity::is_security_group(*it)) {
517 0 : SecurityGroup sg(*it);
518 0 : communities->push_back(sg.ToString());
519 0 : } else if (ExtCommunity::is_security_group4(*it)) {
520 0 : SecurityGroup4ByteAs sg(*it);
521 0 : communities->push_back(sg.ToString());
522 0 : } else if (ExtCommunity::is_site_of_origin(*it)) {
523 0 : SiteOfOrigin soo(*it);
524 0 : communities->push_back(soo.ToString());
525 0 : } else if (ExtCommunity::is_tunnel_encap(*it)) {
526 0 : TunnelEncap encap(*it);
527 0 : communities->push_back(encap.ToString());
528 0 : TunnelEncapType::Encap id = encap.tunnel_encap();
529 0 : tunnel_encap.push_back(TunnelEncapType::TunnelEncapToString(id));
530 0 : } else if (ExtCommunity::is_load_balance(*it)) {
531 0 : LoadBalance load_balance(*it);
532 0 : communities->push_back(load_balance.ToString());
533 :
534 0 : ShowLoadBalance show_load_balance;
535 0 : load_balance.ShowAttribute(&show_load_balance);
536 0 : show_path->set_load_balance(show_load_balance);
537 0 : } else if (ExtCommunity::is_tag(*it)) {
538 0 : Tag tag(*it);
539 0 : communities->push_back(tag.ToString());
540 0 : } else if (ExtCommunity::is_tag4(*it)) {
541 0 : Tag4ByteAs tag(*it);
542 0 : communities->push_back(tag.ToString());
543 0 : } else if (ExtCommunity::is_source_as(*it)) {
544 0 : SourceAs sas(*it);
545 0 : communities->push_back(sas.ToString());
546 0 : } else if (ExtCommunity::is_sub_cluster(*it)) {
547 0 : SubCluster sc(*it);
548 0 : communities->push_back(sc.ToString());
549 0 : } else if (ExtCommunity::is_vrf_route_import(*it)) {
550 0 : VrfRouteImport rt_import(*it);
551 0 : communities->push_back(rt_import.ToString());
552 0 : } else if (ExtCommunity::is_multicast_flags(*it)) {
553 0 : MulticastFlags mf(*it);
554 0 : communities->push_back(mf.ToString());
555 : } else {
556 : char temp[50];
557 0 : int len = snprintf(temp, sizeof(temp), "ext community: ");
558 0 : for (size_t i = 0; i < it->size(); i++) {
559 0 : len += snprintf(temp+len, sizeof(temp) - len, "%02x", (*it)[i]);
560 : }
561 0 : communities->push_back(string(temp));
562 : }
563 : }
564 0 : show_path->set_tunnel_encap(tunnel_encap);
565 0 : }
566 :
567 0 : static void FillRoutePathLargeCommunityInfo(const LargeCommunity *largecomm,
568 : ShowRoutePath *show_path, vector<string> *communities) {
569 :
570 0 : const LargeCommunity::LargeCommunityList &v = largecomm->communities();
571 0 : for (const auto &lc : v) {
572 0 : if (LargeCommunity::is_tag(lc)) {
573 0 : TagLC tag(lc);
574 0 : communities->push_back(tag.ToString());
575 : } else {
576 : char temp[50];
577 0 : int len = snprintf(temp, sizeof(temp), "large community: ");
578 0 : for (size_t i = 0; i < lc.size(); i++) {
579 0 : len += snprintf(temp + len, sizeof(temp) - len, "%02x",
580 0 : lc[i]);
581 : }
582 0 : communities->push_back(string(temp));
583 : }
584 : }
585 0 : }
586 :
587 0 : static void FillEdgeForwardingInfo(const EdgeForwarding *edge_forwarding,
588 : ShowRoutePath *show_path) {
589 0 : vector<ShowEdgeForwarding> show_ef_list;
590 : vector<EdgeForwardingSpec::Edge *> edge_list =
591 0 : edge_forwarding->edge_forwarding().edge_list;
592 0 : for (vector<EdgeForwardingSpec::Edge *>::const_iterator it =
593 0 : edge_list.begin(); it != edge_list.end(); ++it) {
594 0 : const EdgeForwardingSpec::Edge *edge = *it;
595 0 : ShowEdgeForwarding show_ef;
596 0 : ostringstream oss;
597 0 : oss << edge->GetInboundIp4Address() << ":" << edge->inbound_label;
598 0 : show_ef.set_in_address_label(oss.str());
599 0 : oss.str("");
600 0 : oss.clear();
601 0 : oss << edge->GetOutboundIp4Address() << ":" << edge->outbound_label;
602 0 : show_ef.set_out_address_label(oss.str());
603 0 : show_ef_list.push_back(show_ef);
604 0 : }
605 0 : show_path->set_edge_forwarding(show_ef_list);
606 0 : }
607 :
608 0 : static void FillEdgeDiscoveryInfo(const EdgeDiscovery *edge_discovery,
609 : ShowRoutePath *show_path) {
610 0 : vector<ShowEdgeDiscovery> show_ed_list;
611 : vector<EdgeDiscoverySpec::Edge *> edge_list =
612 0 : edge_discovery->edge_discovery().edge_list;
613 0 : int idx = 0;
614 0 : for (vector<EdgeDiscoverySpec::Edge *>::const_iterator it =
615 0 : edge_list.begin();
616 0 : it != edge_list.end(); ++it, ++idx) {
617 0 : const EdgeDiscoverySpec::Edge *edge = *it;
618 0 : ShowEdgeDiscovery show_ed;
619 0 : ostringstream oss;
620 : uint32_t first_label, last_label;
621 0 : oss << edge->GetIp4Address();
622 0 : show_ed.set_address(oss.str());
623 0 : oss.str("");
624 0 : oss.clear();
625 0 : edge->GetLabels(&first_label, &last_label);
626 0 : oss << first_label << "-" << last_label;
627 0 : show_ed.set_labels(oss.str());
628 0 : show_ed_list.push_back(show_ed);
629 0 : }
630 0 : show_path->set_edge_discovery(show_ed_list);
631 0 : }
632 :
633 0 : static void FillOriginVnPathInfo(const OriginVnPath *ovnpath,
634 : ShowRoutePath *show_path) {
635 0 : const OriginVnPath::OriginVnList &v = ovnpath->origin_vns();
636 0 : vector<string> origin_vn_path = vector<string>();
637 0 : for (OriginVnPath::OriginVnList::const_iterator it = v.begin();
638 0 : it != v.end(); ++it) {
639 0 : OriginVn origin_vn(*it);
640 0 : origin_vn_path.push_back(origin_vn.ToString());
641 : }
642 0 : show_path->set_origin_vn_path(origin_vn_path);
643 0 : }
644 :
645 0 : static void FillPmsiTunnelInfo(const PmsiTunnel *pmsi_tunnel,
646 : const ExtCommunity *ext, ShowRoutePath *show_path) {
647 0 : ShowPmsiTunnel spt;
648 0 : spt.set_type(pmsi_tunnel->pmsi_tunnel().GetTunnelTypeString());
649 0 : spt.set_ar_type(pmsi_tunnel->pmsi_tunnel().GetTunnelArTypeString());
650 0 : spt.set_identifier(pmsi_tunnel->identifier().to_string());
651 0 : spt.set_label(pmsi_tunnel->GetLabel(ext));
652 0 : spt.set_flags(pmsi_tunnel->pmsi_tunnel().GetTunnelFlagsStrings());
653 0 : show_path->set_pmsi_tunnel(spt);
654 0 : }
655 :
656 0 : void BgpRoute::FillRouteInfo(const BgpTable *table,
657 : ShowRoute *show_route, const string &source, const string &protocol) const {
658 0 : const RoutingInstance *ri = table->routing_instance();
659 :
660 0 : show_route->set_prefix(ToString());
661 0 : show_route->set_last_modified(
662 0 : integerToString(UTCUsecToPTime(last_change_at())));
663 :
664 0 : vector<ShowRoutePath> show_route_paths;
665 0 : for (Route::PathList::const_iterator it = GetPathList().begin();
666 0 : it != GetPathList().end(); ++it) {
667 0 : const BgpPath *path = static_cast<const BgpPath *>(it.operator->());
668 0 : ShowRoutePath srp;
669 0 : const IPeer *peer = path->GetPeer();
670 :
671 : // Filter against peer source, if specified.
672 0 : if (!source.empty() && (!peer || source != peer->ToString()))
673 0 : continue;
674 :
675 : // Filter against path protocol, if specified.
676 0 : if (!protocol.empty() && protocol != path->GetSourceString())
677 0 : continue;
678 :
679 0 : if (peer) {
680 0 : srp.set_source(peer->ToString());
681 : }
682 :
683 0 : const BgpPeer *bgp_peer = dynamic_cast<const BgpPeer *>(peer);
684 0 : if (bgp_peer) {
685 0 : srp.set_local_as(bgp_peer->local_as());
686 0 : srp.set_peer_as(bgp_peer->peer_as());
687 0 : srp.set_peer_router_id(bgp_peer->bgp_identifier_string());
688 : }
689 :
690 0 : const BgpAttr *attr = path->GetAttr();
691 0 : if (attr->edge_forwarding()) {
692 0 : FillEdgeForwardingInfo(attr->edge_forwarding(), &srp);
693 : }
694 0 : if (attr->edge_discovery()) {
695 0 : FillEdgeDiscoveryInfo(attr->edge_discovery(), &srp);
696 : }
697 0 : if (attr->sub_protocol().empty()) {
698 0 : srp.set_protocol(path->GetSourceString());
699 : } else {
700 0 : const string sbp = attr->sub_protocol();
701 0 : srp.set_protocol(path->GetSourceString() + " (" + sbp + ")");
702 0 : }
703 0 : srp.set_origin(attr->origin_string());
704 0 : if (attr->as_path() != NULL)
705 0 : srp.set_as_path(attr->as_path()->path().ToString());
706 0 : else if (attr->aspath_4byte() != NULL)
707 0 : srp.set_as_path(attr->aspath_4byte()->path().ToString());
708 0 : if (attr->as4_path() != NULL)
709 0 : srp.set_as4_path(attr->as4_path()->path().ToString());
710 0 : srp.set_local_preference(attr->local_pref());
711 0 : srp.set_med(attr->med());
712 0 : srp.set_next_hop(attr->nexthop().to_string());
713 0 : srp.set_label(path->GetLabel());
714 0 : srp.set_flags(path->GetFlagsStringList());
715 0 : srp.set_last_modified(
716 0 : integerToString(UTCUsecToPTime(path->time_stamp_usecs())));
717 0 : if (path->IsReplicated()) {
718 : const BgpSecondaryPath *replicated;
719 0 : replicated = static_cast<const BgpSecondaryPath *>(path);
720 0 : srp.set_replicated(true);
721 0 : srp.set_primary_table(replicated->src_table()->name());
722 : } else {
723 0 : srp.set_replicated(false);
724 : Address::Family vpn_family =
725 0 : Address::VpnFamilyFromFamily(table->family());
726 : const RoutePathReplicator *replicator =
727 0 : table->server()->replicator(vpn_family);
728 0 : if (replicator) {
729 0 : srp.set_secondary_tables(
730 0 : replicator->GetReplicatedTableNameList(table, this, path));
731 : }
732 : }
733 0 : if (attr->cluster_list()) {
734 0 : FillRoutePathClusterListInfo(attr->cluster_list(), &srp);
735 : }
736 0 : vector<string> communities;
737 0 : if (attr->community()) {
738 0 : FillRoutePathCommunityInfo(attr->community(), &srp, &communities);
739 : }
740 0 : if (attr->ext_community()) {
741 0 : FillRoutePathExtCommunityInfo(table, attr->ext_community(), &srp,
742 : &communities);
743 : }
744 0 : if (attr->large_community()) {
745 0 : FillRoutePathLargeCommunityInfo(attr->large_community(), &srp,
746 : &communities);
747 : }
748 0 : if (!communities.empty()){
749 0 : srp.set_communities(communities);
750 : }
751 0 : if (srp.get_origin_vn().empty() &&
752 0 : !table->IsVpnTable() && path->IsVrfOriginated()) {
753 0 : srp.set_origin_vn(ri->GetVirtualNetworkName());
754 : }
755 0 : if (attr->origin_vn_path()) {
756 0 : FillOriginVnPathInfo(attr->origin_vn_path(), &srp);
757 : }
758 0 : if (attr->pmsi_tunnel()) {
759 0 : const ExtCommunity *extcomm = attr->ext_community();
760 0 : if (extcomm) {
761 0 : FillPmsiTunnelInfo(attr->pmsi_tunnel(), extcomm, &srp);
762 : }
763 : }
764 0 : if (attr->originator_id().to_ulong()) {
765 0 : srp.set_originator_id(attr->originator_id().to_string());
766 : }
767 :
768 0 : show_route_paths.push_back(srp);
769 0 : }
770 0 : show_route->set_paths(show_route_paths);
771 0 : }
772 :
773 0 : void BgpRoute::NotifyOrDelete() {
774 0 : if (!front()) {
775 0 : Delete();
776 : } else {
777 0 : Notify();
778 : }
779 0 : }
780 :
781 958 : uint32_t BgpRoute::SubClusterId() const {
782 958 : BgpTable *table = static_cast<BgpTable *>(get_table());
783 958 : const BgpConfigManager *config_manager_ = table->server()->config_manager();
784 957 : if (!config_manager_) {
785 0 : return 0;
786 : }
787 : const BgpProtocolConfig *proto =
788 957 : config_manager_->GetProtocolConfig(BgpConfigManager::kMasterInstance);
789 955 : if (!proto) {
790 0 : return 0;
791 : }
792 955 : return proto->subcluster_id();
793 : }
|