-
Notifications
You must be signed in to change notification settings - Fork 390
/
inet_unicast_route.cc
1846 lines (1616 loc) · 71.9 KB
/
inet_unicast_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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/uuid/uuid_io.hpp>
#include "base/task_annotations.h"
#include <boost/foreach.hpp>
#include <cmn/agent_cmn.h>
#include "net/address_util.h"
#include <route/route.h>
#include <oper/ecmp_load_balance.h>
#include <oper/route_common.h>
#include <oper/vrf.h>
#include <oper/tunnel_nh.h>
#include <oper/mpls.h>
#include <oper/vxlan.h>
#include <oper/mirror_table.h>
#include <oper/multicast.h>
#include <controller/controller_export.h>
#include <controller/controller_peer.h>
#include <controller/controller_route_path.h>
#include <oper/agent_sandesh.h>
using namespace std;
using namespace boost::asio;
AgentRoute *
InetUnicastRouteKey::AllocRouteEntry(VrfEntry *vrf, bool is_multicast) const {
InetUnicastRouteEntry * entry =
new InetUnicastRouteEntry(vrf, dip_, plen_, is_multicast);
return static_cast<AgentRoute *>(entry);
}
AgentRouteKey *InetUnicastRouteKey::Clone() const {
return (new InetUnicastRouteKey(peer(), vrf_name_, dip_, plen_));
}
/////////////////////////////////////////////////////////////////////////////
// InetUnicastAgentRouteTable functions
/////////////////////////////////////////////////////////////////////////////
InetUnicastAgentRouteTable::InetUnicastAgentRouteTable(DB *db,
const std::string &name) :
AgentRouteTable(db, name), walkid_(DBTableWalker::kInvalidWalkerId) {
if (name.find("uc.route.0") != std::string::npos) {
type_ = Agent::INET4_UNICAST;
} else if (name.find("uc.route6.0") != std::string::npos) {
type_ = Agent::INET6_UNICAST;
} else if (name.find("evpn.route.0") != std::string::npos) {
type_ = Agent::EVPN;
} else if (name.find("l2.route.0") != std::string::npos) {
type_ = Agent::BRIDGE;
} else if (name.find("mc.route.0") != std::string::npos) {
type_ = Agent::INET4_MULTICAST;
} else {
type_ = Agent::INVALID;
}
}
DBTableBase *
InetUnicastAgentRouteTable::CreateTable(DB *db, const std::string &name) {
AgentRouteTable *table = new InetUnicastAgentRouteTable(db, name);
table->Init();
return table;
}
InetUnicastRouteEntry *
InetUnicastAgentRouteTable::FindLPM(const IpAddress &ip) {
uint32_t plen = 128;
if (ip.is_v4()) {
plen = 32;
}
InetUnicastRouteEntry key(NULL, ip, plen, false);
return tree_.LPMFind(&key);
}
InetUnicastRouteEntry *
InetUnicastAgentRouteTable::FindLPM(const InetUnicastRouteEntry &rt_key) {
return tree_.LPMFind(&rt_key);
}
InetUnicastRouteEntry *
InetUnicastAgentRouteTable::FindResolveRoute(const Ip4Address &ip) {
uint8_t plen = 32;
InetUnicastRouteEntry *rt = NULL;
do {
InetUnicastRouteEntry key(NULL, ip, plen, false);
rt = tree_.LPMFind(&key);
if (rt) {
const NextHop *nh = rt->GetActiveNextHop();
if (nh && nh->GetType() == NextHop::RESOLVE)
return rt;
}
} while (rt && --plen);
return NULL;
}
InetUnicastRouteEntry *
InetUnicastAgentRouteTable::FindResolveRoute(const string &vrf_name,
const Ip4Address &ip) {
VrfEntry *vrf =
Agent::GetInstance()->vrf_table()->FindVrfFromName(vrf_name);
InetUnicastAgentRouteTable *rt_table =
static_cast<InetUnicastAgentRouteTable *>
(vrf->GetInet4UnicastRouteTable());
return rt_table->FindResolveRoute(ip);
}
static void Inet4UnicastTableEnqueue(Agent *agent, DBRequest *req) {
AgentRouteTable *table = agent->fabric_inet4_unicast_table();
if (table) {
table->Enqueue(req);
}
}
static void Inet6UnicastTableEnqueue(Agent *agent, const string &vrf_name,
DBRequest *req) {
AgentRouteTable *table = agent->fabric_inet4_unicast_table();
if (table) {
table->Enqueue(req);
}
}
static void InetUnicastTableEnqueue(Agent *agent, const string &vrf,
DBRequest *req) {
InetUnicastRouteKey *key = static_cast<InetUnicastRouteKey *>(req->key.get());
if (key->addr().is_v4()) {
Inet4UnicastTableEnqueue(agent, req);
} else if (key->addr().is_v6()) {
Inet6UnicastTableEnqueue(agent, vrf, req);
}
}
static void Inet4UnicastTableProcess(Agent *agent, const string &vrf_name,
DBRequest &req) {
AgentRouteTable *table =
agent->vrf_table()->GetInet4UnicastRouteTable(vrf_name);
if (table) {
table->Process(req);
}
}
static void Inet6UnicastTableProcess(Agent *agent, const string &vrf_name,
DBRequest &req) {
AgentRouteTable *table =
agent->vrf_table()->GetInet6UnicastRouteTable(vrf_name);
if (table) {
table->Process(req);
}
}
static void InetUnicastTableProcess(Agent *agent, const string &vrf_name,
DBRequest &req) {
InetUnicastRouteKey *key = static_cast<InetUnicastRouteKey *>(req.key.get());
if (key->addr().is_v4()) {
Inet4UnicastTableProcess(agent, vrf_name, req);
} else if (key->addr().is_v6()) {
Inet6UnicastTableProcess(agent, vrf_name, req);
}
}
void InetUnicastAgentRouteTable::ReEvaluatePaths(const Agent* agent,
const string &vrf_name,
const IpAddress &addr,
uint8_t plen) {
DBRequest rt_req(DBRequest::DB_ENTRY_ADD_CHANGE);
InetUnicastRouteKey *rt_key = new InetUnicastRouteKey(agent->local_peer(),
vrf_name,
addr,
plen);
rt_key->sub_op_ = AgentKey::RESYNC;
rt_req.key.reset(rt_key);
rt_req.data.reset(NULL);
InetUnicastTableEnqueue(Agent::GetInstance(), vrf_name, &rt_req);
}
/*
* Traverse all smaller subnets w.r.t. route sent and mark the arp flood flag
* accordingly.
*/
bool InetUnicastAgentRouteTable::ResyncSubnetRoutes(const InetUnicastRouteEntry *rt,
bool add_change)
{
const IpAddress addr = rt->addr();
uint16_t plen = rt->plen();
InetUnicastRouteEntry *lpm_rt = GetNextNonConst(rt);
Ip4Address v4_parent_mask;
Ip6Address v6_parent_mask;
if (GetTableType() == Agent::INET4_UNICAST) {
v4_parent_mask = Address::GetIp4SubnetAddress(addr.to_v4(),
plen);
} else {
v6_parent_mask = Address::GetIp6SubnetAddress(addr.to_v6(),
plen);
}
while ((lpm_rt != NULL) && (plen < lpm_rt->plen())) {
if (GetTableType() == Agent::INET4_UNICAST) {
Ip4Address node_mask =
Address::GetIp4SubnetAddress(lpm_rt->addr().to_v4(),
plen);
if (v4_parent_mask != node_mask)
break;
} else {
Ip6Address node_mask =
Address::GetIp6SubnetAddress(lpm_rt->addr().to_v6(),
plen);
if (v6_parent_mask != node_mask)
break;
}
//Ignored all non subnet routes.
if (lpm_rt->IsHostRoute() == false) {
bool notify = false;
if (lpm_rt->ipam_subnet_route() != add_change) {
lpm_rt->set_ipam_subnet_route(add_change);
notify = true;
}
if (lpm_rt->proxy_arp() == true) {
if (add_change == true) {
lpm_rt->set_proxy_arp(false);
notify = true;
}
}
if (notify) {
//Send notify
NotifyEntry(lpm_rt);
}
}
lpm_rt = GetNextNonConst(lpm_rt);
}
return false;
}
//Null peer means sync is done on routes.
//If peer is specified then path belonging to peer will be resync'd
//Functions in TraverseHostRoutesInSubnet:
//1) If subnet rt added does not have any supernet then visit all unresolved
// routes and resync them.
//2) If supernet route is present then traverse lpm routes under this newly
// added subnet and resync all host routes pointing to supernet route of this
// subnet.
//3) Skip all the host routes present under this subnet belonging to a better
// subnet(better subnet is the one which comes under subnet being added).
// e.g. 1.1.1.10/24 is a better subnet in 1.1.1.10/16 while supernet can be
// 1.1.1.0/8
void
InetUnicastAgentRouteTable::TraverseHostRoutesInSubnet(InetUnicastRouteEntry *rt,
const Peer *peer)
{
const IpAddress addr = rt->addr();
uint16_t plen = rt->plen();
InetUnicastRouteEntry *supernet_rt = GetSuperNetRoute(rt->addr());
//If supernet route is NULL, then this is the default route and visit to
//unresolved route for resync should suffice.
if (supernet_rt == NULL) {
//Resync all unresolved routes.
EvaluateUnresolvedRoutes();
return;
}
//So suernet route is present and this subnet route is a better route.
//Look for all routes which were pointing to supernet route and move them
//to this subnet route, if they fall in same subnet.
IpAddress parent_mask = GetSubnetAddress(addr, plen);
for (InetUnicastRouteEntry *lpm_rt = GetNextNonConst(rt);
(lpm_rt != NULL) && (plen < lpm_rt->plen());
lpm_rt= GetNextNonConst(lpm_rt)) {
IpAddress node_mask = GetSubnetAddress(lpm_rt->addr(), plen);
if (parent_mask != node_mask)
break;
//Non host route, lets continue as this route will have its own NH and it
//need not be modified.
if (lpm_rt->IsHostRoute() == false) {
continue;
}
//If supernet route of this subnet route is not parent of lpm_rt added,
//then skip as its not lpm_rt dependant as well.
//There may be some other subnet which is handling it.
//e.g. 1.1.0.0/8, 1.1.0.0/16, 1.1.1.0/24, 1.1.1.1, 1.1.0.1
//Here parent of 1.1.1.1 will be 1.1.1.0/24 and not 1.1.0.0/16.
//So if subnet route getting added is 1.1.0.0/16 in presence of 1.1.1.0/24
//then 1.1.1.1 should not be modified.
if (lpm_rt->GetActivePath()->dependant_rt() != supernet_rt)
continue;
//Proceed only for host route
//Resync will ensure that subnet route is added to lpm host route
//dependant rt(gw_ip).
lpm_rt->EnqueueRouteResync();
}
}
IpAddress
InetUnicastAgentRouteTable::GetSubnetAddress(const IpAddress &addr,
uint16_t plen) const {
if (type_ == Agent::INET4_UNICAST) {
return (Address::GetIp4SubnetAddress(addr.to_v4(), plen));
}
return (Address::GetIp6SubnetAddress(addr.to_v6(), plen));
}
//Tries searching for subnet route to which this route belongs.
//Route itself can be a subnet/host. To search for supernet plen is reduced by
//one and findlpm is issued.
InetUnicastRouteEntry *
InetUnicastAgentRouteTable::GetSuperNetRoute(const IpAddress &addr) {
InetUnicastRouteEntry key(NULL, addr, (GetHostPlen(addr) - 1),
false);
InetUnicastRouteEntry *parent_key = FindLPM(key);
return parent_key;
}
/////////////////////////////////////////////////////////////////////////////
// Inet4UnicastAgentRouteEntry functions
/////////////////////////////////////////////////////////////////////////////
InetUnicastRouteEntry::InetUnicastRouteEntry(VrfEntry *vrf,
const IpAddress &addr,
uint8_t plen,
bool is_multicast) :
AgentRoute(vrf, is_multicast), plen_(plen), ipam_subnet_route_(false),
proxy_arp_(false) {
if (addr.is_v4()) {
addr_ = Address::GetIp4SubnetAddress(addr.to_v4(), plen);
} else {
addr_ = Address::GetIp6SubnetAddress(addr.to_v6(), plen);
}
}
string InetUnicastRouteKey::ToString() const {
ostringstream str;
str << dip_.to_string();
str << "/";
str << (int)plen_;
return str.str();
}
string InetUnicastRouteEntry::ToString() const {
ostringstream str;
str << addr_.to_string();
str << "/";
str << (int)plen_;
return str.str();
}
int InetUnicastRouteEntry::CompareTo(const Route &rhs) const {
const InetUnicastRouteEntry &a =
static_cast<const InetUnicastRouteEntry &>(rhs);
if (addr_ < a.addr_) {
return -1;
}
if (addr_ > a.addr_) {
return 1;
}
if (plen_ < a.plen_) {
return -1;
}
if (plen_ > a.plen_) {
return 1;
}
return 0;
}
DBEntryBase::KeyPtr InetUnicastRouteEntry::GetDBRequestKey() const {
Agent *agent =
(static_cast<InetUnicastAgentRouteTable *>(get_table()))->agent();
InetUnicastRouteKey *key =
new InetUnicastRouteKey(agent->local_peer(),
vrf()->GetName(), addr_, plen_);
return DBEntryBase::KeyPtr(key);
}
void InetUnicastRouteEntry::SetKey(const DBRequestKey *key) {
Agent *agent =
(static_cast<InetUnicastAgentRouteTable *>(get_table()))->agent();
const InetUnicastRouteKey *k =
static_cast<const InetUnicastRouteKey*>(key);
SetVrf(agent->vrf_table()->FindVrfFromName(k->vrf_name()));
IpAddress tmp(k->addr());
set_addr(tmp);
set_plen(k->plen());
}
bool InetUnicastRouteEntry::IsHostRoute() const {
InetUnicastAgentRouteTable *table =
static_cast<InetUnicastAgentRouteTable *>(get_table());
if (table->GetTableType() == Agent::INET4_UNICAST) {
if (plen_ != Address::kMaxV4PrefixLen)
return false;
} else if (table->GetTableType() == Agent::INET6_UNICAST) {
if (plen_ != Address::kMaxV6PrefixLen)
return false;
}
return true;
}
bool InetUnicastRouteEntry::ModifyEcmpPath(const IpAddress &dest_addr,
uint8_t plen, const VnListType &vn_list,
uint32_t label, bool local_ecmp_nh,
const string &vrf_name,
SecurityGroupList sg_list,
const CommunityList &communities,
const PathPreference &path_preference,
TunnelType::TypeBmap tunnel_bmap,
const EcmpLoadBalance &ecmp_load_balance,
DBRequest &nh_req,
Agent* agent, AgentPath *path) {
bool ret = false;
NextHop *nh = NULL;
agent->nexthop_table()->Process(nh_req);
nh = static_cast<NextHop *>(agent->nexthop_table()->
FindActiveEntry(nh_req.key.get()));
if (nh == NULL) {
VrfEntry *vrf = agent->vrf_table()->FindVrfFromName(vrf_name);
if (vrf->IsDeleted())
return ret;
assert(0);
}
if (path->label() != label) {
path->set_label(label);
ret = true;
}
ret = SyncEcmpPath(path, sg_list, communities, path_preference,
tunnel_bmap, ecmp_load_balance);
path->set_dest_vn_list(vn_list);
ret = true;
path->set_unresolved(false);
if (path->ChangeNH(agent, nh) == true)
ret = true;
return ret;
}
// Function to create a ECMP path from path1 and path2
// Creates Composite-NH with 2 Component-NH (one for each of path1 and path2)
// Creates a new MPLS Label for the ECMP path
AgentPath *InetUnicastRouteEntry::AllocateEcmpPath(Agent *agent,
const AgentPath *path1,
const AgentPath *path2) {
// Allocate and insert a path
AgentPath *path = new AgentPath(agent->ecmp_peer(), NULL);
InsertPath(path);
// Allocate a new label for the ECMP path
uint32_t label = agent->mpls_table()->AllocLabel();
// Create Component NH to be added to ECMP path
DBEntryBase::KeyPtr key1 = path1->ComputeNextHop(agent)->GetDBRequestKey();
NextHopKey *nh_key1 = static_cast<NextHopKey *>(key1.release());
std::auto_ptr<const NextHopKey> nh_akey1(nh_key1);
nh_key1->SetPolicy(false);
ComponentNHKeyPtr component_nh_data1(new ComponentNHKey(path1->label(),
nh_akey1));
DBEntryBase::KeyPtr key2 = path2->ComputeNextHop(agent)->GetDBRequestKey();
NextHopKey *nh_key2 = static_cast<NextHopKey *>(key2.release());
std::auto_ptr<const NextHopKey> nh_akey2(nh_key2);
nh_key2->SetPolicy(false);
ComponentNHKeyPtr component_nh_data2(new ComponentNHKey(path2->label(),
nh_akey2));
ComponentNHKeyList component_nh_list;
component_nh_list.push_back(component_nh_data1);
component_nh_list.push_back(component_nh_data2);
// Directly call AddChangePath to update NH in the ECMP path
// It will also create CompositeNH if necessary
DBRequest nh_req(DBRequest::DB_ENTRY_ADD_CHANGE);
nh_req.key.reset(new CompositeNHKey(Composite::LOCAL_ECMP,
false, component_nh_list,
vrf()->GetName()));
nh_req.data.reset(new CompositeNHData());
InetUnicastRouteEntry::ModifyEcmpPath(addr_, plen_, path2->dest_vn_list(),
label, true, vrf()->GetName(),
path2->sg_list(),
path2->communities(),
path2->path_preference(),
path2->tunnel_bmap(),
path2->ecmp_load_balance(),
nh_req, agent, path);
//Make MPLS label point to Composite NH
MplsLabel::CreateEcmpLabel(agent, label, Composite::LOCAL_ECMP, component_nh_list,
vrf()->GetName());
RouteInfo rt_info;
FillTrace(rt_info, AgentRoute::CHANGE_PATH, path);
AgentRouteTable *table = static_cast<AgentRouteTable *>(get_table());
OPER_TRACE_ROUTE_ENTRY(Route, table, rt_info);
AGENT_ROUTE_LOG(table, "Path change", ToString(), vrf()->GetName(),
GETPEERNAME(agent->ecmp_peer()));
return path;
}
// Handle deletion of a path in route. If the path being deleted is part of
// ECMP, then deletes the Component-NH for the path.
// Delete ECMP path if there is single Component-NH in Composite-NH
bool InetUnicastRouteEntry::EcmpDeletePath(AgentPath *path) {
if (path->peer() == NULL) {
return false;
}
if (path->peer()->GetType() != Peer::LOCAL_VM_PORT_PEER) {
return false;
}
Agent *agent =
(static_cast<InetUnicastAgentRouteTable *> (get_table()))->agent();
// Composite-NH is made from LOCAL_VM_PORT_PEER, count number of paths
// with LOCAL_VM_PORT_PEER
int count = 0;
for(Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
const AgentPath *it_path =
static_cast<const AgentPath *>(it.operator->());
if (it_path->peer() &&
it_path->peer()->GetType() == Peer::LOCAL_VM_PORT_PEER &&
it_path->path_preference().is_ecmp() == true &&
it_path != path)
count++;
}
AgentPath *ecmp = NULL;
// Sanity check. When more than one LOCAL_VM_PORT_PEER, ECMP must be present
if (count >= 1) {
ecmp = FindPath(agent->ecmp_peer());
if (ecmp == NULL) {
return false;
}
}
if (count == 1 && ecmp) {
// There is single path of type LOCAL_VM_PORT_PEER. Delete the ECMP path
remove(ecmp);
//Enqueue MPLS label delete request
MplsLabel::Delete(agent, ecmp->label());
delete ecmp;
} else if (count > 1) {
// Remove Component-NH for the path being deleted
DeleteComponentNH(agent, path);
}
return true;
}
/*
* This routine finds out if there is supernet for this subnet route and that
* is used to inherit flood flag. In case supernet is pointing to resolve i.e.
* gateway without having Ipam path, then search continues further
*/
bool InetUnicastRouteEntry::IpamSubnetRouteAvailable() const {
return (GetIpamSuperNetRoute() != NULL);
}
InetUnicastRouteEntry *
InetUnicastRouteEntry::GetIpamSuperNetRoute() const {
if (plen_ == 0)
return NULL;
//Local path present means that this route itself was programmed
//because of IPAM add as well and hence its eligible for flood in
//all paths where NH is tunnel.
InetUnicastAgentRouteTable *table =
static_cast<InetUnicastAgentRouteTable *>(get_table());
//Search for supernet, if none then dont flood else again search for
//local path. If found then mark for flood otherwise check for active path
//and retain flood flag from that path.
uint16_t plen = plen_ - 1;
while (plen != 0) {
assert(plen < plen_);
InetUnicastRouteEntry key(vrf(), addr_, plen, false);
// Find next highest matching route
InetUnicastRouteEntry *supernet_rt = table->FindRouteUsingKey(key);
if (supernet_rt == NULL)
return NULL;
if (supernet_rt->ipam_subnet_route())
return supernet_rt;
plen--;
}
return NULL;
}
bool InetUnicastRouteEntry::ReComputePathAdd(AgentPath *path) {
bool ret = false;
InetUnicastAgentRouteTable *uc_rt_table =
static_cast<InetUnicastAgentRouteTable *>(get_table());
if (IsHostRoute() == false)
uc_rt_table->TraverseHostRoutesInSubnet(this,
uc_rt_table->agent()->
inet_evpn_peer());
// ECMP path are managed by route module. Update ECMP path with
// addition of new path
ret |= EcmpAddPath(path);
return ret;
}
bool InetUnicastRouteEntry::ReComputePathDeletion(AgentPath *path) {
if (IsHostRoute() == false) {
//TODO merge both evpn inet and subnet routes handling
UpdateDependantRoutes();
}
//Subnet discard = Ipam subnet route.
//Ipam path is getting deleted so all the smaller subnets should be
//resynced to remove the arp flood marking.
if (path->is_subnet_discard()) {
//Reset flag on route as ipam is going off.
ipam_subnet_route_ = false;
proxy_arp_ = false;
InetUnicastAgentRouteTable *uc_rt_table =
static_cast<InetUnicastAgentRouteTable *>(get_table());
uc_rt_table->ResyncSubnetRoutes(this, false);
return true;
}
// ECMP path are managed by route module. Update ECMP path with
// deletion of new path
return EcmpDeletePath(path);
}
bool InetUnicastRouteEntry::SyncEcmpPath(AgentPath *path,
const SecurityGroupList sg_list,
const CommunityList &communities,
const PathPreference &path_preference,
TunnelType::TypeBmap tunnel_bmap,
const EcmpLoadBalance
&ecmp_load_balance) {
if (!path) {
return false;
}
bool ret = false;
path->set_tunnel_bmap(tunnel_bmap);
TunnelType::Type new_tunnel_type =
TunnelType::ComputeType(path->tunnel_bmap());
if (path->tunnel_type() != new_tunnel_type) {
path->set_tunnel_type(new_tunnel_type);
ret = true;
}
SecurityGroupList path_sg_list;
path_sg_list = path->sg_list();
if (path_sg_list != sg_list) {
path->set_sg_list(sg_list);
ret = true;
}
CommunityList path_communities;
path_communities = path->communities();
if (path_communities != communities) {
path->set_communities(communities);
ret = true;
}
if (path_preference != path->path_preference()) {
path->set_path_preference(path_preference);
ret = true;
}
if (path->ecmp_load_balance() != ecmp_load_balance) {
path->set_ecmp_load_balance(ecmp_load_balance);
ret = true;
}
return ret;
}
// Handle add/update of a path in route.
// If there are more than one path of type LOCAL_VM_PORT_PEER, creates/updates
// Composite-NH for them
bool InetUnicastRouteEntry::EcmpAddPath(AgentPath *path) {
if (path->peer() == NULL) {
return false;
}
// We are interested only in path from LOCAL_VM_PORT_PEER
if (path->peer()->GetType() != Peer::LOCAL_VM_PORT_PEER ||
path->path_preference().is_ecmp() == false) {
return false;
}
path->set_tunnel_bmap(TunnelType::MplsType());
Agent *agent =
(static_cast<InetUnicastAgentRouteTable *> (get_table()))->agent();
// Count number of paths from LOCAL_VM_PORT_PEER already present
const AgentPath *ecmp = NULL;
const AgentPath *vm_port_path = NULL;
int count = 0;
for(Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
const AgentPath *it_path =
static_cast<const AgentPath *>(it.operator->());
if (it_path->peer() == agent->ecmp_peer())
ecmp = it_path;
if (it_path->peer() &&
it_path->peer()->GetType() == Peer::LOCAL_VM_PORT_PEER &&
it_path->path_preference().is_ecmp() == true) {
count++;
if (it_path != path)
vm_port_path = it_path;
}
}
if (count == 0) {
return false;
}
// Sanity check. When more than one LOCAL_VM_PORT_PEER, ECMP must be present
if (count > 2) {
assert(ecmp != NULL);
}
if (count == 1) {
assert(ecmp == NULL);
return false;
}
bool ret = false;
if (count == 2 && ecmp == NULL) {
// This is second path being added, make ECMP
AllocateEcmpPath(agent, vm_port_path, path);
ret = true;
} else if (count > 2) {
// ECMP already present, add/update Component-NH for the path
AppendEcmpPath(agent, path);
ret = true;
} else if (ecmp) {
AgentPath *ecmp_path = FindPath(agent->ecmp_peer());
bool updated = UpdateComponentNH(agent, ecmp_path, path);
ret = SyncEcmpPath(ecmp_path, path->sg_list(),
path->communities(), path->path_preference(),
path->tunnel_bmap(),
path->ecmp_load_balance());
if (updated) {
ret = true;
}
}
return ret;
}
void InetUnicastRouteEntry::AppendEcmpPath(Agent *agent,
AgentPath *path) {
AgentPath *ecmp_path = FindPath(agent->ecmp_peer());
assert(ecmp_path);
DBEntryBase::KeyPtr key = path->ComputeNextHop(agent)->GetDBRequestKey();
NextHopKey *nh_key = static_cast<NextHopKey *>(key.release());
std::auto_ptr<const NextHopKey> nh_akey(nh_key);
nh_key->SetPolicy(false);
ComponentNHKeyPtr comp_nh_key_ptr(new ComponentNHKey(path->label(), nh_akey));
ComponentNHKeyList component_nh_key_list;
const CompositeNH *comp_nh =
static_cast<const CompositeNH *>(ecmp_path->ComputeNextHop(agent));
component_nh_key_list = comp_nh->AddComponentNHKey(comp_nh_key_ptr);
// Form the request for Inet4UnicastEcmpRoute and invoke AddChangePath
DBRequest nh_req(DBRequest::DB_ENTRY_ADD_CHANGE);
nh_req.key.reset(new CompositeNHKey(Composite::LOCAL_ECMP,
false, component_nh_key_list,
vrf()->GetName()));
nh_req.data.reset(new CompositeNHData());
InetUnicastRouteEntry::ModifyEcmpPath(addr_, plen_, path->dest_vn_list(),
ecmp_path->label(), true, vrf()->GetName(),
path->sg_list(), path->communities(),
path->path_preference(),
path->tunnel_bmap(),
path->ecmp_load_balance(),
nh_req, agent, ecmp_path);
//Make MPLS label point to composite NH
MplsLabel::CreateEcmpLabel(agent, ecmp_path->label(), Composite::LOCAL_ECMP,
component_nh_key_list, vrf()->GetName());
RouteInfo rt_info;
FillTrace(rt_info, AgentRoute::CHANGE_PATH, path);
AgentRouteTable *table = static_cast<AgentRouteTable *>(get_table());
OPER_TRACE_ROUTE_ENTRY(Route, table, rt_info);
AGENT_ROUTE_LOG(table, "Path change", ToString(), vrf()->GetName(),
GETPEERNAME(agent->ecmp_peer()));
}
/* When label of VMI changes and if that VMI (ie VMI's InterfaceNH) is part of
* ECMP, then update the CompositeNH for ECMP route to point to right label for
* that VMI. Label of VMI can change when policy-status of VMI changes */
bool InetUnicastRouteEntry::UpdateComponentNH(Agent *agent,
AgentPath *ecmp_path,
AgentPath *path) {
if (!ecmp_path) {
return false;
}
//Build ComponentNHKey for new path
DBEntryBase::KeyPtr key = path->ComputeNextHop(agent)->GetDBRequestKey();
NextHopKey *nh_key = static_cast<NextHopKey *>(key.release());
nh_key->SetPolicy(false);
ComponentNHKeyList component_nh_key_list;
const CompositeNH *comp_nh =
static_cast<const CompositeNH *>(ecmp_path->ComputeNextHop(agent));
bool updated = comp_nh->UpdateComponentNHKey(path->label(), nh_key,
component_nh_key_list);
if (!updated) {
return false;
}
// Form the request for Inet4UnicastEcmpRoute and invoke AddChangePath
DBRequest nh_req(DBRequest::DB_ENTRY_ADD_CHANGE);
nh_req.key.reset(new CompositeNHKey(Composite::LOCAL_ECMP,
false, component_nh_key_list,
vrf()->GetName()));
nh_req.data.reset(new CompositeNHData());
InetUnicastRouteEntry::ModifyEcmpPath(addr_, plen_,
ecmp_path->dest_vn_list(),
ecmp_path->label(), true, vrf()->GetName(),
ecmp_path->sg_list(), ecmp_path->communities(),
ecmp_path->path_preference(),
ecmp_path->tunnel_bmap(),
ecmp_path->ecmp_load_balance(),
nh_req, agent, ecmp_path);
//Make MPLS label point to updated composite NH
MplsLabel::CreateEcmpLabel(agent, ecmp_path->label(), Composite::LOCAL_ECMP,
component_nh_key_list, vrf()->GetName());
RouteInfo rt_info;
FillTrace(rt_info, AgentRoute::CHANGE_PATH, path);
AgentRouteTable *table = static_cast<AgentRouteTable *>(get_table());
OPER_TRACE_ROUTE_ENTRY(Route, table, rt_info);
AGENT_ROUTE_LOG(table, "Path Update", ToString(), vrf()->GetName(),
GETPEERNAME(agent->ecmp_peer()));
return true;
}
void InetUnicastRouteEntry::DeleteComponentNH(Agent *agent, AgentPath *path) {
AgentPath *ecmp_path = FindPath(agent->ecmp_peer());
assert(ecmp_path);
DBEntryBase::KeyPtr key = path->ComputeNextHop(agent)->GetDBRequestKey();
NextHopKey *nh_key = static_cast<NextHopKey *>(key.release());
std::auto_ptr<const NextHopKey> nh_akey(nh_key);
nh_key->SetPolicy(false);
ComponentNHKeyPtr comp_nh_key_ptr(new ComponentNHKey(path->label(), nh_akey));
ComponentNHKeyList component_nh_key_list;
const CompositeNH *comp_nh =
static_cast<const CompositeNH *>(ecmp_path->ComputeNextHop(agent));
component_nh_key_list = comp_nh->DeleteComponentNHKey(comp_nh_key_ptr);
// Form the request for Inet4UnicastEcmpRoute and invoke AddChangePath
DBRequest nh_req(DBRequest::DB_ENTRY_ADD_CHANGE);
nh_req.key.reset(new CompositeNHKey(Composite::LOCAL_ECMP,
false, component_nh_key_list,
vrf()->GetName()));
nh_req.data.reset(new CompositeNHData());
if (!InetUnicastRouteEntry::ModifyEcmpPath(addr_, plen_,
ecmp_path->dest_vn_list(),
ecmp_path->label(), true, vrf()->GetName(),
ecmp_path->sg_list(), ecmp_path->communities(),
ecmp_path->path_preference(),
ecmp_path->tunnel_bmap(),
ecmp_path->ecmp_load_balance(),
nh_req, agent, ecmp_path)) {
return;
}
//Make MPLS label point to composite NH
MplsLabel::CreateEcmpLabel(agent, ecmp_path->label(), Composite::LOCAL_ECMP,
component_nh_key_list, vrf()->GetName());
RouteInfo rt_info;
FillTrace(rt_info, AgentRoute::CHANGE_PATH, path);
AgentRouteTable *table = static_cast<AgentRouteTable *>(get_table());
OPER_TRACE_ROUTE_ENTRY(Route, table, rt_info);
AGENT_ROUTE_LOG(table, "Path change", ToString(), vrf()->GetName(),
GETPEERNAME(agent->ecmp_peer()));
}
const NextHop* InetUnicastRouteEntry::GetLocalNextHop() const {
Agent *agent =
(static_cast<InetUnicastAgentRouteTable *> (get_table()))->agent();
if (FindPath(agent->ecmp_peer())) {
return FindPath(agent->ecmp_peer())->ComputeNextHop(agent);
}
//If a route is leaked, and it points to local composite nexthop
//then choose that
if (GetActivePath()->local_ecmp_mpls_label()) {
return GetActivePath()->local_ecmp_mpls_label()->nexthop();
}
//Choose the first local vm peer path
for (Route::PathList::const_iterator it = GetPathList().begin();
it != GetPathList().end(); it++) {
const AgentPath *path = static_cast<const AgentPath *>(it.operator->());
if (path) {
if (path->peer() &&
path->peer()->GetType() == Peer::LOCAL_VM_PORT_PEER) {
return path->ComputeNextHop(agent);
}
}
}
const NextHop *nh = GetActiveNextHop();
if (nh->GetType() == NextHop::COMPOSITE) {
const CompositeNH *comp_nh = static_cast<const CompositeNH *>(nh);
//Get the local composite NH
return comp_nh->GetLocalNextHop();
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// AgentRouteData virtual functions
/////////////////////////////////////////////////////////////////////////////
bool Inet4UnicastArpRoute::AddChangePath(Agent *agent, AgentPath *path,
const AgentRoute *rt) {
bool ret = false;
ArpNHKey key(vrf_name_, addr_, policy_);
NextHop *nh =
static_cast<NextHop *>(agent->nexthop_table()->FindActiveEntry(&key));
path->set_unresolved(false);
if (path->dest_vn_list() != vn_list_) {
path->set_dest_vn_list(vn_list_);
ret = true;
}
if (path->sg_list() != sg_list_) {
path->set_sg_list(sg_list_);
ret = true;
}
if (path->ChangeNH(agent, nh) == true)
ret = true;
if (nh) {
if (path->CopyArpData()) {
ret = true;
}
}
return ret;
}
bool Inet4UnicastGatewayRoute::AddChangePath(Agent *agent, AgentPath *path,
const AgentRoute *agent_rt) {
path->set_vrf_name(vrf_name_);
InetUnicastAgentRouteTable *table = NULL;
table = static_cast<InetUnicastAgentRouteTable *>
(agent->vrf_table()->GetInet4UnicastRouteTable(vrf_name_));
InetUnicastRouteEntry *rt = table->FindRoute(gw_ip_);
if (rt == NULL || rt->plen() == 0) {
path->set_unresolved(true);
} else if (rt->GetActiveNextHop()->GetType() == NextHop::RESOLVE) {
const ResolveNH *nh =
static_cast<const ResolveNH *>(rt->GetActiveNextHop());