@@ -114,6 +114,16 @@ FlexCounter::MACsecSAAttrIds::MACsecSAAttrIds(
114
114
// empty intentionally
115
115
}
116
116
117
+ FlexCounter::TunnelCounterIds::TunnelCounterIds (
118
+ _In_ sai_object_id_t tunnelRid,
119
+ _In_ const std::vector<sai_tunnel_stat_t > &tunnelIds):
120
+ m_tunnelId(tunnelRid),
121
+ m_tunnelCounterIds(tunnelIds)
122
+ {
123
+ SWSS_LOG_ENTER ();
124
+ // empty intentionally
125
+ }
126
+
117
127
void FlexCounter::setPollInterval (
118
128
_In_ uint32_t pollInterval)
119
129
{
@@ -567,6 +577,47 @@ void FlexCounter::setBufferPoolCounterList(
567
577
addCollectCountersHandler (BUFFER_POOL_COUNTER_ID_LIST, &FlexCounter::collectBufferPoolCounters);
568
578
}
569
579
580
+ void FlexCounter::setTunnelCounterList (
581
+ _In_ sai_object_id_t tunnelVid,
582
+ _In_ sai_object_id_t tunnelRid,
583
+ _In_ const std::vector<sai_tunnel_stat_t > &counterIds)
584
+ {
585
+ SWSS_LOG_ENTER ();
586
+
587
+ updateSupportedTunnelCounters (tunnelRid, counterIds);
588
+
589
+ // Remove unsupported counters
590
+ std::vector<sai_tunnel_stat_t > supportedIds;
591
+
592
+ for (auto &counter : counterIds)
593
+ {
594
+ if (isTunnelCounterSupported (counter))
595
+ {
596
+ supportedIds.push_back (counter);
597
+ }
598
+ }
599
+
600
+ if (supportedIds.empty ())
601
+ {
602
+ SWSS_LOG_NOTICE (" Tunnel %s does not have supported counters" , sai_serialize_object_id (tunnelRid).c_str ());
603
+ return ;
604
+ }
605
+
606
+ auto it = m_tunnelCounterIdsMap.find (tunnelVid);
607
+
608
+ if (it != m_tunnelCounterIdsMap.end ())
609
+ {
610
+ it->second ->m_tunnelCounterIds = supportedIds;
611
+ return ;
612
+ }
613
+
614
+ auto tunnelCounterIds = std::make_shared<TunnelCounterIds>(tunnelRid, supportedIds);
615
+
616
+ m_tunnelCounterIdsMap.emplace (tunnelVid, tunnelCounterIds);
617
+
618
+ addCollectCountersHandler (TUNNEL_COUNTER_ID_LIST, &FlexCounter::collectTunnelCounters);
619
+ }
620
+
570
621
void FlexCounter::removePort (
571
622
_In_ sai_object_id_t portVid)
572
623
{
@@ -787,6 +838,27 @@ void FlexCounter::removeSwitchDebugCounters(
787
838
}
788
839
}
789
840
841
+ void FlexCounter::removeTunnel (
842
+ _In_ sai_object_id_t tunnelVid)
843
+ {
844
+ SWSS_LOG_ENTER ();
845
+
846
+ auto it = m_tunnelCounterIdsMap.find (tunnelVid);
847
+
848
+ if (it == m_tunnelCounterIdsMap.end ())
849
+ {
850
+ SWSS_LOG_NOTICE (" Trying to remove nonexisting tunnel counter from Id 0x%" PRIx64, tunnelVid);
851
+ return ;
852
+ }
853
+
854
+ m_tunnelCounterIdsMap.erase (it);
855
+
856
+ if (m_tunnelCounterIdsMap.empty ())
857
+ {
858
+ removeCollectCountersHandler (TUNNEL_COUNTER_ID_LIST);
859
+ }
860
+ }
861
+
790
862
void FlexCounter::checkPluginRegistered (
791
863
_In_ const std::string& sha) const
792
864
{
@@ -797,7 +869,8 @@ void FlexCounter::checkPluginRegistered(
797
869
m_rifPlugins.find (sha) != m_rifPlugins.end () ||
798
870
m_queuePlugins.find (sha) != m_queuePlugins.end () ||
799
871
m_priorityGroupPlugins.find (sha) != m_priorityGroupPlugins.end () ||
800
- m_bufferPoolPlugins.find (sha) != m_bufferPoolPlugins.end ()
872
+ m_bufferPoolPlugins.find (sha) != m_bufferPoolPlugins.end () ||
873
+ m_tunnelPlugins.find (sha) != m_tunnelPlugins.end ()
801
874
)
802
875
{
803
876
SWSS_LOG_ERROR (" Plugin %s already registered" , sha.c_str ());
@@ -864,6 +937,18 @@ void FlexCounter::addBufferPoolCounterPlugin(
864
937
SWSS_LOG_NOTICE (" Buffer pool counters plugin %s registered" , sha.c_str ());
865
938
}
866
939
940
+ void FlexCounter::addTunnelCounterPlugin (
941
+ _In_ const std::string& sha)
942
+ {
943
+ SWSS_LOG_ENTER ();
944
+
945
+ checkPluginRegistered (sha);
946
+
947
+ m_tunnelPlugins.insert (sha);
948
+
949
+ SWSS_LOG_NOTICE (" Tunnel counters plugin %s registered" , sha.c_str ());
950
+ }
951
+
867
952
void FlexCounter::removeCounterPlugins ()
868
953
{
869
954
MUTEX;
@@ -875,6 +960,7 @@ void FlexCounter::removeCounterPlugins()
875
960
m_rifPlugins.clear ();
876
961
m_priorityGroupPlugins.clear ();
877
962
m_bufferPoolPlugins.clear ();
963
+ m_tunnelPlugins.clear ();
878
964
879
965
m_isDiscarded = true ;
880
966
}
@@ -942,6 +1028,13 @@ void FlexCounter::addCounterPlugin(
942
1028
addBufferPoolCounterPlugin (sha);
943
1029
}
944
1030
}
1031
+ else if (field == TUNNEL_PLUGIN_FIELD)
1032
+ {
1033
+ for (auto & sha: shaStrings)
1034
+ {
1035
+ addTunnelCounterPlugin (sha);
1036
+ }
1037
+ }
945
1038
else
946
1039
{
947
1040
SWSS_LOG_ERROR (" Field is not supported %s" , field.c_str ());
@@ -981,7 +1074,8 @@ bool FlexCounter::allIdsEmpty() const
981
1074
m_rifCounterIdsMap.empty () &&
982
1075
m_bufferPoolCounterIdsMap.empty () &&
983
1076
m_switchDebugCounterIdsMap.empty () &&
984
- m_macsecSAAttrIdsMap.empty ();
1077
+ m_macsecSAAttrIdsMap.empty () &&
1078
+ m_tunnelCounterIdsMap.empty ();
985
1079
}
986
1080
987
1081
bool FlexCounter::allPluginsEmpty () const
@@ -992,7 +1086,8 @@ bool FlexCounter::allPluginsEmpty() const
992
1086
m_queuePlugins.empty () &&
993
1087
m_portPlugins.empty () &&
994
1088
m_rifPlugins.empty () &&
995
- m_bufferPoolPlugins.empty ();
1089
+ m_bufferPoolPlugins.empty () &&
1090
+ m_tunnelPlugins.empty ();
996
1091
}
997
1092
998
1093
bool FlexCounter::isPortCounterSupported (sai_port_stat_t counter) const
@@ -1034,6 +1129,14 @@ bool FlexCounter::isBufferPoolCounterSupported(
1034
1129
return m_supportedBufferPoolCounters.count (counter) != 0 ;
1035
1130
}
1036
1131
1132
+ bool FlexCounter::isTunnelCounterSupported (
1133
+ _In_ sai_tunnel_stat_t counter) const
1134
+ {
1135
+ SWSS_LOG_ENTER ();
1136
+
1137
+ return m_supportedTunnelCounters.count (counter) != 0 ;
1138
+ }
1139
+
1037
1140
bool FlexCounter::isStatsModeSupported (
1038
1141
_In_ uint32_t statsMode,
1039
1142
_In_ sai_stats_mode_t statCapability)
@@ -1597,6 +1700,51 @@ void FlexCounter::collectBufferPoolCounters(
1597
1700
}
1598
1701
}
1599
1702
1703
+ void FlexCounter::collectTunnelCounters (
1704
+ _In_ swss::Table &countersTable)
1705
+ {
1706
+ SWSS_LOG_ENTER ();
1707
+
1708
+ // Collect stats for every registered tunnel
1709
+ for (const auto &kv: m_tunnelCounterIdsMap)
1710
+ {
1711
+ const auto &tunnelVid = kv.first ;
1712
+ const auto &tunnelId = kv.second ->m_tunnelId ;
1713
+ const auto &tunnelCounterIds = kv.second ->m_tunnelCounterIds ;
1714
+
1715
+ std::vector<uint64_t > tunnelStats (tunnelCounterIds.size ());
1716
+
1717
+ // Get tunnel stats
1718
+ sai_status_t status = m_vendorSai->getStats (
1719
+ SAI_OBJECT_TYPE_TUNNEL,
1720
+ tunnelId,
1721
+ static_cast <uint32_t >(tunnelCounterIds.size ()),
1722
+ (const sai_stat_id_t *)tunnelCounterIds.data (),
1723
+ tunnelStats.data ());
1724
+
1725
+ if (status != SAI_STATUS_SUCCESS)
1726
+ {
1727
+ SWSS_LOG_ERROR (" Failed to get stats of tunnel 0x%" PRIx64 " : %d" , tunnelId, status);
1728
+ continue ;
1729
+ }
1730
+
1731
+ // Push all counter values to a single vector
1732
+ std::vector<swss::FieldValueTuple> values;
1733
+
1734
+ for (size_t i = 0 ; i != tunnelCounterIds.size (); i++)
1735
+ {
1736
+ const std::string &counterName = sai_serialize_tunnel_stat (tunnelCounterIds[i]);
1737
+
1738
+ values.emplace_back (counterName, std::to_string (tunnelStats[i]));
1739
+ }
1740
+
1741
+ // Write counters to DB
1742
+ std::string tunnelVidStr = sai_serialize_object_id (tunnelVid);
1743
+
1744
+ countersTable.set (tunnelVidStr, values, " " );
1745
+ }
1746
+ }
1747
+
1600
1748
void FlexCounter::runPlugins (
1601
1749
_In_ swss::DBConnector& counters_db)
1602
1750
{
@@ -1675,6 +1823,17 @@ void FlexCounter::runPlugins(
1675
1823
{
1676
1824
runRedisScript (counters_db, sha, bufferPoolVids, argv);
1677
1825
}
1826
+
1827
+ std::vector<std::string> tunnelList;
1828
+ tunnelList.reserve (m_tunnelCounterIdsMap.size ());
1829
+ for (const auto & kv : m_tunnelCounterIdsMap)
1830
+ {
1831
+ tunnelList.push_back (sai_serialize_object_id (kv.first ));
1832
+ }
1833
+ for (const auto & sha : m_tunnelPlugins)
1834
+ {
1835
+ runRedisScript (counters_db, sha, tunnelList, argv);
1836
+ }
1678
1837
}
1679
1838
1680
1839
void FlexCounter::flexCounterThreadRunFunction ()
@@ -2318,6 +2477,41 @@ void FlexCounter::getSupportedBufferPoolCounters(
2318
2477
}
2319
2478
}
2320
2479
2480
+ void FlexCounter::updateSupportedTunnelCounters (
2481
+ _In_ sai_object_id_t tunnelRid,
2482
+ _In_ const std::vector<sai_tunnel_stat_t > &counterIds)
2483
+ {
2484
+ SWSS_LOG_ENTER ();
2485
+
2486
+ if (m_supportedTunnelCounters.size ())
2487
+ {
2488
+ return ;
2489
+ }
2490
+
2491
+ uint64_t value;
2492
+ for (auto &counter: counterIds)
2493
+ {
2494
+ sai_status_t status = m_vendorSai->getStats (
2495
+ SAI_OBJECT_TYPE_TUNNEL,
2496
+ tunnelRid,
2497
+ 1 ,
2498
+ (const sai_stat_id_t *)&counter,
2499
+ &value);
2500
+
2501
+ if (status != SAI_STATUS_SUCCESS)
2502
+ {
2503
+ SWSS_LOG_INFO (" Counter %s is not supported on tunnel RID %s: %s" ,
2504
+ sai_serialize_tunnel_stat (counter).c_str (),
2505
+ sai_serialize_object_id (tunnelRid).c_str (),
2506
+ sai_serialize_status (status).c_str ());
2507
+
2508
+ continue ;
2509
+ }
2510
+
2511
+ m_supportedTunnelCounters.insert (counter);
2512
+ }
2513
+ }
2514
+
2321
2515
void FlexCounter::updateSupportedBufferPoolCounters (
2322
2516
_In_ sai_object_id_t bufferPoolId,
2323
2517
_In_ const std::vector<sai_buffer_pool_stat_t > &counterIds,
@@ -2413,6 +2607,10 @@ void FlexCounter::removeCounter(
2413
2607
{
2414
2608
removeMACsecSA (vid);
2415
2609
}
2610
+ else if (objectType == SAI_OBJECT_TYPE_TUNNEL)
2611
+ {
2612
+ removeTunnel (vid);
2613
+ }
2416
2614
else
2417
2615
{
2418
2616
SWSS_LOG_ERROR (" Object type for removal not supported, %s" ,
@@ -2568,6 +2766,19 @@ void FlexCounter::addCounter(
2568
2766
{
2569
2767
statsMode = value;
2570
2768
}
2769
+ else if (objectType == SAI_OBJECT_TYPE_TUNNEL && field == TUNNEL_COUNTER_ID_LIST)
2770
+ {
2771
+ std::vector<sai_tunnel_stat_t > tunnelCounterIds;
2772
+
2773
+ for (const auto &str : idStrings)
2774
+ {
2775
+ sai_tunnel_stat_t stat;
2776
+ sai_deserialize_tunnel_stat (str.c_str (), &stat);
2777
+ tunnelCounterIds.push_back (stat);
2778
+ }
2779
+
2780
+ setTunnelCounterList (vid, rid, tunnelCounterIds);
2781
+ }
2571
2782
else
2572
2783
{
2573
2784
SWSS_LOG_ERROR (" Object type and field combination is not supported, object type %s, field %s" ,
0 commit comments