-
Notifications
You must be signed in to change notification settings - Fork 531
/
routeorch.cpp
2129 lines (1836 loc) · 73.8 KB
/
routeorch.cpp
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
#include <assert.h>
#include <inttypes.h>
#include <algorithm>
#include "routeorch.h"
#include "logger.h"
#include "swssnet.h"
#include "crmorch.h"
#include "directory.h"
extern sai_object_id_t gVirtualRouterId;
extern sai_object_id_t gSwitchId;
extern sai_next_hop_group_api_t* sai_next_hop_group_api;
extern sai_route_api_t* sai_route_api;
extern sai_switch_api_t* sai_switch_api;
extern PortsOrch *gPortsOrch;
extern CrmOrch *gCrmOrch;
extern Directory<Orch*> gDirectory;
extern size_t gMaxBulkSize;
/* Default maximum number of next hop groups */
#define DEFAULT_NUMBER_OF_ECMP_GROUPS 128
#define DEFAULT_MAX_ECMP_GROUP_SIZE 32
const int routeorch_pri = 5;
RouteOrch::RouteOrch(DBConnector *db, string tableName, SwitchOrch *switchOrch, NeighOrch *neighOrch, IntfsOrch *intfsOrch, VRFOrch *vrfOrch, FgNhgOrch *fgNhgOrch) :
gRouteBulker(sai_route_api, gMaxBulkSize),
gNextHopGroupMemberBulker(sai_next_hop_group_api, gSwitchId, gMaxBulkSize),
Orch(db, tableName, routeorch_pri),
m_switchOrch(switchOrch),
m_neighOrch(neighOrch),
m_intfsOrch(intfsOrch),
m_vrfOrch(vrfOrch),
m_fgNhgOrch(fgNhgOrch),
m_nextHopGroupCount(0),
m_resync(false)
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
attr.id = SAI_SWITCH_ATTR_NUMBER_OF_ECMP_GROUPS;
sai_status_t status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_WARN("Failed to get switch attribute number of ECMP groups. \
Use default value. rv:%d", status);
m_maxNextHopGroupCount = DEFAULT_NUMBER_OF_ECMP_GROUPS;
}
else
{
m_maxNextHopGroupCount = attr.value.s32;
/*
* ASIC specific workaround to re-calculate maximum ECMP groups
* according to different ECMP mode used.
*
* On Mellanox platform, the maximum ECMP groups returned is the value
* under the condition that the ECMP group size is 1. Dividing this
* number by DEFAULT_MAX_ECMP_GROUP_SIZE gets the maximum number of
* ECMP groups when the maximum ECMP group size is 32.
*/
char *platform = getenv("platform");
if (platform && strstr(platform, MLNX_PLATFORM_SUBSTRING))
{
m_maxNextHopGroupCount /= DEFAULT_MAX_ECMP_GROUP_SIZE;
}
}
vector<FieldValueTuple> fvTuple;
fvTuple.emplace_back("MAX_NEXTHOP_GROUP_COUNT", to_string(m_maxNextHopGroupCount));
m_switchOrch->set_switch_capability(fvTuple);
SWSS_LOG_NOTICE("Maximum number of ECMP groups supported is %d", m_maxNextHopGroupCount);
IpPrefix default_ip_prefix("0.0.0.0/0");
sai_route_entry_t unicast_route_entry;
unicast_route_entry.vr_id = gVirtualRouterId;
unicast_route_entry.switch_id = gSwitchId;
copy(unicast_route_entry.destination, default_ip_prefix);
subnet(unicast_route_entry.destination, unicast_route_entry.destination);
attr.id = SAI_ROUTE_ENTRY_ATTR_PACKET_ACTION;
attr.value.s32 = SAI_PACKET_ACTION_DROP;
status = sai_route_api->create_route_entry(&unicast_route_entry, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create IPv4 default route with packet action drop");
throw runtime_error("Failed to create IPv4 default route with packet action drop");
}
gCrmOrch->incCrmResUsedCounter(CrmResourceType::CRM_IPV4_ROUTE);
/* Add default IPv4 route into the m_syncdRoutes */
m_syncdRoutes[gVirtualRouterId][default_ip_prefix] = NextHopGroupKey();
SWSS_LOG_NOTICE("Create IPv4 default route with packet action drop");
IpPrefix v6_default_ip_prefix("::/0");
copy(unicast_route_entry.destination, v6_default_ip_prefix);
subnet(unicast_route_entry.destination, unicast_route_entry.destination);
status = sai_route_api->create_route_entry(&unicast_route_entry, 1, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create IPv6 default route with packet action drop");
throw runtime_error("Failed to create IPv6 default route with packet action drop");
}
gCrmOrch->incCrmResUsedCounter(CrmResourceType::CRM_IPV6_ROUTE);
/* Add default IPv6 route into the m_syncdRoutes */
m_syncdRoutes[gVirtualRouterId][v6_default_ip_prefix] = NextHopGroupKey();
SWSS_LOG_NOTICE("Create IPv6 default route with packet action drop");
/* All the interfaces have the same MAC address and hence the same
* auto-generated link-local ipv6 address with eui64 interface-id.
* Hence add a single /128 route entry for the link-local interface
* address pointing to the CPU port.
*/
IpPrefix linklocal_prefix = getLinkLocalEui64Addr();
addLinkLocalRouteToMe(gVirtualRouterId, linklocal_prefix);
/* Add fe80::/10 subnet route to forward all link-local packets
* destined to us, to CPU */
IpPrefix default_link_local_prefix("fe80::/10");
addLinkLocalRouteToMe(gVirtualRouterId, default_link_local_prefix);
/* TODO: Add the link-local fe80::/10 route to cpu in every VRF created from
* vrforch::addOperation. */
}
std::string RouteOrch::getLinkLocalEui64Addr(void)
{
SWSS_LOG_ENTER();
string ip_prefix;
const uint8_t *gmac = gMacAddress.getMac();
uint8_t eui64_interface_id[EUI64_INTF_ID_LEN];
char ipv6_ll_addr[INET6_ADDRSTRLEN] = {0};
/* Link-local IPv6 address autogenerated by kernel with eui64 interface-id
* derived from the MAC address of the host interface.
*/
eui64_interface_id[0] = gmac[0] ^ 0x02;
eui64_interface_id[1] = gmac[1];
eui64_interface_id[2] = gmac[2];
eui64_interface_id[3] = 0xff;
eui64_interface_id[4] = 0xfe;
eui64_interface_id[5] = gmac[3];
eui64_interface_id[6] = gmac[4];
eui64_interface_id[7] = gmac[5];
snprintf(ipv6_ll_addr, INET6_ADDRSTRLEN, "fe80::%02x%02x:%02x%02x:%02x%02x:%02x%02x",
eui64_interface_id[0], eui64_interface_id[1], eui64_interface_id[2],
eui64_interface_id[3], eui64_interface_id[4], eui64_interface_id[5],
eui64_interface_id[6], eui64_interface_id[7]);
ip_prefix = string(ipv6_ll_addr);
return ip_prefix;
}
void RouteOrch::addLinkLocalRouteToMe(sai_object_id_t vrf_id, IpPrefix linklocal_prefix)
{
sai_route_entry_t unicast_route_entry;
unicast_route_entry.switch_id = gSwitchId;
unicast_route_entry.vr_id = vrf_id;
copy(unicast_route_entry.destination, linklocal_prefix);
subnet(unicast_route_entry.destination, unicast_route_entry.destination);
sai_attribute_t attr;
vector<sai_attribute_t> attrs;
attr.id = SAI_ROUTE_ENTRY_ATTR_PACKET_ACTION;
attr.value.s32 = SAI_PACKET_ACTION_FORWARD;
attrs.push_back(attr);
Port cpu_port;
gPortsOrch->getCpuPort(cpu_port);
attr.id = SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID;
attr.value.oid = cpu_port.m_port_id;
attrs.push_back(attr);
sai_status_t status = sai_route_api->create_route_entry(&unicast_route_entry, (uint32_t)attrs.size(), attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create link local ipv6 route %s to cpu, rv:%d",
linklocal_prefix.getIp().to_string().c_str(), status);
throw runtime_error("Failed to create link local ipv6 route to cpu.");
}
gCrmOrch->incCrmResUsedCounter(CrmResourceType::CRM_IPV6_ROUTE);
SWSS_LOG_NOTICE("Created link local ipv6 route %s to cpu", linklocal_prefix.to_string().c_str());
}
bool RouteOrch::hasNextHopGroup(const NextHopGroupKey& nexthops) const
{
return m_syncdNextHopGroups.find(nexthops) != m_syncdNextHopGroups.end();
}
sai_object_id_t RouteOrch::getNextHopGroupId(const NextHopGroupKey& nexthops)
{
assert(hasNextHopGroup(nexthops));
return m_syncdNextHopGroups[nexthops].next_hop_group_id;
}
void RouteOrch::attach(Observer *observer, const IpAddress& dstAddr, sai_object_id_t vrf_id)
{
SWSS_LOG_ENTER();
Host host = std::make_pair(vrf_id, dstAddr);
auto observerEntry = m_nextHopObservers.find(host);
/* Create a new observer entry if no current observer is observing this
* IP address */
if (observerEntry == m_nextHopObservers.end())
{
m_nextHopObservers.emplace(host, NextHopObserverEntry());
observerEntry = m_nextHopObservers.find(host);
/* Find the prefixes that cover the destination IP */
if (m_syncdRoutes.find(vrf_id) != m_syncdRoutes.end())
{
for (auto route : m_syncdRoutes.at(vrf_id))
{
if (route.first.isAddressInSubnet(dstAddr))
{
SWSS_LOG_INFO("Prefix %s covers destination address",
route.first.to_string().c_str());
observerEntry->second.routeTable.emplace(
route.first, route.second);
}
}
}
}
observerEntry->second.observers.push_back(observer);
// Trigger next hop change for the first time the observer is attached
// Note that rbegin() is pointing to the entry with longest prefix match
auto route = observerEntry->second.routeTable.rbegin();
if (route != observerEntry->second.routeTable.rend())
{
SWSS_LOG_NOTICE("Attached next hop observer of route %s for destination IP %s",
observerEntry->second.routeTable.rbegin()->first.to_string().c_str(),
dstAddr.to_string().c_str());
NextHopUpdate update = { vrf_id, dstAddr, route->first, route->second };
observer->update(SUBJECT_TYPE_NEXTHOP_CHANGE, static_cast<void *>(&update));
}
}
void RouteOrch::detach(Observer *observer, const IpAddress& dstAddr, sai_object_id_t vrf_id)
{
SWSS_LOG_ENTER();
auto observerEntry = m_nextHopObservers.find(std::make_pair(vrf_id, dstAddr));
if (observerEntry == m_nextHopObservers.end())
{
SWSS_LOG_ERROR("Failed to locate observer for destination IP %s",
dstAddr.to_string().c_str());
assert(false);
return;
}
// Find the observer
for (auto iter = observerEntry->second.observers.begin();
iter != observerEntry->second.observers.end(); ++iter)
{
if (observer == *iter)
{
observerEntry->second.observers.erase(iter);
SWSS_LOG_NOTICE("Detached next hop observer for destination IP %s",
dstAddr.to_string().c_str());
// Remove NextHopObserverEntry if no observer is tracking this
// destination IP.
if (observerEntry->second.observers.empty())
{
m_nextHopObservers.erase(observerEntry);
}
break;
}
}
}
bool RouteOrch::validnexthopinNextHopGroup(const NextHopKey &nexthop, uint32_t& count)
{
SWSS_LOG_ENTER();
sai_object_id_t nexthop_id;
sai_status_t status;
count = 0;
for (auto nhopgroup = m_syncdNextHopGroups.begin();
nhopgroup != m_syncdNextHopGroups.end(); ++nhopgroup)
{
if (!(nhopgroup->first.contains(nexthop)))
{
continue;
}
vector<sai_attribute_t> nhgm_attrs;
sai_attribute_t nhgm_attr;
nhgm_attr.id = SAI_NEXT_HOP_GROUP_MEMBER_ATTR_NEXT_HOP_GROUP_ID;
nhgm_attr.value.oid = nhopgroup->second.next_hop_group_id;
nhgm_attrs.push_back(nhgm_attr);
nhgm_attr.id = SAI_NEXT_HOP_GROUP_MEMBER_ATTR_NEXT_HOP_ID;
nhgm_attr.value.oid = m_neighOrch->getNextHopId(nexthop);
nhgm_attrs.push_back(nhgm_attr);
if (nexthop.weight)
{
nhgm_attr.id = SAI_NEXT_HOP_GROUP_MEMBER_ATTR_WEIGHT;
nhgm_attr.value.s32 = nexthop.weight;
nhgm_attrs.push_back(nhgm_attr);
}
status = sai_next_hop_group_api->create_next_hop_group_member(&nexthop_id, gSwitchId,
(uint32_t)nhgm_attrs.size(),
nhgm_attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to add next hop member to group %" PRIx64 ": %d\n",
nhopgroup->second.next_hop_group_id, status);
task_process_status handle_status = handleSaiCreateStatus(SAI_API_NEXT_HOP_GROUP, status);
if (handle_status != task_success)
{
return parseHandleSaiStatusFailure(handle_status);
}
}
++count;
gCrmOrch->incCrmResUsedCounter(CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER);
nhopgroup->second.nhopgroup_members[nexthop] = nexthop_id;
}
if (!m_fgNhgOrch->validNextHopInNextHopGroup(nexthop))
{
return false;
}
return true;
}
bool RouteOrch::invalidnexthopinNextHopGroup(const NextHopKey &nexthop, uint32_t& count)
{
SWSS_LOG_ENTER();
sai_object_id_t nexthop_id;
sai_status_t status;
count = 0;
for (auto nhopgroup = m_syncdNextHopGroups.begin();
nhopgroup != m_syncdNextHopGroups.end(); ++nhopgroup)
{
if (!(nhopgroup->first.contains(nexthop)))
{
continue;
}
nexthop_id = nhopgroup->second.nhopgroup_members[nexthop];
status = sai_next_hop_group_api->remove_next_hop_group_member(nexthop_id);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to remove next hop member %" PRIx64 " from group %" PRIx64 ": %d\n",
nexthop_id, nhopgroup->second.next_hop_group_id, status);
task_process_status handle_status = handleSaiRemoveStatus(SAI_API_NEXT_HOP_GROUP, status);
if (handle_status != task_success)
{
return parseHandleSaiStatusFailure(handle_status);
}
}
++count;
gCrmOrch->decCrmResUsedCounter(CrmResourceType::CRM_NEXTHOP_GROUP_MEMBER);
}
if (!m_fgNhgOrch->invalidNextHopInNextHopGroup(nexthop))
{
return false;
}
return true;
}
void RouteOrch::doTask(Consumer& consumer)
{
SWSS_LOG_ENTER();
if (!gPortsOrch->allPortsReady())
{
return;
}
auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
// Route bulk results will be stored in a map
std::map<
std::pair<
std::string, // Key
std::string // Op
>,
RouteBulkContext
> toBulk;
// Add or remove routes with a route bulker
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
string key = kfvKey(t);
string op = kfvOp(t);
auto rc = toBulk.emplace(std::piecewise_construct,
std::forward_as_tuple(key, op),
std::forward_as_tuple());
bool inserted = rc.second;
auto& ctx = rc.first->second;
if (!inserted)
{
ctx.clear();
}
/* Get notification from application */
/* resync application:
* When routeorch receives 'resync' message, it marks all current
* routes as dirty and waits for 'resync complete' message. For all
* newly received routes, if they match current dirty routes, it unmarks
* them dirty. After receiving 'resync complete' message, it creates all
* newly added routes and removes all dirty routes.
*/
if (key == "resync")
{
if (op == "SET")
{
/* Mark all current routes as dirty (DEL) in consumer.m_toSync map */
SWSS_LOG_NOTICE("Start resync routes\n");
for (auto j : m_syncdRoutes)
{
string vrf;
if (j.first != gVirtualRouterId)
{
vrf = m_vrfOrch->getVRFname(j.first) + ":";
}
for (auto i : j.second)
{
vector<FieldValueTuple> v;
key = vrf + i.first.to_string();
auto x = KeyOpFieldsValuesTuple(key, DEL_COMMAND, v);
consumer.addToSync(x);
}
}
m_resync = true;
}
else
{
SWSS_LOG_NOTICE("Complete resync routes\n");
m_resync = false;
}
it = consumer.m_toSync.erase(it);
continue;
}
if (m_resync)
{
it++;
continue;
}
sai_object_id_t& vrf_id = ctx.vrf_id;
IpPrefix& ip_prefix = ctx.ip_prefix;
if (!key.compare(0, strlen(VRF_PREFIX), VRF_PREFIX))
{
size_t found = key.find(':');
string vrf_name = key.substr(0, found);
if (!m_vrfOrch->isVRFexists(vrf_name))
{
it++;
continue;
}
vrf_id = m_vrfOrch->getVRFid(vrf_name);
ip_prefix = IpPrefix(key.substr(found+1));
}
else
{
vrf_id = gVirtualRouterId;
ip_prefix = IpPrefix(key);
}
if (op == SET_COMMAND)
{
string ips;
string aliases;
string vni_labels;
string remote_macs;
string weights;
bool& excp_intfs_flag = ctx.excp_intfs_flag;
bool overlay_nh = false;
bool blackhole = false;
for (auto i : kfvFieldsValues(t))
{
if (fvField(i) == "nexthop")
ips = fvValue(i);
if (fvField(i) == "ifname")
aliases = fvValue(i);
if (fvField(i) == "vni_label") {
vni_labels = fvValue(i);
overlay_nh = true;
}
if (fvField(i) == "router_mac")
remote_macs = fvValue(i);
if (fvField(i) == "blackhole")
blackhole = fvValue(i) == "true";
if (fvField(i) == "weight")
weights = fvValue(i);
}
vector<string>& ipv = ctx.ipv;
ipv = tokenize(ips, ',');
vector<string> alsv = tokenize(aliases, ',');
vector<string> vni_labelv = tokenize(vni_labels, ',');
vector<string> rmacv = tokenize(remote_macs, ',');
/*
* For backward compatibility, adjust ip string from old format to
* new format. Meanwhile it can deal with some abnormal cases.
*/
/* Resize the ip vector to match ifname vector
* as tokenize(",", ',') will miss the last empty segment. */
if (alsv.size() == 0 && !blackhole)
{
SWSS_LOG_WARN("Skip the route %s, for it has an empty ifname field.", key.c_str());
it = consumer.m_toSync.erase(it);
continue;
}
else if (alsv.size() != ipv.size())
{
SWSS_LOG_NOTICE("Route %s: resize ipv to match alsv, %zd -> %zd.", key.c_str(), ipv.size(), alsv.size());
ipv.resize(alsv.size());
}
/* Set the empty ip(s) to zero
* as IpAddress("") will construct a incorrect ip. */
for (auto &ip : ipv)
{
if (ip.empty())
{
SWSS_LOG_NOTICE("Route %s: set the empty nexthop ip to zero.", key.c_str());
ip = ip_prefix.isV4() ? "0.0.0.0" : "::";
}
}
for (auto alias : alsv)
{
/* skip route to management, docker, loopback
* TODO: for route to loopback interface, the proper
* way is to create loopback interface and then create
* route pointing to it, so that we can traps packets to
* CPU */
if (alias == "eth0" || alias == "docker0" ||
alias == "lo" || !alias.compare(0, strlen(LOOPBACK_PREFIX), LOOPBACK_PREFIX))
{
excp_intfs_flag = true;
break;
}
}
// TODO: cannot trust m_portsOrch->getPortIdByAlias because sometimes alias is empty
if (excp_intfs_flag)
{
/* If any existing routes are updated to point to the
* above interfaces, remove them from the ASIC. */
if (removeRoute(ctx))
it = consumer.m_toSync.erase(it);
else
it++;
continue;
}
string nhg_str = "";
NextHopGroupKey& nhg = ctx.nhg;
if (blackhole)
{
nhg = NextHopGroupKey();
}
else if (overlay_nh == false)
{
if (alsv[0] == "tun0" && !(IpAddress(ipv[0]).isZero()))
{
alsv[0] = gIntfsOrch->getRouterIntfsAlias(ipv[0]);
}
nhg_str = ipv[0] + NH_DELIMITER + alsv[0];
for (uint32_t i = 1; i < ipv.size(); i++)
{
if (alsv[i] == "tun0" && !(IpAddress(ipv[i]).isZero()))
{
alsv[i] = gIntfsOrch->getRouterIntfsAlias(ipv[i]);
}
nhg_str += NHG_DELIMITER + ipv[i] + NH_DELIMITER + alsv[i];
}
nhg = NextHopGroupKey(nhg_str, weights);
}
else
{
nhg_str = ipv[0] + NH_DELIMITER + "vni" + alsv[0] + NH_DELIMITER + vni_labelv[0] + NH_DELIMITER + rmacv[0];
for (uint32_t i = 1; i < ipv.size(); i++)
{
nhg_str += NHG_DELIMITER + ipv[i] + NH_DELIMITER + "vni" + alsv[i] + NH_DELIMITER + vni_labelv[i] + NH_DELIMITER + rmacv[i];
}
nhg = NextHopGroupKey(nhg_str, overlay_nh);
}
if (ipv.size() == 1 && IpAddress(ipv[0]).isZero())
{
if (alsv[0] == "unknown")
{
it = consumer.m_toSync.erase(it);
}
/* skip direct routes to tun0 */
else if (alsv[0] == "tun0")
{
it = consumer.m_toSync.erase(it);
}
/* directly connected route to VRF interface which come from kernel */
else if (!alsv[0].compare(0, strlen(VRF_PREFIX), VRF_PREFIX))
{
it = consumer.m_toSync.erase(it);
}
/* skip prefix which is linklocal or multicast */
else if (ip_prefix.getIp().getAddrScope() != IpAddress::GLOBAL_SCOPE)
{
it = consumer.m_toSync.erase(it);
}
/* fullmask subnet route is same as ip2me route */
else if (ip_prefix.isFullMask() && m_intfsOrch->isPrefixSubnet(ip_prefix, alsv[0]))
{
it = consumer.m_toSync.erase(it);
}
/* subnet route, vrf leaked route, etc */
else
{
if (addRoute(ctx, nhg))
it = consumer.m_toSync.erase(it);
else
it++;
}
}
else if (m_syncdRoutes.find(vrf_id) == m_syncdRoutes.end() ||
m_syncdRoutes.at(vrf_id).find(ip_prefix) == m_syncdRoutes.at(vrf_id).end() ||
m_syncdRoutes.at(vrf_id).at(ip_prefix) != nhg)
{
if (addRoute(ctx, nhg))
it = consumer.m_toSync.erase(it);
else
it++;
}
else
/* Duplicate entry */
it = consumer.m_toSync.erase(it);
// If already exhaust the nexthop groups, and there are pending removing routes in bulker,
// flush the bulker and possibly collect some released nexthop groups
if (m_nextHopGroupCount >= m_maxNextHopGroupCount && gRouteBulker.removing_entries_count() > 0)
{
break;
}
}
else if (op == DEL_COMMAND)
{
if (removeRoute(ctx))
it = consumer.m_toSync.erase(it);
else
it++;
}
else
{
SWSS_LOG_ERROR("Unknown operation type %s\n", op.c_str());
it = consumer.m_toSync.erase(it);
}
}
// Flush the route bulker, so routes will be written to syncd and ASIC
gRouteBulker.flush();
// Go through the bulker results
auto it_prev = consumer.m_toSync.begin();
m_bulkNhgReducedRefCnt.clear();
while (it_prev != it)
{
KeyOpFieldsValuesTuple t = it_prev->second;
string key = kfvKey(t);
string op = kfvOp(t);
auto found = toBulk.find(make_pair(key, op));
if (found == toBulk.end())
{
it_prev++;
continue;
}
const auto& ctx = found->second;
const auto& object_statuses = ctx.object_statuses;
if (object_statuses.empty())
{
it_prev++;
continue;
}
const sai_object_id_t& vrf_id = ctx.vrf_id;
const IpPrefix& ip_prefix = ctx.ip_prefix;
if (op == SET_COMMAND)
{
const bool& excp_intfs_flag = ctx.excp_intfs_flag;
const vector<string>& ipv = ctx.ipv;
if (excp_intfs_flag)
{
/* If any existing routes are updated to point to the
* above interfaces, remove them from the ASIC. */
if (removeRoutePost(ctx))
it_prev = consumer.m_toSync.erase(it_prev);
else
it_prev++;
continue;
}
const NextHopGroupKey& nhg = ctx.nhg;
if (ipv.size() == 1 && IpAddress(ipv[0]).isZero())
{
if (addRoutePost(ctx, nhg))
it_prev = consumer.m_toSync.erase(it_prev);
else
it_prev++;
}
else if (m_syncdRoutes.find(vrf_id) == m_syncdRoutes.end() ||
m_syncdRoutes.at(vrf_id).find(ip_prefix) == m_syncdRoutes.at(vrf_id).end() ||
m_syncdRoutes.at(vrf_id).at(ip_prefix) != nhg)
{
if (addRoutePost(ctx, nhg))
it_prev = consumer.m_toSync.erase(it_prev);
else
it_prev++;
}
}
else if (op == DEL_COMMAND)
{
/* Cannot locate the route or remove succeed */
if (removeRoutePost(ctx))
it_prev = consumer.m_toSync.erase(it_prev);
else
it_prev++;
}
}
/* Remove next hop group if the reference count decreases to zero */
for (auto& it_nhg : m_bulkNhgReducedRefCnt)
{
if (it_nhg.first.is_overlay_nexthop() && it_nhg.second != 0)
{
removeOverlayNextHops(it_nhg.second, it_nhg.first);
}
else if (m_syncdNextHopGroups[it_nhg.first].ref_count == 0)
{
removeNextHopGroup(it_nhg.first);
}
}
}
}
void RouteOrch::notifyNextHopChangeObservers(sai_object_id_t vrf_id, const IpPrefix &prefix, const NextHopGroupKey &nexthops, bool add)
{
SWSS_LOG_ENTER();
for (auto& entry : m_nextHopObservers)
{
if (vrf_id != entry.first.first || !prefix.isAddressInSubnet(entry.first.second))
{
continue;
}
if (add)
{
bool update_required = false;
NextHopUpdate update = { vrf_id, entry.first.second, prefix, nexthops };
/* Table should not be empty. Default route should always exists. */
assert(!entry.second.routeTable.empty());
auto route = entry.second.routeTable.find(prefix);
if (route == entry.second.routeTable.end())
{
/* If added route is best match update observers */
if (entry.second.routeTable.rbegin()->first < prefix)
{
update_required = true;
}
entry.second.routeTable.emplace(prefix, nexthops);
}
else
{
if (route->second != nexthops)
{
route->second = nexthops;
/* If changed route is best match update observers */
if (entry.second.routeTable.rbegin()->first == route->first)
{
update_required = true;
}
}
}
if (update_required)
{
for (auto observer : entry.second.observers)
{
observer->update(SUBJECT_TYPE_NEXTHOP_CHANGE, static_cast<void *>(&update));
}
}
}
else
{
auto route = entry.second.routeTable.find(prefix);
if (route != entry.second.routeTable.end())
{
/* If removed route was best match find another best match route */
if (route->first == entry.second.routeTable.rbegin()->first)
{
entry.second.routeTable.erase(route);
/* Table should not be empty. Default route should always exists. */
assert(!entry.second.routeTable.empty());
auto route = entry.second.routeTable.rbegin();
NextHopUpdate update = { vrf_id, entry.first.second, route->first, route->second };
for (auto observer : entry.second.observers)
{
observer->update(SUBJECT_TYPE_NEXTHOP_CHANGE, static_cast<void *>(&update));
}
}
else
{
entry.second.routeTable.erase(route);
}
}
}
}
}
void RouteOrch::increaseNextHopRefCount(const NextHopGroupKey &nexthops)
{
/* Return when there is no next hop (dropped) */
if (nexthops.getSize() == 0)
{
return;
}
else if (nexthops.getSize() == 1)
{
NextHopKey nexthop;
bool overlay_nh = nexthops.is_overlay_nexthop();
if (overlay_nh)
{
nexthop = NextHopKey (nexthops.to_string(), overlay_nh);
}
else
{
nexthop = NextHopKey (nexthops.to_string());
}
if (nexthop.ip_address.isZero())
m_intfsOrch->increaseRouterIntfsRefCount(nexthop.alias);
else
m_neighOrch->increaseNextHopRefCount(nexthop);
}
else
{
m_syncdNextHopGroups[nexthops].ref_count ++;
}
}
void RouteOrch::decreaseNextHopRefCount(const NextHopGroupKey &nexthops)
{
/* Return when there is no next hop (dropped) */
if (nexthops.getSize() == 0)
{
return;
}
else if (nexthops.getSize() == 1)
{
NextHopKey nexthop;
bool overlay_nh = nexthops.is_overlay_nexthop();
if (overlay_nh)
{
nexthop = NextHopKey (nexthops.to_string(), overlay_nh);
}
else
{
nexthop = NextHopKey (nexthops.to_string());
}
if (nexthop.ip_address.isZero())
m_intfsOrch->decreaseRouterIntfsRefCount(nexthop.alias);
else
m_neighOrch->decreaseNextHopRefCount(nexthop);
}
else
{
m_syncdNextHopGroups[nexthops].ref_count --;
}
}
bool RouteOrch::isRefCounterZero(const NextHopGroupKey &nexthops) const
{
if (!hasNextHopGroup(nexthops))
{
return true;
}
return m_syncdNextHopGroups.at(nexthops).ref_count == 0;
}
const NextHopGroupKey RouteOrch::getSyncdRouteNhgKey(sai_object_id_t vrf_id, const IpPrefix& ipPrefix)
{
NextHopGroupKey nhg;
auto route_table = m_syncdRoutes.find(vrf_id);
if (route_table != m_syncdRoutes.end())
{
auto route_entry = route_table->second.find(ipPrefix);
if (route_entry != route_table->second.end())
{
nhg = route_entry->second;
}
}
return nhg;
}
bool RouteOrch::createFineGrainedNextHopGroup(sai_object_id_t &next_hop_group_id, vector<sai_attribute_t> &nhg_attrs)
{
SWSS_LOG_ENTER();
if (m_nextHopGroupCount >= m_maxNextHopGroupCount)
{
SWSS_LOG_DEBUG("Failed to create new next hop group. \
Reaching maximum number of next hop groups.");
return false;
}
sai_status_t status = sai_next_hop_group_api->create_next_hop_group(&next_hop_group_id,
gSwitchId,
(uint32_t)nhg_attrs.size(),
nhg_attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to create next hop group rv:%d", status);
task_process_status handle_status = handleSaiCreateStatus(SAI_API_NEXT_HOP_GROUP, status);
if (handle_status != task_success)
{
return parseHandleSaiStatusFailure(handle_status);