/
static_route.cc
920 lines (783 loc) · 31.8 KB
/
static_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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/routing-instance/static_route.h"
#include <boost/assign/list_of.hpp>
#include <algorithm>
#include <string>
#include <vector>
#include "base/map_util.h"
#include "base/task_annotations.h"
#include "base/task_trigger.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_server.h"
#include "bgp/inet6vpn/inet6vpn_route.h"
#include "bgp/l3vpn/inetvpn_route.h"
#include "bgp/origin-vn/origin_vn.h"
#include "bgp/routing-instance/routepath_replicator.h"
#include "bgp/routing-instance/routing_instance.h"
#include "bgp/routing-instance/static_route_types.h"
#include "net/community_type.h"
using boost::assign::list_of;
using boost::system::error_code;
using std::make_pair;
using std::set;
using std::string;
using std::vector;
class StaticRouteState : public ConditionMatchState {
public:
explicit StaticRouteState(StaticRoutePtr info) : info_(info) {
}
StaticRoutePtr info() {
return info_;
}
private:
StaticRoutePtr info_;
DISALLOW_COPY_AND_ASSIGN(StaticRouteState);
};
template <typename T>
class StaticRoute : public ConditionMatch {
public:
typedef typename T::RouteT RouteT;
typedef typename T::VpnRouteT VpnRouteT;
typedef typename T::PrefixT PrefixT;
typedef typename T::AddressT AddressT;
typedef StaticRouteMgr<T> StaticRouteMgrT;
// List of Route targets
typedef set<RouteTarget> RouteTargetList;
// List of path ids for the Nexthop
typedef set<uint32_t> NexthopPathIdList;
enum CompareResult {
NoChange = 0,
PrefixChange = -1,
NexthopChange = 1,
RTargetChange = 2
};
StaticRoute(RoutingInstance *rtinstance, StaticRouteMgrT *manager,
const PrefixT &static_route, const vector<string> &rtargets,
IpAddress nexthop);
Address::Family GetFamily() const { return manager_->GetFamily(); }
AddressT GetAddress(IpAddress addr) const {
return manager_->GetAddress(addr);
}
// Compare config and return whether cfg has updated
CompareResult CompareStaticRouteCfg(const StaticRouteConfig &cfg);
void FillShowInfo(StaticRouteInfo *info) const;
const PrefixT &static_route_prefix() const {
return static_route_prefix_;
}
RoutingInstance *routing_instance() const {
return routing_instance_;
}
BgpTable *bgp_table() const {
return routing_instance_->GetTable(this->GetFamily());
}
BgpRoute *nexthop_route() const {
return nexthop_route_;
}
NexthopPathIdList *NexthopPathIds() { return &nexthop_path_ids_; }
void set_nexthop_route(BgpRoute *nexthop_route) {
nexthop_route_ = nexthop_route;
if (!nexthop_route_) {
nexthop_path_ids_.clear();
return;
}
assert(nexthop_path_ids_.empty());
for (Route::PathList::iterator it =
nexthop_route->GetPathList().begin();
it != nexthop_route->GetPathList().end(); it++) {
BgpPath *path = static_cast<BgpPath *>(it.operator->());
// Infeasible paths are not considered
if (!path->IsFeasible()) break;
// take snapshot of all ECMP paths
if (nexthop_route_->BestPath()->PathCompare(*path, true)) break;
// Use the nexthop attribute of the nexthop path as the path id.
uint32_t path_id = path->GetAttr()->nexthop().to_v4().to_ulong();
nexthop_path_ids_.insert(path_id);
}
}
const RouteTargetList &rtarget_list() const {
return rtarget_list_;
}
void UpdateRtargetList(const vector<string> &rtargets) {
rtarget_list_.clear();
for (vector<string>::const_iterator it = rtargets.begin();
it != rtargets.end(); ++it) {
error_code ec;
RouteTarget rtarget = RouteTarget::FromString(*it, &ec);
if (ec != 0)
continue;
rtarget_list_.insert(rtarget);
}
// Update static route if added already
UpdateStaticRoute();
}
void AddStaticRoute(NexthopPathIdList *list);
void UpdateStaticRoute();
void RemoveStaticRoute();
void NotifyRoute();
bool IsPending() const;
virtual bool Match(BgpServer *server, BgpTable *table,
BgpRoute *route, bool deleted);
virtual string ToString() const {
return (string("StaticRoute ") + nexthop_.to_string());
}
void set_unregistered() {
unregistered_ = true;
}
bool unregistered() const {
return unregistered_;
}
private:
// Helper function to match
bool is_nexthop_route(BgpRoute *route) {
RouteT *ip_route = dynamic_cast<RouteT *>(route);
return (nexthop_ == ip_route->GetPrefix().addr());
}
ExtCommunityPtr UpdateExtCommunity(const BgpAttr *attr) const;
RoutingInstance *routing_instance_;
StaticRouteMgrT *manager_;
PrefixT static_route_prefix_;
IpAddress nexthop_;
BgpRoute *nexthop_route_;
NexthopPathIdList nexthop_path_ids_;
RouteTargetList rtarget_list_;
bool unregistered_;
DISALLOW_COPY_AND_ASSIGN(StaticRoute);
};
template <typename T>
StaticRoute<T>::StaticRoute(RoutingInstance *rtinstance,
StaticRouteMgrT *manager, const PrefixT &static_route,
const vector<string> &rtargets, IpAddress nexthop)
: routing_instance_(rtinstance),
manager_(manager),
static_route_prefix_(static_route),
nexthop_(nexthop),
nexthop_route_(NULL),
unregistered_(false) {
for (vector<string>::const_iterator it = rtargets.begin();
it != rtargets.end(); it++) {
error_code ec;
RouteTarget rtarget = RouteTarget::FromString(*it, &ec);
if (ec != 0)
continue;
rtarget_list_.insert(rtarget);
}
}
// Compare config and return whether cfg has updated
template <typename T>
typename StaticRoute<T>::CompareResult StaticRoute<T>::CompareStaticRouteCfg(
const StaticRouteConfig &cfg) {
AddressT address = this->GetAddress(cfg.address);
PrefixT prefix(address, cfg.prefix_length);
if (static_route_prefix_ != prefix) {
return PrefixChange;
}
if (nexthop_ != cfg.nexthop) {
return NexthopChange;
}
if (rtarget_list_.size() != cfg.route_target.size()) {
return RTargetChange;
}
for (vector<string>::const_iterator it = cfg.route_target.begin();
it != cfg.route_target.end(); it++) {
error_code ec;
RouteTarget rtarget = RouteTarget::FromString(*it, &ec);
if (rtarget_list_.find(rtarget) == rtarget_list_.end()) {
return RTargetChange;
}
}
return NoChange;
}
template <typename T>
void StaticRoute<T>::FillShowInfo(StaticRouteInfo *info) const {
BgpTable *table = bgp_table();
RouteT rt_key(static_route_prefix_);
const BgpRoute *route = static_cast<const BgpRoute *>(table->Find(&rt_key));
const BgpPath *path = route ? route->FindPath(BgpPath::StaticRoute) : NULL;
info->set_prefix(static_route_prefix_.ToString());
info->set_static_rt(path ? true : false);
info->set_nexthop(nexthop_.to_string());
if (nexthop_route_) {
ShowRouteBrief show_route;
nexthop_route_->FillRouteInfo(table, &show_route);
info->set_nexthop_rt(show_route);
}
vector<string> route_target_list;
for (RouteTargetList::const_iterator it = rtarget_list_.begin();
it != rtarget_list_.end(); ++it) {
route_target_list.push_back(it->ToString());
}
info->set_route_target_list(route_target_list);
if (path) {
const RoutePathReplicator *replicator = table->server()->replicator(
Address::VpnFamilyFromFamily(GetFamily()));
info->set_secondary_tables(
replicator->GetReplicatedTableNameList(table, route, path));
}
}
// Match function called from BgpConditionListener
// Concurrency : db::DBTable
template <typename T>
bool StaticRoute<T>::Match(BgpServer *server, BgpTable *table,
BgpRoute *route, bool deleted) {
CHECK_CONCURRENCY("db::DBTable");
StaticRouteRequest::RequestType type;
if (is_nexthop_route(route) && !unregistered()) {
if (deleted)
type = StaticRouteRequest::NEXTHOP_DELETE;
else
type = StaticRouteRequest::NEXTHOP_ADD_CHG;
} else {
return false;
}
BgpConditionListener *listener = server->condition_listener(GetFamily());
StaticRouteState *state = static_cast<StaticRouteState *>
(listener->GetMatchState(table, route, this));
if (!deleted) {
// MatchState is added to the Route to ensure that DBEntry is not
// deleted before the module processes the WorkQueue request.
if (!state) {
state = new StaticRouteState(StaticRoutePtr(this));
listener->SetMatchState(table, route, this, state);
}
} else {
// MatchState is set on all the Routes that matches the conditions
// Retrieve to check and ignore delete of unseen Add Match
if (state == NULL) {
// Not seen ADD ignore DELETE
return false;
}
}
// The MatchState reference is taken to ensure that the route is not
// deleted when request is still present in the queue
// This is to handle the case where MatchState already exists and
// deleted entry gets reused or reused entry gets deleted.
state->IncrementRefCnt();
// Post the Match result to StaticRoute processing task to take Action
// Nexthop route found in NAT instance ==> Add Static Route
// and stitch the Path Attribute from nexthop route
StaticRouteRequest *req =
new StaticRouteRequest(type, table, route, StaticRoutePtr(this));
manager_->EnqueueStaticRouteReq(req);
return true;
}
//
// Build an updated ExtCommunity for the static route.
//
// Replace any RouteTargets with the list of RouteTargets for the StaticRoute.
// If the StaticRoute has an empty RouteTarget list, then we infer that this
// is not a snat use case and add the OriginVn as well. We don't want to add
// OriginVn in snat scenario because the route has to be imported into many
// VRFs and we want to set the OriginVn differently for each imported route.
//
template <typename T>
ExtCommunityPtr StaticRoute<T>::UpdateExtCommunity(const BgpAttr *attr) const {
ExtCommunity::ExtCommunityList export_list;
for (RouteTargetList::const_iterator it = rtarget_list().begin();
it != rtarget_list().end(); it++) {
export_list.push_back(it->GetExtCommunity());
}
BgpServer *server = routing_instance()->server();
ExtCommunityDB *extcomm_db = server->extcomm_db();
ExtCommunityPtr new_ext_community = extcomm_db->ReplaceRTargetAndLocate(
attr->ext_community(), export_list);
int vn_index = routing_instance()->virtual_network_index();
if (export_list.empty() && vn_index) {
OriginVn origin_vn(server->autonomous_system(), vn_index);
new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
new_ext_community.get(), origin_vn.GetExtCommunity());
}
return new_ext_community;
}
// RemoveStaticRoute
template <typename T>
void StaticRoute<T>::RemoveStaticRoute() {
CHECK_CONCURRENCY("bgp::StaticRoute");
RouteT rt_key(static_route_prefix());
DBTablePartition *partition =
static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
BgpRoute *static_route =
static_cast<BgpRoute *>(partition->Find(&rt_key));
if (!static_route || static_route->IsDeleted()) return;
for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
it != NexthopPathIds()->end(); it++) {
static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
"Removed Static route path " << static_route->ToString() <<
" path_id " << BgpPath::PathIdString(*it) <<
" in table " << bgp_table()->name());
}
if (!static_route->BestPath()) {
partition->Delete(static_route);
} else {
partition->Notify(static_route);
}
}
// UpdateStaticRoute
template <typename T>
void StaticRoute<T>::UpdateStaticRoute() {
CHECK_CONCURRENCY("bgp::Config");
RouteT rt_key(static_route_prefix());
DBTablePartition *partition =
static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
BgpRoute *static_route =
static_cast<BgpRoute *>(partition->Find(&rt_key));
if (static_route == NULL) return;
static_route->ClearDelete();
BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
for (NexthopPathIdList::iterator it = NexthopPathIds()->begin();
it != NexthopPathIds()->end(); it++) {
BgpPath *existing_path =
static_route->FindPath(BgpPath::StaticRoute, NULL, *it);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
"Update the RTarget list of Static route path "
<< static_route->ToString() << " path_id "
<< BgpPath::PathIdString(*it) << " in table "
<< bgp_table()->name());
// Add the route target in the ExtCommunity attribute.
ExtCommunityPtr ptr = UpdateExtCommunity(existing_path->GetAttr());
BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
existing_path->GetAttr(), ptr);
BgpPath *new_path =
new BgpPath(*it, BgpPath::StaticRoute, new_attr.get(),
existing_path->GetFlags(), existing_path->GetLabel());
static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
static_route->InsertPath(new_path);
}
partition->Notify(static_route);
}
// AddStaticRoute
template <typename T>
void StaticRoute<T>::AddStaticRoute(NexthopPathIdList *old_path_ids) {
CHECK_CONCURRENCY("bgp::StaticRoute");
RouteT rt_key(static_route_prefix());
DBTablePartition *partition =
static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
BgpRoute *static_route =
static_cast<BgpRoute *>(partition->Find(&rt_key));
if (static_route == NULL) {
static_route = new RouteT(static_route_prefix());
partition->Add(static_route);
} else {
static_route->ClearDelete();
}
BgpAttrDB *attr_db = routing_instance()->server()->attr_db();
CommunityDB *comm_db = routing_instance()->server()->comm_db();
for (Route::PathList::iterator it = nexthop_route()->GetPathList().begin();
it != nexthop_route()->GetPathList().end(); it++) {
BgpPath *nexthop_route_path = static_cast<BgpPath *>(it.operator->());
// Infeasible paths are not considered
if (!nexthop_route_path->IsFeasible()) break;
// take snapshot of all ECMP paths
if (nexthop_route()->BestPath()->PathCompare(*nexthop_route_path, true))
break;
// Skip paths with duplicate forwarding information. This ensures
// that we generate only one path with any given next hop and label
// when there are multiple nexthop paths from the original source
// received via different peers e.g. directly via XMPP and via BGP.
if (nexthop_route()->DuplicateForwardingPath(nexthop_route_path))
continue;
// Add the route target in the ExtCommunity attribute.
ExtCommunityPtr ptr = UpdateExtCommunity(nexthop_route_path->GetAttr());
BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
nexthop_route_path->GetAttr(), ptr);
// Append accept-own to the communities from the nexthop route.
const Community *orig_community =
nexthop_route_path->GetAttr()->community();
CommunityPtr new_community = comm_db->AppendAndLocate(
orig_community, CommunityType::AcceptOwnNexthop);
new_attr =
attr_db->ReplaceCommunityAndLocate(new_attr.get(), new_community);
// Strip aspath. This is required when the nexthop route is learnt
// via BGP.
new_attr = attr_db->ReplaceAsPathAndLocate(new_attr.get(), AsPathPtr());
// Replace the source rd if the nexthop path is a secondary path
// of a primary path in the l3vpn table. Use the RD of the primary.
if (nexthop_route_path->IsReplicated()) {
const BgpSecondaryPath *spath =
static_cast<const BgpSecondaryPath *>(nexthop_route_path);
const RoutingInstance *ri = spath->src_table()->routing_instance();
if (ri->IsMasterRoutingInstance()) {
const VpnRouteT *vpn_route =
static_cast<const VpnRouteT *>(spath->src_rt());
new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(),
vpn_route->GetPrefix().route_distinguisher());
}
}
// Check whether we already have a path with the associated path id.
uint32_t path_id =
nexthop_route_path->GetAttr()->nexthop().to_v4().to_ulong();
BgpPath *existing_path = static_route->FindPath(BgpPath::StaticRoute,
NULL, path_id);
bool is_stale = false;
if (existing_path != NULL) {
if ((new_attr.get() != existing_path->GetAttr()) ||
(nexthop_route_path->GetLabel() != existing_path->GetLabel())) {
// Update Attributes and notify (if needed)
is_stale = existing_path->IsStale();
static_route->RemovePath(BgpPath::StaticRoute, NULL, path_id);
} else {
continue;
}
}
BgpPath *new_path =
new BgpPath(path_id, BgpPath::StaticRoute, new_attr.get(),
nexthop_route_path->GetFlags(), nexthop_route_path->GetLabel());
if (is_stale)
new_path->SetStale();
static_route->InsertPath(new_path);
partition->Notify(static_route);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
"Added Static Route path " << static_route->ToString() <<
" path_id " << BgpPath::PathIdString(path_id) <<
" in table " << bgp_table()->name());
}
if (!old_path_ids) return;
for (NexthopPathIdList::iterator it = old_path_ids->begin();
it != old_path_ids->end(); it++) {
if (NexthopPathIds()->find(*it) != NexthopPathIds()->end())
continue;
static_route->RemovePath(BgpPath::StaticRoute, NULL, *it);
partition->Notify(static_route);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
"Removed StaticRoute path " << static_route->ToString() <<
" path_id " << BgpPath::PathIdString(*it) <<
" in table " << bgp_table()->name());
}
}
template <typename T>
void StaticRoute<T>::NotifyRoute() {
RouteT rt_key(static_route_prefix());
DBTablePartition *partition =
static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
BgpRoute *static_route = static_cast<BgpRoute *>(partition->Find(&rt_key));
if (!static_route)
return;
partition->Notify(static_route);
}
template <typename T>
bool StaticRoute<T>::IsPending() const {
RouteT rt_key(static_route_prefix());
DBTablePartition *partition =
static_cast<DBTablePartition *>(bgp_table()->GetTablePartition(&rt_key));
const BgpRoute *route = static_cast<BgpRoute *>(partition->Find(&rt_key));
return (!route || !route->FindPath(BgpPath::StaticRoute));
}
template <>
int StaticRouteMgr<StaticRouteInet>::static_route_task_id_ = -1;
template <>
int StaticRouteMgr<StaticRouteInet6>::static_route_task_id_ = -1;
template <typename T>
StaticRouteMgr<T>::StaticRouteMgr(RoutingInstance *rtinstance)
: rtinstance_(rtinstance),
listener_(rtinstance_->server()->condition_listener(GetFamily())),
resolve_trigger_(new TaskTrigger(
boost::bind(&StaticRouteMgr::ResolvePendingStaticRouteConfig, this),
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0)) {
if (static_route_task_id_ == -1) {
TaskScheduler *scheduler = TaskScheduler::GetInstance();
static_route_task_id_ = scheduler->GetTaskId("bgp::StaticRoute");
}
static_route_queue_ = new WorkQueue<StaticRouteRequest *>
(static_route_task_id_, routing_instance()->index(),
boost::bind(&StaticRouteMgr::StaticRouteEventCallback, this, _1));
}
template <>
Address::Family StaticRouteMgr<StaticRouteInet>::GetFamily() const {
return Address::INET;
}
template <>
Address::Family StaticRouteMgr<StaticRouteInet6>::GetFamily() const {
return Address::INET6;
}
template <>
Ip4Address StaticRouteMgr<StaticRouteInet>::GetAddress(IpAddress addr) const {
assert(addr.is_v4());
return addr.to_v4();
}
template <>
Ip6Address StaticRouteMgr<StaticRouteInet6>::GetAddress(IpAddress addr) const {
assert(addr.is_v6());
return addr.to_v6();
}
template <typename T>
void StaticRouteMgr<T>::EnqueueStaticRouteReq(StaticRouteRequest *req) {
static_route_queue_->Enqueue(req);
}
template <typename T>
bool StaticRouteMgr<T>::StaticRouteEventCallback(StaticRouteRequest *req) {
CHECK_CONCURRENCY("bgp::StaticRoute");
BgpTable *table = req->table_;
BgpRoute *route = req->rt_;
StaticRouteT *info = static_cast<StaticRouteT *>(req->info_.get());
StaticRouteState *state = NULL;
if (route) {
state = static_cast<StaticRouteState *>
(listener_->GetMatchState(table, route, info));
}
switch (req->type_) {
case StaticRouteRequest::NEXTHOP_ADD_CHG: {
assert(state);
state->reset_deleted();
if (route->IsDeleted() || !route->BestPath() ||
!route->BestPath()->IsFeasible()) {
break;
}
// Store the old path list
typename StaticRouteT::NexthopPathIdList path_ids;
path_ids.swap(*(info->NexthopPathIds()));
// Populate the Nexthop PathID
info->set_nexthop_route(route);
info->AddStaticRoute(&path_ids);
break;
}
case StaticRouteRequest::NEXTHOP_DELETE: {
assert(state);
info->RemoveStaticRoute();
if (info->deleted() || route->IsDeleted()) {
state->set_deleted();
}
info->set_nexthop_route(NULL);
break;
}
case StaticRouteRequest::DELETE_STATIC_ROUTE_DONE: {
info->set_unregistered();
if (!info->num_matchstate()) {
listener_->UnregisterMatchCondition(table, info);
static_route_map_.erase(info->static_route_prefix());
if (static_route_map_.empty())
rtinstance_->server()->RemoveStaticRouteMgr(this);
if (!routing_instance()->deleted() &&
routing_instance()->config()) {
resolve_trigger_->Set();
}
}
break;
}
default: {
assert(0);
break;
}
}
if (state) {
state->DecrementRefCnt();
if (state->refcnt() == 0 && state->deleted()) {
listener_->RemoveMatchState(table, route, info);
delete state;
if (!info->num_matchstate() && info->unregistered()) {
listener_->UnregisterMatchCondition(table, info);
static_route_map_.erase(info->static_route_prefix());
if (static_route_map_.empty())
rtinstance_->server()->RemoveStaticRouteMgr(this);
if (!routing_instance()->deleted() &&
routing_instance()->config()) {
resolve_trigger_->Set();
}
}
}
}
delete req;
return true;
}
template <typename T>
void StaticRouteMgr<T>::LocateStaticRoutePrefix(const StaticRouteConfig &cfg) {
CHECK_CONCURRENCY("bgp::Config");
AddressT address = this->GetAddress(cfg.address);
PrefixT prefix(address, cfg.prefix_length);
// Verify whether the entry already exists
typename StaticRouteMap::iterator it = static_route_map_.find(prefix);
if (it != static_route_map_.end()) {
// Wait for the delete complete cb
if (it->second->deleted()) return;
StaticRouteT *match =
static_cast<StaticRouteT *>(it->second.get());
// Check whether the config has got updated
typename StaticRouteT::CompareResult change =
match->CompareStaticRouteCfg(cfg);
// StaticRoutePrefix is the key,, it can't change
assert(change != StaticRouteT::PrefixChange);
// No change..
if (change == StaticRouteT::NoChange) return;
if (change == StaticRouteT::RTargetChange) {
// Update the route target in ExtCommunity attribute if the
// route is already added
match->UpdateRtargetList(cfg.route_target);
return;
}
// If the nexthop changes, remove the static route, if already added.
// To do this, remove the match condition and wait for remove completion
BgpConditionListener::RequestDoneCb callback =
boost::bind(&StaticRouteMgr::StopStaticRouteDone, this,
_1, _2);
listener_->RemoveMatchCondition(match->bgp_table(), it->second.get(),
callback);
return;
}
StaticRouteT *match = new StaticRouteT(
routing_instance(), this, prefix, cfg.route_target, cfg.nexthop);
StaticRoutePtr static_route_match = StaticRoutePtr(match);
if (static_route_map_.empty())
rtinstance_->server()->InsertStaticRouteMgr(this);
static_route_map_.insert(make_pair(prefix, static_route_match));
listener_->AddMatchCondition(match->bgp_table(), static_route_match.get(),
BgpConditionListener::RequestDoneCb());
}
template <typename T>
void StaticRouteMgr<T>::StopStaticRouteDone(BgpTable *table,
ConditionMatch *info) {
// Post the RequestDone event to StaticRoute task to take Action
StaticRouteRequest *req =
new StaticRouteRequest(StaticRouteRequest::DELETE_STATIC_ROUTE_DONE,
table, NULL, StaticRoutePtr(info));
EnqueueStaticRouteReq(req);
return;
}
template <typename T>
void StaticRouteMgr<T>::RemoveStaticRoutePrefix(const PrefixT &static_route) {
CHECK_CONCURRENCY("bgp::Config");
typename StaticRouteMap::iterator it = static_route_map_.find(static_route);
if (it == static_route_map_.end()) return;
if (it->second->deleted()) return;
BgpConditionListener::RequestDoneCb callback =
boost::bind(&StaticRouteMgr::StopStaticRouteDone, this, _1, _2);
StaticRouteT *match = static_cast<StaticRouteT *>(it->second.get());
listener_->RemoveMatchCondition(match->bgp_table(), match, callback);
}
template <typename T>
void StaticRouteMgr<T>::ProcessStaticRouteConfig() {
CHECK_CONCURRENCY("bgp::Config");
if (routing_instance()->deleted() || !routing_instance()->config()) return;
const BgpInstanceConfig::StaticRouteList &list =
routing_instance()->config()->static_routes(GetFamily());
typedef BgpInstanceConfig::StaticRouteList::const_iterator iterator_t;
for (iterator_t iter = list.begin(); iter != list.end(); ++iter) {
LocateStaticRoutePrefix(*iter);
}
}
template <typename T>
bool StaticRouteMgr<T>::ResolvePendingStaticRouteConfig() {
ProcessStaticRouteConfig();
return true;
}
template <typename T>
StaticRouteMgr<T>::~StaticRouteMgr() {
resolve_trigger_->Reset();
if (static_route_queue_)
delete static_route_queue_;
}
bool CompareStaticRouteConfig(const StaticRouteConfig &lhs,
const StaticRouteConfig &rhs) {
BOOL_KEY_COMPARE(lhs.address, rhs.address);
BOOL_KEY_COMPARE(lhs.prefix_length, rhs.prefix_length);
return false;
}
template <typename T>
void StaticRouteMgr<T>::UpdateStaticRouteConfig() {
CHECK_CONCURRENCY("bgp::Config");
StaticRouteConfigList config_list =
routing_instance()->config()->static_routes(GetFamily());
sort(config_list.begin(), config_list.end(), CompareStaticRouteConfig);
map_difference(&static_route_map_,
config_list.begin(), config_list.end(),
boost::bind(&StaticRouteMgr<T>::CompareStaticRoute, this, _1, _2),
boost::bind(&StaticRouteMgr<T>::AddStaticRoute, this, _1),
boost::bind(&StaticRouteMgr<T>::DelStaticRoute, this, _1),
boost::bind(&StaticRouteMgr<T>::UpdateStaticRoute, this, _1, _2));
}
template <typename T>
void StaticRouteMgr<T>::FlushStaticRouteConfig() {
CHECK_CONCURRENCY("bgp::Config");
for (typename StaticRouteMap::iterator it = static_route_map_.begin();
it != static_route_map_.end(); it++) {
RemoveStaticRoutePrefix(it->first);
}
}
template <typename T>
int StaticRouteMgr<T>::CompareStaticRoute(
typename StaticRouteMap::iterator loc,
StaticRouteConfigList::iterator it) {
AddressT address = this->GetAddress(it->address);
PrefixT prefix(address, it->prefix_length);
KEY_COMPARE(loc->first, prefix);
return 0;
}
template <typename T>
void StaticRouteMgr<T>::AddStaticRoute(StaticRouteConfigList::iterator it) {
LocateStaticRoutePrefix(*it);
}
template <typename T>
void StaticRouteMgr<T>::DelStaticRoute(typename StaticRouteMap::iterator loc) {
RemoveStaticRoutePrefix(loc->first);
}
template <typename T>
void StaticRouteMgr<T>::UpdateStaticRoute(typename StaticRouteMap::iterator loc,
StaticRouteConfigList::iterator it) {
LocateStaticRoutePrefix(*it);
}
template <typename T>
void StaticRouteMgr<T>::NotifyAllRoutes() {
CHECK_CONCURRENCY("bgp::Config");
for (typename StaticRouteMap::iterator it = static_route_map_.begin();
it != static_route_map_.end(); ++it) {
StaticRouteT *static_route =
static_cast<StaticRouteT *>(it->second.get());
static_route->NotifyRoute();
}
}
template <typename T>
void StaticRouteMgr<T>::UpdateAllRoutes() {
CHECK_CONCURRENCY("bgp::Config");
for (typename StaticRouteMap::iterator it = static_route_map_.begin();
it != static_route_map_.end(); ++it) {
StaticRouteT *static_route =
static_cast<StaticRouteT *>(it->second.get());
static_route->UpdateStaticRoute();
}
}
template <typename T>
void StaticRouteMgr<T>::DisableResolveTrigger() {
resolve_trigger_->set_disable();
}
template <typename T>
void StaticRouteMgr<T>::EnableResolveTrigger() {
resolve_trigger_->set_enable();
}
template <typename T>
uint32_t StaticRouteMgr<T>::GetRouteCount() const {
CHECK_CONCURRENCY("bgp::Config");
return static_route_map_.size();
}
template <typename T>
uint32_t StaticRouteMgr<T>::GetDownRouteCount() const {
CHECK_CONCURRENCY("bgp::Config");
uint32_t count = 0;
for (typename StaticRouteMap::const_iterator it = static_route_map_.begin();
it != static_route_map_.end(); ++it) {
const StaticRouteT *static_route =
static_cast<const StaticRouteT *>(it->second.get());
if (static_route->IsPending())
count++;
}
return count;
}
template <typename T>
bool StaticRouteMgr<T>::FillStaticRouteInfo(RoutingInstance *ri,
StaticRouteEntriesInfo *info) const{
if (static_route_map_.empty())
return false;
info->set_ri_name(ri->name());
for (typename StaticRouteMgr<T>::StaticRouteMap::const_iterator it =
static_route_map_.begin(); it != static_route_map_.end(); ++it) {
StaticRoute<T> *match =
static_cast<StaticRoute<T> *>(it->second.get());
StaticRouteInfo static_info;
match->FillShowInfo(&static_info);
info->static_route_list.push_back(static_info);
}
return true;
}
// Explicit instantiation of StaticRouteMgr for INET and INET6.
template class StaticRouteMgr<StaticRouteInet>;
template class StaticRouteMgr<StaticRouteInet6>;