/
agent_route.cc
841 lines (737 loc) · 27.7 KB
/
agent_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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/uuid/uuid_io.hpp>
#include <boost/lexical_cast.hpp>
#include <cmn/agent_cmn.h>
#include <route/route.h>
#include <vnc_cfg_types.h>
#include <agent_types.h>
#include <oper/peer.h>
#include <oper/vrf.h>
#include <oper/interface_common.h>
#include <oper/nexthop.h>
#include <oper/tunnel_nh.h>
#include <oper/vn.h>
#include <oper/mirror_table.h>
#include <oper/vxlan.h>
#include <oper/mpls.h>
#include <oper/route_common.h>
#include <oper/multicast.h>
#include <sandesh/sandesh.h>
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh_trace.h>
#include <sandesh/common/vns_constants.h>
using namespace std;
using namespace boost::asio;
SandeshTraceBufferPtr AgentDBwalkTraceBuf(SandeshTraceBufferCreate(
AGENT_DBWALK_TRACE_BUF, 1000));
class AgentRouteTable::DeleteActor : public LifetimeActor {
public:
DeleteActor(AgentRouteTable *rt_table) :
LifetimeActor(rt_table->agent()->lifetime_manager()),
table_(rt_table) {
}
virtual ~DeleteActor() {
}
virtual bool MayDelete() const {
return table_->MayDelete();
}
virtual void Shutdown() {
}
virtual void Destroy() {
assert(table_->vrf_entry_.get() != NULL);
table_->vrf_entry_->SetRouteTableDeleted(table_->GetTableType());
//Release refernces
table_->vrf_delete_ref_.Reset(NULL);
table_->vrf_entry_ = NULL;
}
private:
AgentRouteTable *table_;
};
bool RouteComparator::operator() (const AgentRoute *rt1,
const AgentRoute *rt2) {
return rt1->IsLess(*rt2);
}
bool NHComparator::operator() (const NextHop *nh1, const NextHop *nh2) {
return nh1->IsLess(*nh2);
}
AgentRouteTable::AgentRouteTable(DB *db, const std::string &name):
RouteTable(db, name), agent_(NULL), vrf_entry_(NULL, this), deleter_(NULL),
vrf_delete_ref_(this, NULL) {
OperDBTraceBuf = SandeshTraceBufferCreate("OperRoute", 5000);
}
AgentRouteTable::~AgentRouteTable() {
}
const string &AgentRouteTable::GetSuffix(Agent::RouteTableType type) {
static const string uc_suffix(".uc.route.0");
static const string mc_suffix(".mc.route.0");
static const string evpn_suffix(".evpn.route.0");
static const string l2_suffix(".l2.route.0");
static const string uc_inet6_suffix(".uc.route6.0");
switch (type) {
case Agent::INET4_UNICAST:
return uc_suffix;
case Agent::INET4_MULTICAST:
return mc_suffix;
case Agent::EVPN:
return evpn_suffix;
case Agent::BRIDGE:
return l2_suffix;
case Agent::INET6_UNICAST:
return uc_inet6_suffix;
default:
return Agent::NullString();
}
}
// Set VRF and delete life-time actor reference to VRF
void AgentRouteTable::SetVrf(VrfEntry *vrf) {
agent_ = (static_cast<VrfTable *>(vrf->get_table()))->agent();
vrf_entry_ = vrf;
vrf_id_ = vrf->vrf_id();
vrf_delete_ref_.Reset(vrf->deleter());
deleter_.reset(new DeleteActor(this));
}
// Allocate a route entry
auto_ptr<DBEntry> AgentRouteTable::AllocEntry(const DBRequestKey *k) const {
const AgentRouteKey *key = static_cast<const AgentRouteKey*>(k);
VrfKey vrf_key(key->vrf_name());
AgentRoute *route =
static_cast<AgentRoute *>(key->AllocRouteEntry(vrf_entry_.get(),
false));
return auto_ptr<DBEntry>(static_cast<DBEntry *>(route));
}
// Delete all paths from BGP Peer. Delete route if no path left
bool AgentRouteTable::DeleteAllBgpPath(DBTablePartBase *part,
DBEntryBase *entry) {
AgentRoute *route = static_cast<AgentRoute *>(entry);
if (route && !route->IsDeleted()) {
std::list<AgentPath *> to_be_deleted_path_list;
for(Route::PathList::iterator it = route->GetPathList().begin();
it != route->GetPathList().end();) {
AgentPath *path =
static_cast<AgentPath *>(it.operator->());
const Peer *peer = path->peer();
it++;
if (path->is_stale()) continue;
if (peer && peer->GetType() == Peer::BGP_PEER) {
to_be_deleted_path_list.push_back(path);
}
if (peer && peer->GetType() == Peer::MULTICAST_FABRIC_TREE_BUILDER) {
to_be_deleted_path_list.push_back(path);
}
}
//Now delete all selected paths.
std::list<AgentPath *>::iterator to_be_deleted_path_list_it =
to_be_deleted_path_list.begin();
while (to_be_deleted_path_list_it != to_be_deleted_path_list.end()) {
AgentPath *path =
static_cast<AgentPath *>(*to_be_deleted_path_list_it);
DeletePathFromPeer(part, route, path);
to_be_deleted_path_list_it++;
}
SquashStalePaths(route, NULL);
}
return true;
}
void AgentRouteTable::DeleteRouteDone(DBTableBase *base,
RouteTableWalkerState *state) {
LOG(DEBUG, "Deleted all BGP injected routes for " << base->name());
delete state;
}
bool AgentRouteTable::DelExplicitRouteWalkerCb(DBTablePartBase *part,
DBEntryBase *entry) {
return DeleteAllBgpPath(part, entry);
}
// Algorithm to select an active path from multiple potential paths.
// Uses comparator in path for selection
bool AgentRouteTable::PathSelection(const Path &path1, const Path &path2) {
const AgentPath &l_path = dynamic_cast<const AgentPath &> (path1);
const AgentPath &r_path = dynamic_cast<const AgentPath &> (path2);
// Stale path should take last precedence
if (l_path.is_stale() != r_path.is_stale()) {
return (l_path.is_stale() < r_path.is_stale());
}
return l_path.IsLess(r_path);
}
// Re-evaluate all unresolved NH. Flush and enqueue RESYNC for all NH in the
// unresolved NH tree
void AgentRouteTable::EvaluateUnresolvedNH(void) {
for (UnresolvedNHTree::iterator it = unresolved_nh_tree_.begin();
it != unresolved_nh_tree_.end(); ++it) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key = (*it)->GetDBRequestKey();
(static_cast<NextHopKey *>(req.key.get()))->sub_op_ = AgentKey::RESYNC;
agent_->nexthop_table()->Enqueue(&req);
}
unresolved_nh_tree_.clear();
}
void AgentRouteTable::AddUnresolvedNH(const NextHop *nh) {
unresolved_nh_tree_.insert(nh);
}
void AgentRouteTable::RemoveUnresolvedNH(const NextHop *nh) {
unresolved_nh_tree_.erase(nh);
}
// Re-evaluate all unresolved routes. Flush and enqueue RESYNC for all routes
// in the unresolved route tree
void AgentRouteTable::EvaluateUnresolvedRoutes(void) {
for (UnresolvedRouteTree::iterator it = unresolved_rt_tree_.begin();
it != unresolved_rt_tree_.end(); ++it) {
const AgentRoute *rt = *it;
rt->EnqueueRouteResync();
}
unresolved_rt_tree_.clear();
}
void AgentRouteTable::AddUnresolvedRoute(const AgentRoute *rt) {
unresolved_rt_tree_.insert(rt);
}
void AgentRouteTable::RemoveUnresolvedRoute(const AgentRoute *rt) {
unresolved_rt_tree_.erase(rt);
}
// Delete path from the given peer.
// If all paths are deleted,
// delete the route and notify
// Else
// Notify the DBEntry if any path is deleted
//
// Ideally, route must be notified only when active-path is deleted,
// But, notification of deleting non-active path is needed in one case.
//
// For VM spawned locally, we path BGP_PEER path with higher priority than
// LOCAL_VM peer path. But, controller-peer needs to know deletion of
// LOCAL_VM path to retract the route. So, force notify deletion of any path.
void AgentRouteTable::DeletePathFromPeer(DBTablePartBase *part,
AgentRoute *rt,
AgentPath *path) {
if (rt == NULL) {
return;
}
RouteInfo rt_info;
rt->FillTrace(rt_info, AgentRoute::DELETE_PATH, path);
OPER_TRACE_ROUTE(Route, rt_info);
if (path == NULL) {
return;
}
const Peer *peer = path->peer();
//Recompute paths since one is going off before deleting.
rt->ReComputePathDeletion(path);
// Remove path from the route
rt->RemovePath(path);
// Local path(non BGP type) is going away and so will route.
// For active peers reflector will remove the route but for
// non active peers explicitly squash the paths.
if (peer && (peer->GetType() != Peer::BGP_PEER)) {
path = rt->FindStalePath();
if (path) {
rt->RemovePath(path);
}
}
// Delete route if no more paths
if (rt->GetActivePath() == NULL) {
RouteInfo rt_info_del;
rt->FillTrace(rt_info_del, AgentRoute::DELETE, NULL);
OPER_TRACE_ROUTE(Route, rt_info_del);
PreRouteDelete(rt);
RemoveUnresolvedRoute(rt);
rt->UpdateDependantRoutes();
rt->ResyncTunnelNextHop();
ProcessDelete(rt);
part->Delete(rt);
} else {
// Notify deletion of path.
part->Notify(rt);
UpdateDependants(rt);
}
}
// Inline processing of Route request.
void AgentRouteTable::Process(DBRequest &req) {
agent()->ConcurrencyCheck();
DBTablePartition *tpart =
static_cast<DBTablePartition *>(GetTablePartition(req.key.get()));
tpart->Process(NULL, &req);
}
// Input handler for Route Table.
// Adds a route entry if not present.
// Adds path to route entry
// Paths are sorted in order of their precedence
// A DELETE request always removes path from the peer
// Route entry with no paths is automatically deleted
void AgentRouteTable::Input(DBTablePartition *part, DBClient *client,
DBRequest *req) {
AgentRouteKey *key = static_cast<AgentRouteKey *>(req->key.get());
AgentRouteData *data = static_cast<AgentRouteData *>(req->data.get());
bool notify = false;
bool route_added = false;
VrfEntry *vrf = agent_->vrf_table()->FindVrfFromName(key->vrf_name());
// Ignore request if VRF not found. Note, we process the DELETE
// request even if VRF is in deleted state
if (vrf == NULL) {
if (req->oper == DBRequest::DB_ENTRY_DELETE) {
LOG(DEBUG, "VRF <" << key->vrf_name() <<
"> not found. Ignore route DELETE");
} else {
LOG(DEBUG, "VRF <" << key->vrf_name() << "> not found. "
"> not found. Ignore route ADD/CHANGE");
}
return;
}
// We dont force DBRequest to be enqueued to the right DB Table.
// Find the right DBTable from VRF and invoke Input from right table
AgentRouteTable *route_table = vrf->GetRouteTable(key->GetRouteTableType());
if (route_table != this) {
if (route_table == NULL) {
// route table for the VRF is already deleted
// vrf should be in deleted state
assert(vrf->IsDeleted());
LOG(DEBUG, "Route Table " << key->GetRouteTableType() <<
" for VRF <" << key->vrf_name() <<
"> not found. Ignore route Request for " <<
key->ToString());
return;
}
DBTablePartition *p = static_cast<DBTablePartition *>
(route_table->GetTablePartition(key));
route_table->Input(p, client, req);
return;
}
AgentPath *path = NULL;
AgentRoute *rt = static_cast<AgentRoute *>(part->Find(key));
if (data) {
if (data->IsPeerValid(key) == false) {
AGENT_ROUTE_LOG(this,
"Route operation ignored. Invalid/Inactive Peer ",
key->ToString(), vrf_name(),
data->InvalidPeerMsg(key));
return;
}
} else {
assert(key->peer()->NeedValidityCheck() == false);
}
if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
if (vrf->IsDeleted() &&
vrf->allow_route_add_on_deleted_vrf() == false) {
return;
}
if (key->sub_op_ == AgentKey::RESYNC) {
if (rt && (rt->IsDeleted() == false)) {
if (data) {
path = key->peer() ? rt->FindPath(key->peer()) : NULL;
if (path != NULL) {
bool ecmp = path->path_preference().is_ecmp();
// AddChangePath should be triggered only if a path
// is available from the given peer
notify = data->AddChangePath(agent_, path, rt);
//If a path transition from ECMP to non ECMP
//remote the path from ecmp peer
if (ecmp && ecmp != path->path_preference().is_ecmp()) {
rt->ReComputePathDeletion(path);
} else if (rt->ReComputePathAdd(path)) {
notify = true;
}
}
} else {
//Ignore RESYNC if received on non-existing
//or deleted route entry
rt->Sync();
notify = true;
}
}
} else if (key->sub_op_ == AgentKey::ADD_DEL_CHANGE) {
// Renew the route if its in deleted state
if (rt && rt->IsDeleted()) {
rt->ClearDelete();
ProcessAdd(rt);
notify = true;
}
// Add route if not present already
if (rt == NULL) {
//If route is a gateway route first check
//if its corresponding direct route is present
//or not, if not present dont add the route
//just maintain it in unresolved list
rt = static_cast<AgentRoute *>(key->AllocRouteEntry
(vrf, data->is_multicast()));
assert(rt->vrf() != NULL);
part->Add(rt);
// Mark path as NULL so that its allocated below
path = NULL;
ProcessAdd(rt);
RouteInfo rt_info;
rt->FillTrace(rt_info, AgentRoute::ADD, NULL);
OPER_TRACE_ROUTE_ENTRY(Route, this, rt_info);
route_added = true;
} else {
// RT present. Check if path is also present by peer
path = rt->FindPathUsingKeyData(key, data);
}
//Update route with information sent in data
if (data && data->UpdateRoute(rt)) {
notify = true;
}
// Allocate path if not yet present
if (path == NULL) {
path = data->CreateAgentPath(key->peer(), rt);
rt->InsertPath(path);
rt->ProcessPath(agent_, part, path, data);
notify = true;
RouteInfo rt_info;
rt->FillTrace(rt_info, AgentRoute::ADD_PATH, path);
OPER_TRACE_ROUTE_ENTRY(Route, this, rt_info);
} else {
// Let path know of route change and update itself
path->set_is_stale(false);
bool ecmp = path->path_preference().is_ecmp();
notify = rt->ProcessPath(agent_, part, path, data);
//If a path transition from ECMP to non ECMP
//remote the path from ecmp peer
if (ecmp && ecmp != path->path_preference().is_ecmp()) {
rt->ReComputePathDeletion(path);
}
RouteInfo rt_info;
rt->FillTrace(rt_info, AgentRoute::CHANGE_PATH, path);
OPER_TRACE_ROUTE_ENTRY(Route, this, rt_info);
}
if (path->RouteNeedsSync())
rt->Sync();
if (route_added) {
EvaluateUnresolvedRoutes();
EvaluateUnresolvedNH();
}
//Used for routes which have use more than one peer information
//to compute the nexthops.
if (rt->ReComputePathAdd(path)) {
notify = true;
}
} else {
assert(0);
}
} else if (req->oper == DBRequest::DB_ENTRY_DELETE) {
assert (key->sub_op_ == AgentKey::ADD_DEL_CHANGE);
if (rt)
rt->DeletePathUsingKeyData(key, data, false);
} else {
assert(0);
}
//If this route has a unresolved path, insert to unresolved list
if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE ||
key->sub_op_ == AgentKey::RESYNC) {
if (rt && rt->HasUnresolvedPath() == true) {
AddUnresolvedRoute(rt);
}
}
//Route changed, trigger change on dependent routes
if (notify) {
const Path *prev_front = rt->front();
if (prev_front) {
rt->Sort(&AgentRouteTable::PathSelection, prev_front);
}
part->Notify(rt);
rt->UpdateDependantRoutes();
rt->ResyncTunnelNextHop();
UpdateDependants(rt);
}
}
LifetimeActor *AgentRouteTable::deleter() {
return deleter_.get();
}
//Delete all the routes
void AgentRouteTable::ManagedDelete() {
DBTableWalker *walker = agent_->db()->GetWalker();
RouteTableWalkerState *state = new RouteTableWalkerState(deleter());
walker->WalkTable(this, NULL,
boost::bind(&AgentRouteTable::DelExplicitRouteWalkerCb, this, _1, _2),
boost::bind(&AgentRouteTable::DeleteRouteDone, this, _1, state));
deleter_->Delete();
}
void AgentRouteTable::RetryDelete() {
if (!deleter()->IsDeleted()) {
return;
}
if (empty()) {
vrf_entry()->RetryDelete();
}
deleter()->RetryDelete();
}
// Find entry not in deleted state
AgentRoute *AgentRouteTable::FindActiveEntry(const AgentRouteKey *key) {
AgentRoute *entry = static_cast<AgentRoute *>(Find(key));
if (entry && entry->IsDeleted()) {
return NULL;
}
return entry;
}
AgentRoute *AgentRouteTable::FindActiveEntryNoLock(const AgentRouteKey *key) {
DBTable *table = static_cast<DBTable *>(this);
AgentRoute *entry = static_cast<AgentRoute *>(table->FindNoLock(key));
if (entry && entry->IsDeleted()) {
return NULL;
}
return entry;
}
AgentRoute *AgentRouteTable::FindActiveEntry(const AgentRoute *key) {
AgentRoute *entry = static_cast<AgentRoute *>(Find(key));
if (entry && entry->IsDeleted()) {
return NULL;
}
return entry;
}
AgentRoute *AgentRouteTable::FindActiveEntryNoLock(const AgentRoute *key) {
DBTable *table = static_cast<DBTable *>(this);
AgentRoute *entry = static_cast<AgentRoute *>(table->FindNoLock(key));
if (entry && entry->IsDeleted()) {
return NULL;
}
return entry;
}
const std::string &AgentRouteTable::vrf_name() const {
return vrf_entry_->GetName();
}
VrfEntry *AgentRouteTable::vrf_entry() const {
return vrf_entry_.get();
}
void AgentRouteTable::NotifyEntry(AgentRoute *e) {
agent()->ConcurrencyCheck();
DBTablePartBase *tpart =
static_cast<DBTablePartition *>(GetTablePartition(e));
tpart->Notify(e);
}
void AgentRouteTable::SquashStalePaths(AgentRoute *route,
const AgentPath *exception_path) {
Route::PathList::iterator it = route->GetPathList().begin();
AgentPath *path = NULL;
while (it != route->GetPathList().end()) {
path = static_cast<AgentPath *>(it.operator->());
// Delete all stale path except for the path sent(exception_path)
if (path->is_stale() && (path != exception_path)) {
// Since we squash stales, at any point of time there should be only
// one stale other than exception_path in list
DeletePathFromPeer(route->get_table_partition(), route, path);
return;
}
it++;
}
}
uint32_t AgentRoute::GetActiveLabel() const {
return GetActivePath()->label();
};
const string &AgentRoute::dest_vn_name() const {
assert(GetActivePath()->dest_vn_list().size() <= 1);
return *GetActivePath()->dest_vn_list().begin();
};
string AgentRoute::ToString() const {
return "Route Entry";
}
bool AgentRoute::IsLess(const DBEntry &rhs) const {
int cmp = CompareTo(static_cast<const Route &>(rhs));
return (cmp < 0);
};
uint32_t AgentRoute::vrf_id() const {
return vrf_->vrf_id();
}
void AgentRoute::InsertPath(const AgentPath *path) {
const Path *prev_front = front();
insert(path);
Sort(&AgentRouteTable::PathSelection, prev_front);
}
void AgentRoute::RemovePathInternal(AgentPath *path) {
remove(path);
path->clear_sg_list();
}
void AgentRoute::RemovePath(AgentPath *path) {
const Path *prev_front = front();
RemovePathInternal(path);
Sort(&AgentRouteTable::PathSelection, prev_front);
delete path;
return;
}
AgentPath *AgentRoute::FindLocalVmPortPath() 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() == NULL) {
continue;
}
if (path->peer()->export_to_controller()) {
return const_cast<AgentPath *>(path);
}
if (path->peer()->GetType() == Peer::ECMP_PEER ||
path->peer()->GetType() == Peer::VGW_PEER ||
path->peer()->GetType() == Peer::LOCAL_VM_PORT_PEER ||
path->peer()->GetType() == Peer::MULTICAST_TOR_PEER ||
path->peer()->GetType() == Peer::OVS_PEER) {
return const_cast<AgentPath *>(path);
}
}
return NULL;
}
AgentPath *AgentRoute::FindStalePath() {
Route::PathList::iterator it = GetPathList().begin();
while (it != GetPathList().end()) {
AgentPath *path = static_cast<AgentPath *>(it.operator->());
if (path->is_stale()) {
return path;
}
it++;
}
return NULL;
}
void AgentRoute::DeletePathInternal(AgentPath *path) {
AgentRouteTable *table = static_cast<AgentRouteTable *>(get_table());
table->DeletePathFromPeer(get_table_partition(), this, path);
}
//Deletes the path created by peer in key.
//Ideally only peer match is required in path however for few cases
//more than peer comparision be required.
//force_delete is used to specify if only peer check needs to be done or
//extra checks other than peer check has to be done.
//
//Explicit route walker used for deleting paths will set force_delete as true.
void AgentRoute::DeletePathUsingKeyData(const AgentRouteKey *key,
const AgentRouteData *data,
bool force_delete) {
AgentPath *peer_path = FindPathUsingKeyData(key, data);
DeletePathInternal(peer_path);
}
AgentPath *AgentRoute::FindPathUsingKeyData(const AgentRouteKey *key,
const AgentRouteData *data) const {
return FindPath(key->peer());
}
AgentPath *AgentRoute::FindPath(const Peer *peer) 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() == peer) {
return const_cast<AgentPath *>(path);
}
}
return NULL;
}
// First path in list is always treated as active path.
const AgentPath *AgentRoute::GetActivePath() const {
return static_cast<const AgentPath *>(front());
}
const NextHop *AgentRoute::GetActiveNextHop() const {
const AgentPath *path = GetActivePath();
if (path == NULL)
return NULL;
return path->ComputeNextHop(static_cast<AgentRouteTable *>(get_table())->
agent());
}
bool AgentRoute::IsRPFInvalid() const {
const AgentPath *path = GetActivePath();
if (path == NULL) {
return false;
}
return path->is_subnet_discard();
}
// If a direct route has changed, invoke a change on tunnel NH dependent on it
void AgentRoute::ResyncTunnelNextHop(void) {
for (AgentRoute::TunnelNhDependencyList::iterator iter =
tunnel_nh_list_.begin(); iter != tunnel_nh_list_.end(); iter++) {
NextHop *nh = static_cast<NextHop *>(iter.operator->());
DBEntryBase::KeyPtr key = nh->GetDBRequestKey();
NextHopKey *nh_key = static_cast<NextHopKey *>(key.get());
nh_key->sub_op_ = AgentKey::RESYNC;
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key = key;
req.data.reset(NULL);
Agent *agent = (static_cast<AgentRouteTable *>(get_table()))->agent();
agent->nexthop_table()->Enqueue(&req);
}
}
// Enqueue request to RESYNC a route
void AgentRoute::EnqueueRouteResync(void) const {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key = GetDBRequestKey();
(static_cast<AgentKey *>(req.key.get()))->sub_op_ = AgentKey::RESYNC;
Agent *agent = (static_cast<AgentRouteTable *>(get_table()))->agent();
agent->fabric_inet4_unicast_table()->Enqueue(&req);
}
//If a direct route get modified invariably trigger change
//on all dependent indirect routes, coz if a nexthop has
//changed we need to update the same in datapath for indirect
//routes
void AgentRoute::UpdateDependantRoutes(void) {
for (AgentRoute::RouteDependencyList::iterator iter =
dependant_routes_.begin(); iter != dependant_routes_.end(); iter++) {
AgentRoute *rt = iter.operator->();
rt->EnqueueRouteResync();
}
}
bool AgentRoute::HasUnresolvedPath(void) {
for(Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
const AgentPath *path =
static_cast<const AgentPath *>(it.operator->());
if (path->unresolved() == true) {
return true;
}
}
return false;
}
// Invoke SYNC on all paths to re-evaluate NH/active state
bool AgentRoute::Sync(void) {
bool ret = false;
for(Route::PathList::iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
AgentPath *path = static_cast<AgentPath *>(it.operator->());
if (path->Sync(this) == true) {
if (GetActivePath() == path) {
ret = true;
}
}
}
return ret;
}
bool AgentRoute::WaitForTraffic() const {
for(Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
const AgentPath *path = static_cast<const AgentPath *>(it.operator->());
if (path->path_preference().wait_for_traffic() == true) {
return true;
}
}
return false;
}
void AgentRouteTable::StalePathFromPeer(DBTablePartBase *part, AgentRoute *rt,
const Peer *peer) {
if (rt == NULL) {
return;
}
// Find path for the peer
AgentPath *path = rt->FindPath(peer);
RouteInfo rt_info;
rt->FillTrace(rt_info, AgentRoute::STALE_PATH, path);
OPER_TRACE_ROUTE(Route, rt_info);
if (path == NULL) {
return;
}
if (rt && (rt->IsDeleted() == false)) {
path->set_is_stale(true);
// Remove all stale path except the path received
SquashStalePaths(rt, path);
rt->GetPathList().sort(&AgentRouteTable::PathSelection);
rt->Sync();
part->Notify(rt);
}
}
bool AgentRoute::ProcessPath(Agent *agent, DBTablePartition *part,
AgentPath *path, AgentRouteData *data) {
bool ret = data->AddChangePath(agent, path, this);
if (RecomputeRoutePath(agent, part, path, data)) {
ret = true;
}
return ret;
}
bool AgentRouteData::IsPeerValid(const AgentRouteKey *key) const {
const Peer *peer = key->peer();
assert(peer->NeedValidityCheck() == false);
return true;
}
std::string AgentRouteData::InvalidPeerMsg(const AgentRouteKey *key) const {
return "AgentRouteData: Unknown Reason";
}
AgentPath *AgentRouteData::CreateAgentPath(const Peer *peer,
AgentRoute *rt) const {
return (new AgentPath(peer, rt));
}