-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy pathvxlanorch.cpp
2644 lines (2208 loc) · 80.6 KB
/
vxlanorch.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 <cassert>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <stdexcept>
#include <inttypes.h>
extern "C" {
#include "sai.h"
}
#include "macaddress.h"
#include "ipaddress.h"
#include "orch.h"
#include "request_parser.h"
#include "vxlanorch.h"
#include "directory.h"
#include "swssnet.h"
#include "warm_restart.h"
#include "tokenize.h"
#include "sai_serialize.h"
#include "flex_counter_manager.h"
#include "converter.h"
/* Global variables */
extern sai_object_id_t gSwitchId;
extern sai_object_id_t gVirtualRouterId;
extern sai_tunnel_api_t *sai_tunnel_api;
extern sai_next_hop_api_t *sai_next_hop_api;
extern Directory<Orch*> gDirectory;
extern PortsOrch* gPortsOrch;
extern sai_object_id_t gUnderlayIfId;
extern FlexManagerDirectory g_FlexManagerDirectory;
#define FLEX_COUNTER_UPD_INTERVAL 1
const map<MAP_T, uint32_t> vxlanTunnelMap =
{
{ MAP_T::VNI_TO_VLAN_ID, SAI_TUNNEL_MAP_TYPE_VNI_TO_VLAN_ID },
{ MAP_T::VLAN_ID_TO_VNI, SAI_TUNNEL_MAP_TYPE_VLAN_ID_TO_VNI },
{ MAP_T::VRID_TO_VNI, SAI_TUNNEL_MAP_TYPE_VIRTUAL_ROUTER_ID_TO_VNI },
{ MAP_T::VNI_TO_VRID, SAI_TUNNEL_MAP_TYPE_VNI_TO_VIRTUAL_ROUTER_ID },
{ MAP_T::BRIDGE_TO_VNI, SAI_TUNNEL_MAP_TYPE_BRIDGE_IF_TO_VNI },
{ MAP_T::VNI_TO_BRIDGE, SAI_TUNNEL_MAP_TYPE_VNI_TO_BRIDGE_IF},
};
const map<MAP_T, std::pair<uint32_t, uint32_t>> vxlanTunnelMapKeyVal =
{
{ MAP_T::VNI_TO_VLAN_ID,
{ SAI_TUNNEL_MAP_ENTRY_ATTR_VNI_ID_KEY, SAI_TUNNEL_MAP_ENTRY_ATTR_VLAN_ID_VALUE }
},
{ MAP_T::VLAN_ID_TO_VNI,
{ SAI_TUNNEL_MAP_ENTRY_ATTR_VLAN_ID_KEY, SAI_TUNNEL_MAP_ENTRY_ATTR_VNI_ID_VALUE }
},
{ MAP_T::VRID_TO_VNI,
{ SAI_TUNNEL_MAP_ENTRY_ATTR_VIRTUAL_ROUTER_ID_KEY, SAI_TUNNEL_MAP_ENTRY_ATTR_VNI_ID_VALUE }
},
{ MAP_T::VNI_TO_VRID,
{ SAI_TUNNEL_MAP_ENTRY_ATTR_VNI_ID_KEY, SAI_TUNNEL_MAP_ENTRY_ATTR_VIRTUAL_ROUTER_ID_VALUE }
},
{ MAP_T::BRIDGE_TO_VNI,
{ SAI_TUNNEL_MAP_ENTRY_ATTR_BRIDGE_ID_KEY, SAI_TUNNEL_MAP_ENTRY_ATTR_VNI_ID_VALUE }
},
{ MAP_T::VNI_TO_BRIDGE,
{ SAI_TUNNEL_MAP_ENTRY_ATTR_VNI_ID_KEY, SAI_TUNNEL_MAP_ENTRY_ATTR_BRIDGE_ID_VALUE }
},
};
const vector<sai_tunnel_stat_t> tunnel_stat_ids =
{
SAI_TUNNEL_STAT_IN_OCTETS,
SAI_TUNNEL_STAT_IN_PACKETS,
SAI_TUNNEL_STAT_OUT_OCTETS,
SAI_TUNNEL_STAT_OUT_PACKETS
};
/*
* Manipulators for the above Map
*/
static inline uint32_t tunnel_map_type (MAP_T map_t)
{
return vxlanTunnelMap.at(map_t);
}
static inline uint32_t tunnel_map_key (MAP_T map_t)
{
return vxlanTunnelMapKeyVal.at(map_t).first;
}
static inline uint32_t tunnel_map_val (MAP_T map_t)
{
return vxlanTunnelMapKeyVal.at(map_t).second;
}
static inline MAP_T tunnel_map_type (tunnel_map_type_t type, bool isencap)
{
if (isencap)
{
switch(type)
{
case TUNNEL_MAP_T_VLAN : return MAP_T::VLAN_ID_TO_VNI;
case TUNNEL_MAP_T_VIRTUAL_ROUTER: return MAP_T::VRID_TO_VNI;
case TUNNEL_MAP_T_BRIDGE: return MAP_T::BRIDGE_TO_VNI;
default: return MAP_T::MAP_TO_INVALID;
}
}
else
{
switch(type)
{
case TUNNEL_MAP_T_VLAN : return MAP_T::VNI_TO_VLAN_ID;
case TUNNEL_MAP_T_VIRTUAL_ROUTER: return MAP_T::VNI_TO_VRID;
case TUNNEL_MAP_T_BRIDGE: return MAP_T::VNI_TO_BRIDGE;
default: return MAP_T::MAP_TO_INVALID;
}
}
}
//------------------- SAI Interface functions --------------------------//
static sai_object_id_t
create_tunnel_map(MAP_T map_t)
{
sai_attribute_t attr;
std::vector<sai_attribute_t> tunnel_map_attrs;
if (map_t == MAP_T::MAP_TO_INVALID)
{
SWSS_LOG_ERROR("Invalid map type %d", static_cast<int>(map_t));
return SAI_NULL_OBJECT_ID;
}
attr.id = SAI_TUNNEL_MAP_ATTR_TYPE;
attr.value.s32 = tunnel_map_type(map_t);
tunnel_map_attrs.push_back(attr);
sai_object_id_t tunnel_map_id;
sai_status_t status = sai_tunnel_api->create_tunnel_map(
&tunnel_map_id,
gSwitchId,
static_cast<uint32_t>(tunnel_map_attrs.size()),
tunnel_map_attrs.data()
);
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't create tunnel map object");
}
return tunnel_map_id;
}
void
remove_tunnel_map(sai_object_id_t tunnel_map_id)
{
sai_status_t status = sai_tunnel_api->remove_tunnel_map(tunnel_map_id);
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't remove a tunnel map object");
}
}
static sai_object_id_t create_tunnel_map_entry(
MAP_T map_t,
sai_object_id_t tunnel_map_id,
sai_uint32_t vni,
sai_uint16_t vlan_id,
sai_object_id_t obj_id=SAI_NULL_OBJECT_ID,
bool encap=false
)
{
sai_attribute_t attr;
sai_object_id_t tunnel_map_entry_id;
std::vector<sai_attribute_t> tunnel_map_entry_attrs;
attr.id = SAI_TUNNEL_MAP_ENTRY_ATTR_TUNNEL_MAP_TYPE;
attr.value.s32 = tunnel_map_type(map_t);
tunnel_map_entry_attrs.push_back(attr);
attr.id = SAI_TUNNEL_MAP_ENTRY_ATTR_TUNNEL_MAP;
attr.value.oid = tunnel_map_id;
tunnel_map_entry_attrs.push_back(attr);
attr.id = (encap)? tunnel_map_key(map_t):tunnel_map_val(map_t);
if (obj_id != SAI_NULL_OBJECT_ID)
{
attr.value.oid = obj_id;
}
else
{
attr.value.u16 = vlan_id;
}
tunnel_map_entry_attrs.push_back(attr);
attr.id = (encap)? tunnel_map_val(map_t):tunnel_map_key(map_t);
attr.value.u32 = vni;
tunnel_map_entry_attrs.push_back(attr);
sai_status_t status = sai_tunnel_api->create_tunnel_map_entry(&tunnel_map_entry_id, gSwitchId,
static_cast<uint32_t> (tunnel_map_entry_attrs.size()),
tunnel_map_entry_attrs.data());
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't create a tunnel map entry object");
}
return tunnel_map_entry_id;
}
void remove_tunnel_map_entry(sai_object_id_t obj_id)
{
sai_status_t status = SAI_STATUS_SUCCESS;
if (obj_id != SAI_NULL_OBJECT_ID)
{
status = sai_tunnel_api->remove_tunnel_map_entry(obj_id);
}
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't delete a tunnel map entry object");
}
}
static sai_status_t create_nexthop_tunnel(
sai_ip_address_t host_ip,
sai_uint32_t vni, // optional vni
sai_mac_t *mac, // inner destination mac
sai_object_id_t tunnel_id,
sai_object_id_t *next_hop_id)
{
std::vector<sai_attribute_t> next_hop_attrs;
sai_attribute_t next_hop_attr;
next_hop_attr.id = SAI_NEXT_HOP_ATTR_TYPE;
next_hop_attr.value.s32 = SAI_NEXT_HOP_TYPE_TUNNEL_ENCAP;
next_hop_attrs.push_back(next_hop_attr);
next_hop_attr.id = SAI_NEXT_HOP_ATTR_IP;
next_hop_attr.value.ipaddr = host_ip;
next_hop_attrs.push_back(next_hop_attr);
next_hop_attr.id = SAI_NEXT_HOP_ATTR_TUNNEL_ID;
next_hop_attr.value.oid = tunnel_id;
next_hop_attrs.push_back(next_hop_attr);
if (vni != 0)
{
next_hop_attr.id = SAI_NEXT_HOP_ATTR_TUNNEL_VNI;
next_hop_attr.value.u32 = vni;
next_hop_attrs.push_back(next_hop_attr);
}
if (mac != nullptr)
{
next_hop_attr.id = SAI_NEXT_HOP_ATTR_TUNNEL_MAC;
memcpy(next_hop_attr.value.mac, mac, sizeof(sai_mac_t));
next_hop_attrs.push_back(next_hop_attr);
}
sai_status_t status = sai_next_hop_api->create_next_hop(next_hop_id, gSwitchId,
static_cast<uint32_t>(next_hop_attrs.size()),
next_hop_attrs.data());
return status;
}
// Create Tunnel
static sai_object_id_t
create_tunnel(
struct tunnel_ids_t* ids,
sai_ip_address_t *src_ip,
sai_ip_address_t *dst_ip,
sai_object_id_t underlay_rif,
bool p2p,
sai_uint8_t encap_ttl=0)
{
sai_attribute_t attr;
std::vector<sai_attribute_t> tunnel_attrs;
attr.id = SAI_TUNNEL_ATTR_TYPE;
attr.value.s32 = SAI_TUNNEL_TYPE_VXLAN;
tunnel_attrs.push_back(attr);
attr.id = SAI_TUNNEL_ATTR_UNDERLAY_INTERFACE;
attr.value.oid = underlay_rif;
tunnel_attrs.push_back(attr);
sai_object_id_t map_list[TUNNEL_MAP_T_MAX_MAPPER+1];
uint8_t num_map=0;
for (int i=TUNNEL_MAP_T_VLAN;i<TUNNEL_MAP_T_MAX_MAPPER;i++)
{
if (ids->tunnel_decap_id[i] != SAI_NULL_OBJECT_ID)
{
map_list[num_map] = ids->tunnel_decap_id[i];
SWSS_LOG_INFO("create_tunnel:maplist[%d]=0x%" PRIx64 "",num_map,map_list[num_map]);
num_map++;
}
}
attr.id = SAI_TUNNEL_ATTR_DECAP_MAPPERS;
attr.value.objlist.count = num_map;
attr.value.objlist.list = map_list;
tunnel_attrs.push_back(attr);
sai_object_id_t emap_list[TUNNEL_MAP_T_MAX_MAPPER+1];
uint8_t num_emap=0;
for (int i=TUNNEL_MAP_T_VLAN;i<TUNNEL_MAP_T_MAX_MAPPER;i++)
{
if (ids->tunnel_encap_id[i] != SAI_NULL_OBJECT_ID)
{
emap_list[num_emap] = ids->tunnel_encap_id[i];
SWSS_LOG_NOTICE("create_tunnel:encapmaplist[%d]=0x%" PRIx64 "",num_emap,emap_list[num_emap]);
num_emap++;
}
}
attr.id = SAI_TUNNEL_ATTR_ENCAP_MAPPERS;
attr.value.objlist.count = num_emap;
attr.value.objlist.list = emap_list;
tunnel_attrs.push_back(attr);
// source ip
if (src_ip != nullptr)
{
attr.id = SAI_TUNNEL_ATTR_ENCAP_SRC_IP;
attr.value.ipaddr = *src_ip;
tunnel_attrs.push_back(attr);
}
// dest ip
if ((dst_ip != nullptr) && p2p)
{
attr.id = SAI_TUNNEL_ATTR_PEER_MODE;
attr.value.s32 = SAI_TUNNEL_PEER_MODE_P2P;
tunnel_attrs.push_back(attr);
attr.id = SAI_TUNNEL_ATTR_ENCAP_DST_IP;
attr.value.ipaddr = *dst_ip;
tunnel_attrs.push_back(attr);
}
else
{
attr.id = SAI_TUNNEL_ATTR_PEER_MODE;
attr.value.s32 = SAI_TUNNEL_PEER_MODE_P2MP;
tunnel_attrs.push_back(attr);
}
if (encap_ttl != 0)
{
attr.id = SAI_TUNNEL_ATTR_ENCAP_TTL_MODE;
attr.value.s32 = SAI_TUNNEL_TTL_MODE_PIPE_MODEL;
tunnel_attrs.push_back(attr);
attr.id = SAI_TUNNEL_ATTR_ENCAP_TTL_VAL;
attr.value.u8 = encap_ttl;
tunnel_attrs.push_back(attr);
}
sai_object_id_t tunnel_id;
sai_status_t status = sai_tunnel_api->create_tunnel(
&tunnel_id,
gSwitchId,
static_cast<uint32_t>(tunnel_attrs.size()),
tunnel_attrs.data()
);
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't create a tunnel object");
}
return tunnel_id;
}
void
remove_tunnel(sai_object_id_t tunnel_id)
{
if (tunnel_id != SAI_NULL_OBJECT_ID)
{
sai_status_t status = sai_tunnel_api->remove_tunnel(tunnel_id);
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't remove a tunnel object");
}
}
else
{
SWSS_LOG_DEBUG("Tunnel id is NULL.");
}
}
// Create tunnel termination
static sai_object_id_t
create_tunnel_termination(
sai_object_id_t tunnel_oid,
sai_ip_address_t srcip,
sai_ip_address_t *dstip,
sai_object_id_t default_vrid)
{
sai_attribute_t attr;
std::vector<sai_attribute_t> tunnel_attrs;
if (dstip == nullptr) // It's P2MP tunnel
{
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_TYPE;
attr.value.s32 = SAI_TUNNEL_TERM_TABLE_ENTRY_TYPE_P2MP;
tunnel_attrs.push_back(attr);
}
else
{
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_TYPE;
attr.value.s32 = SAI_TUNNEL_TERM_TABLE_ENTRY_TYPE_P2P;
tunnel_attrs.push_back(attr);
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_SRC_IP;
attr.value.ipaddr = *dstip;
tunnel_attrs.push_back(attr);
}
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_VR_ID;
attr.value.oid = default_vrid;
tunnel_attrs.push_back(attr);
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_DST_IP;
attr.value.ipaddr = srcip;
tunnel_attrs.push_back(attr);
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_TUNNEL_TYPE;
attr.value.s32 = SAI_TUNNEL_TYPE_VXLAN;
tunnel_attrs.push_back(attr);
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_ACTION_TUNNEL_ID;
attr.value.oid = tunnel_oid;
tunnel_attrs.push_back(attr);
sai_object_id_t term_table_id;
sai_status_t status = sai_tunnel_api->create_tunnel_term_table_entry(
&term_table_id,
gSwitchId,
static_cast<uint32_t>(tunnel_attrs.size()),
tunnel_attrs.data()
);
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't create a tunnel term table object");
}
return term_table_id;
}
void
remove_tunnel_termination(sai_object_id_t term_table_id)
{
if (term_table_id != SAI_NULL_OBJECT_ID)
{
sai_status_t status = sai_tunnel_api->remove_tunnel_term_table_entry(term_table_id);
if (status != SAI_STATUS_SUCCESS)
{
throw std::runtime_error("Can't remove a tunnel term table object");
}
}
else
{
SWSS_LOG_DEBUG("Tunnel term table id is NULL.");
}
}
//------------------- VxlanTunnel Implementation --------------------------//
VxlanTunnel::VxlanTunnel(string name, IpAddress srcIp, IpAddress dstIp, tunnel_creation_src_t src)
:tunnel_name_(name), src_ip_(srcIp), dst_ip_(dstIp), src_creation_(src)
{
VxlanTunnelOrch* tunnel_orch = gDirectory.get<VxlanTunnelOrch*>();
if (dstIp.isZero())
{
tunnel_orch->addVTEP(this, srcIp);
vtep_ptr = NULL;
}
else if (src_creation_ == TNL_CREATION_SRC_EVPN)
{
vtep_ptr = tunnel_orch->getVTEP(srcIp);
tunnel_orch->addRemoveStateTableEntry(name,srcIp, dstIp,
src, true);
}
}
VxlanTunnel::~VxlanTunnel()
{
VxlanTunnelOrch* tunnel_orch = gDirectory.get<VxlanTunnelOrch*>();
tunnel_orch->addRemoveStateTableEntry(tunnel_name_,src_ip_, dst_ip_,
src_creation_, false);
}
bool VxlanTunnel::createTunnel(MAP_T encap, MAP_T decap, uint8_t encap_ttl)
{
try
{
VxlanTunnelOrch* tunnel_orch = gDirectory.get<VxlanTunnelOrch*>();
sai_ip_address_t ips, ipd, *ip=nullptr;
uint8_t mapper_list = 0;
swss::copy(ips, src_ip_);
// Only a single mapper type is created
if (decap == MAP_T::VNI_TO_BRIDGE)
{
TUNNELMAP_SET_BRIDGE(mapper_list);
}
else if (decap == MAP_T::VNI_TO_VLAN_ID)
{
TUNNELMAP_SET_VLAN(mapper_list);
}
else
{
TUNNELMAP_SET_VRF(mapper_list);
}
createMapperHw(mapper_list, (encap == MAP_T::MAP_TO_INVALID) ?
TUNNEL_MAP_USE_DECAP_ONLY: TUNNEL_MAP_USE_DEDICATED_ENCAP_DECAP);
if (encap != MAP_T::MAP_TO_INVALID)
{
ip = &ips;
}
ids_.tunnel_id = create_tunnel(&ids_, ip, NULL, gUnderlayIfId, false, encap_ttl);
if (ids_.tunnel_id != SAI_NULL_OBJECT_ID)
{
tunnel_orch->addTunnelToFlexCounter(ids_.tunnel_id, tunnel_name_);
}
ip = nullptr;
if (!dst_ip_.isZero())
{
swss::copy(ipd, dst_ip_);
ip = &ipd;
}
ids_.tunnel_term_id = create_tunnel_termination(ids_.tunnel_id, ips, ip, gVirtualRouterId);
active_ = true;
tunnel_map_ = { encap, decap };
}
catch (const std::runtime_error& error)
{
SWSS_LOG_ERROR("Error creating tunnel %s: %s", tunnel_name_.c_str(), error.what());
// FIXME: add code to remove already created objects
return false;
}
SWSS_LOG_NOTICE("Vxlan tunnel '%s' was created", tunnel_name_.c_str());
return true;
}
sai_object_id_t VxlanTunnel::addEncapMapperEntry(sai_object_id_t obj, uint32_t vni, tunnel_map_type_t type)
{
const auto encap_id = getEncapMapId(type);
const auto map_t = tunnel_map_type(type,true);
return create_tunnel_map_entry(map_t, encap_id, vni, 0, obj, true);
}
sai_object_id_t VxlanTunnel::addDecapMapperEntry(sai_object_id_t obj, uint32_t vni, tunnel_map_type_t type)
{
const auto decap_id = getDecapMapId(type);
const auto map_t = tunnel_map_type(type,false);
return create_tunnel_map_entry(map_t, decap_id, vni, 0, obj);
}
void VxlanTunnel::insertMapperEntry(sai_object_id_t encap, sai_object_id_t decap, uint32_t vni)
{
tunnel_map_entries_[vni] = std::pair<sai_object_id_t, sai_object_id_t>(encap, decap);
}
std::pair<sai_object_id_t, sai_object_id_t> VxlanTunnel::getMapperEntry(uint32_t vni)
{
if (tunnel_map_entries_.find(vni) != tunnel_map_entries_.end())
{
return tunnel_map_entries_[vni];
}
return std::make_pair(SAI_NULL_OBJECT_ID, SAI_NULL_OBJECT_ID);
}
void VxlanTunnel::updateNextHop(IpAddress& ipAddr, MacAddress macAddress,
uint32_t vni, sai_object_id_t nh_id)
{
auto key = nh_key_t(ipAddr, macAddress, vni);
SWSS_LOG_INFO("Update NH tunnel for ip %s, mac %s, vni %d",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni);
auto it = nh_tunnels_.find(key);
if (it == nh_tunnels_.end())
{
nh_tunnels_[key] = {nh_id, 1};
return;
}
else
{
SWSS_LOG_INFO("Dup Update NH tunnel for ip %s, mac %s, vni %d",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni);
}
}
sai_object_id_t VxlanTunnel::getNextHop(IpAddress& ipAddr,
MacAddress macAddress, uint32_t vni) const
{
auto key = nh_key_t(ipAddr, macAddress, vni);
auto it = nh_tunnels_.find(key);
if (it == nh_tunnels_.end())
{
return SAI_NULL_OBJECT_ID;
}
return nh_tunnels_.at(key).nh_id;
}
void VxlanTunnel::incNextHopRefCount(IpAddress& ipAddr, MacAddress macAddress, uint32_t vni)
{
auto key = nh_key_t(ipAddr, macAddress, vni);
nh_tunnels_[key].ref_count ++;
SWSS_LOG_INFO("refcnt increment NH tunnel for ip %s, mac %s, vni %d, ref_count %d",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni,
nh_tunnels_[key].ref_count);
}
void VxlanTunnel::decNextHopRefCount(IpAddress& ipAddr, MacAddress macAddress, uint32_t vni)
{
auto key = nh_key_t(ipAddr, macAddress, vni);
nh_tunnels_[key].ref_count --;
SWSS_LOG_INFO("refcnt decrement NH tunnel for ip %s, mac %s, vni %d, ref_count %d",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni,
nh_tunnels_[key].ref_count);
}
bool VxlanTunnel::removeNextHop(IpAddress& ipAddr, MacAddress macAddress, uint32_t vni)
{
auto key = nh_key_t(ipAddr, macAddress, vni);
auto it = nh_tunnels_.find(key);
if (it == nh_tunnels_.end())
{
SWSS_LOG_INFO("remove NH tunnel for ip %s, mac %s, vni %d doesn't exist",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni);
return false;
}
SWSS_LOG_INFO("remove NH tunnel for ip %s, mac %s, vni %d, ref_count %d",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni,
nh_tunnels_[key].ref_count);
//Decrement ref count if already exists
nh_tunnels_[key].ref_count --;
if (!nh_tunnels_[key].ref_count)
{
if (sai_next_hop_api->remove_next_hop(nh_tunnels_[key].nh_id) != SAI_STATUS_SUCCESS)
{
SWSS_LOG_INFO("delete NH tunnel for ip '%s', mac '%s' vni %d failed",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni);
string err_msg = "NH tunnel delete failed for " + ipAddr.to_string();
throw std::runtime_error(err_msg);
}
nh_tunnels_.erase(key);
}
SWSS_LOG_INFO("NH tunnel for ip '%s', mac '%s' vni %d updated/deleted",
ipAddr.to_string().c_str(), macAddress.to_string().c_str(), vni);
return true;
}
bool VxlanTunnel::deleteMapperHw(uint8_t mapper_list, tunnel_map_use_t map_src)
{
try
{
if (map_src == TUNNEL_MAP_USE_DEDICATED_ENCAP_DECAP)
{
if (IS_TUNNELMAP_SET_VLAN(mapper_list))
{
remove_tunnel_map(ids_.tunnel_decap_id[TUNNEL_MAP_T_VLAN]);
remove_tunnel_map(ids_.tunnel_encap_id[TUNNEL_MAP_T_VLAN]);
}
if (IS_TUNNELMAP_SET_VRF(mapper_list))
{
remove_tunnel_map(ids_.tunnel_decap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER]);
remove_tunnel_map(ids_.tunnel_encap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER]);
}
if (IS_TUNNELMAP_SET_BRIDGE(mapper_list))
{
remove_tunnel_map(ids_.tunnel_decap_id[TUNNEL_MAP_T_BRIDGE]);
remove_tunnel_map(ids_.tunnel_encap_id[TUNNEL_MAP_T_BRIDGE]);
}
}
else if (map_src == TUNNEL_MAP_USE_COMMON_DECAP_DEDICATED_ENCAP)
{
if (IS_TUNNELMAP_SET_VLAN(mapper_list))
{
remove_tunnel_map(ids_.tunnel_encap_id[TUNNEL_MAP_T_VLAN]);
}
if (IS_TUNNELMAP_SET_VRF(mapper_list))
{
remove_tunnel_map(ids_.tunnel_encap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER]);
}
if (IS_TUNNELMAP_SET_BRIDGE(mapper_list))
{
remove_tunnel_map(ids_.tunnel_encap_id[TUNNEL_MAP_T_BRIDGE]);
}
}
else if (map_src == TUNNEL_MAP_USE_DECAP_ONLY)
{
if (IS_TUNNELMAP_SET_VLAN(mapper_list))
{
remove_tunnel_map(ids_.tunnel_decap_id[TUNNEL_MAP_T_VLAN]);
}
if (IS_TUNNELMAP_SET_VRF(mapper_list))
{
remove_tunnel_map(ids_.tunnel_decap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER]);
}
if (IS_TUNNELMAP_SET_BRIDGE(mapper_list))
{
remove_tunnel_map(ids_.tunnel_decap_id[TUNNEL_MAP_T_BRIDGE]);
}
}
}
catch (const std::runtime_error& error)
{
SWSS_LOG_ERROR("Error deleting mapper %s: %s", tunnel_name_.c_str(), error.what());
return false;
}
return true;
}
bool VxlanTunnel::createMapperHw(uint8_t mapper_list, tunnel_map_use_t map_src)
{
try
{
for (int i=TUNNEL_MAP_T_VLAN; i<TUNNEL_MAP_T_MAX_MAPPER; i++)
{
ids_.tunnel_decap_id[i] = SAI_NULL_OBJECT_ID;
ids_.tunnel_encap_id[i] = SAI_NULL_OBJECT_ID;
}
if (TUNNEL_MAP_USE_DEDICATED_ENCAP_DECAP == map_src)
{
if (IS_TUNNELMAP_SET_VLAN(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VLAN] = create_tunnel_map(MAP_T::VNI_TO_VLAN_ID);
ids_.tunnel_encap_id[TUNNEL_MAP_T_VLAN] = create_tunnel_map(MAP_T::VLAN_ID_TO_VNI);
TUNNELMAP_SET_VLAN(encap_dedicated_mappers_);
TUNNELMAP_SET_VLAN(decap_dedicated_mappers_);
}
if (IS_TUNNELMAP_SET_VRF(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER] = create_tunnel_map(MAP_T::VNI_TO_VRID);
ids_.tunnel_encap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER] = create_tunnel_map(MAP_T::VRID_TO_VNI);
TUNNELMAP_SET_VRF(encap_dedicated_mappers_);
TUNNELMAP_SET_VRF(decap_dedicated_mappers_);
}
if (IS_TUNNELMAP_SET_BRIDGE(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_BRIDGE] = create_tunnel_map(MAP_T::VNI_TO_BRIDGE);
ids_.tunnel_encap_id[TUNNEL_MAP_T_BRIDGE] = create_tunnel_map(MAP_T::BRIDGE_TO_VNI);
TUNNELMAP_SET_BRIDGE(encap_dedicated_mappers_);
TUNNELMAP_SET_BRIDGE(decap_dedicated_mappers_);
}
}
else if (map_src == TUNNEL_MAP_USE_COMMON_DECAP_DEDICATED_ENCAP)
{
if (IS_TUNNELMAP_SET_VLAN(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VLAN] = vtep_ptr->getDecapMapId(TUNNEL_MAP_T_VLAN);
ids_.tunnel_encap_id[TUNNEL_MAP_T_VLAN] = create_tunnel_map(MAP_T::VLAN_ID_TO_VNI);
TUNNELMAP_SET_VLAN(encap_dedicated_mappers_);
}
if (IS_TUNNELMAP_SET_VRF(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER] = vtep_ptr->getDecapMapId(TUNNEL_MAP_T_VIRTUAL_ROUTER);
ids_.tunnel_encap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER] = create_tunnel_map(MAP_T::VRID_TO_VNI);
TUNNELMAP_SET_VRF(encap_dedicated_mappers_);
}
if (IS_TUNNELMAP_SET_BRIDGE(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_BRIDGE] = vtep_ptr->getDecapMapId(TUNNEL_MAP_T_BRIDGE);
ids_.tunnel_encap_id[TUNNEL_MAP_T_BRIDGE] = create_tunnel_map(MAP_T::BRIDGE_TO_VNI);
TUNNELMAP_SET_BRIDGE(encap_dedicated_mappers_);
}
}
else if (TUNNEL_MAP_USE_COMMON_ENCAP_DECAP == map_src)
{
if (IS_TUNNELMAP_SET_VLAN(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VLAN] = vtep_ptr->getDecapMapId(TUNNEL_MAP_T_VLAN);
ids_.tunnel_encap_id[TUNNEL_MAP_T_VLAN] = vtep_ptr->getEncapMapId(TUNNEL_MAP_T_VLAN);
}
if (IS_TUNNELMAP_SET_VRF(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER] = vtep_ptr->getDecapMapId(TUNNEL_MAP_T_VIRTUAL_ROUTER);
ids_.tunnel_encap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER] = vtep_ptr->getEncapMapId(TUNNEL_MAP_T_VIRTUAL_ROUTER);
}
if (IS_TUNNELMAP_SET_BRIDGE(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_BRIDGE] = vtep_ptr->getDecapMapId(TUNNEL_MAP_T_BRIDGE);
ids_.tunnel_encap_id[TUNNEL_MAP_T_BRIDGE] = vtep_ptr->getEncapMapId(TUNNEL_MAP_T_BRIDGE);
}
}
else if (TUNNEL_MAP_USE_DECAP_ONLY == map_src)
{
if (IS_TUNNELMAP_SET_VLAN(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VLAN] = create_tunnel_map(MAP_T::VNI_TO_VLAN_ID);
TUNNELMAP_SET_VLAN(decap_dedicated_mappers_);
}
if (IS_TUNNELMAP_SET_VRF(mapper_list))
{
ids_.tunnel_decap_id[TUNNEL_MAP_T_VIRTUAL_ROUTER] = create_tunnel_map(MAP_T::VNI_TO_VRID);
TUNNELMAP_SET_VRF(decap_dedicated_mappers_);
}
if (IS_TUNNELMAP_SET_BRIDGE(mapper_list))
{
ids_.tunnel_encap_id[TUNNEL_MAP_T_BRIDGE] = create_tunnel_map(MAP_T::BRIDGE_TO_VNI);
TUNNELMAP_SET_BRIDGE(decap_dedicated_mappers_);
}
}
}
catch (const std::runtime_error& error)
{
SWSS_LOG_ERROR("Error creating tunnel %s: %s", tunnel_name_.c_str(), error.what());
return false;
}
return true;
}
bool VxlanTunnel::deleteTunnelHw(uint8_t mapper_list, tunnel_map_use_t map_src,
bool with_term)
{
try
{
VxlanTunnelOrch* tunnel_orch = gDirectory.get<VxlanTunnelOrch*>();
if (with_term)
{
remove_tunnel_termination(ids_.tunnel_term_id);
}
tunnel_orch->removeTunnelFromFlexCounter(ids_.tunnel_id, tunnel_name_);
remove_tunnel(ids_.tunnel_id);
deleteMapperHw(mapper_list, map_src);
}
catch (const std::runtime_error& error)
{
SWSS_LOG_ERROR("Error deleting tunnel %s: %s", tunnel_name_.c_str(), error.what());
return false;
}
active_ = false;
return true;
}
//Creation of SAI Tunnel Object with multiple mapper types
bool VxlanTunnel::createTunnelHw(uint8_t mapper_list, tunnel_map_use_t map_src,
bool with_term, sai_uint8_t encap_ttl)
{
bool p2p = false;
try
{
VxlanTunnelOrch* tunnel_orch = gDirectory.get<VxlanTunnelOrch*>();
sai_ip_address_t ips, ipd, *ip=nullptr;
swss::copy(ips, src_ip_);
createMapperHw(mapper_list, map_src);
ip = nullptr;
if (!dst_ip_.isZero())
{
swss::copy(ipd, dst_ip_);
ip = &ipd;
p2p = (src_creation_ == TNL_CREATION_SRC_EVPN)? true:false;
SWSS_LOG_WARN("creation src = %d",src_creation_);
}
ids_.tunnel_id = create_tunnel(&ids_, &ips, ip, gUnderlayIfId, p2p, encap_ttl);
if (ids_.tunnel_id != SAI_NULL_OBJECT_ID)
{
tunnel_orch->addTunnelToFlexCounter(ids_.tunnel_id, tunnel_name_);
}
if (with_term)
{
ids_.tunnel_term_id = create_tunnel_termination(ids_.tunnel_id, ips,
ip, gVirtualRouterId);
}
active_ = true;
}
catch (const std::runtime_error& error)
{
SWSS_LOG_ERROR("Error creating tunnel %s: %s", tunnel_name_.c_str(), error.what());
return false;
}
SWSS_LOG_INFO("Vxlan tunnel '%s' was created", tunnel_name_.c_str());
return true;
}
void VxlanTunnel::deletePendingSIPTunnel()
{
VxlanTunnelOrch* tunnel_orch = gDirectory.get<VxlanTunnelOrch*>();
bool dip_tunnels_used = tunnel_orch->isDipTunnelsSupported();
if ((!dip_tunnels_used || getDipTunnelCnt() == 0) && del_tnl_hw_pending)
{
uint8_t mapper_list=0;
TUNNELMAP_SET_VLAN(mapper_list);
TUNNELMAP_SET_VRF(mapper_list);
deleteTunnelHw(mapper_list, TUNNEL_MAP_USE_DEDICATED_ENCAP_DECAP);
del_tnl_hw_pending = false;
SWSS_LOG_INFO("Removing SIP Tunnel HW which is pending");
}
return;
}
int VxlanTunnel::getDipTunnelCnt()
{
int ret;
ret = (int)tnl_users_.size();
return ret;
}
void VxlanTunnel::increment_spurious_imr_add(const std::string remote_vtep)
{
tunnel_refcnt_t tnl_refcnts;
auto it = tnl_users_.find(remote_vtep);
if (it == tnl_users_.end())
{
return ;
}
else
{
tnl_refcnts = it->second;
tnl_refcnts.spurious_add_imr_refcnt++;
tnl_users_[remote_vtep] = tnl_refcnts;
}
}
void VxlanTunnel::increment_spurious_imr_del(const std::string remote_vtep)
{
tunnel_refcnt_t tnl_refcnts;
auto it = tnl_users_.find(remote_vtep);
if (it == tnl_users_.end())
{
return ;
}
else
{
tnl_refcnts = it->second;
tnl_refcnts.spurious_del_imr_refcnt++;
tnl_users_[remote_vtep] = tnl_refcnts;
}
}
int VxlanTunnel::getRemoteEndPointRefCnt(const std::string remote_vtep)
{