-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathflex_counter_manager.cpp
More file actions
286 lines (239 loc) · 9.25 KB
/
flex_counter_manager.cpp
File metadata and controls
286 lines (239 loc) · 9.25 KB
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
#include "flex_counter_manager.h"
#include <vector>
#include "schema.h"
#include "rediscommand.h"
#include "logger.h"
#include <macsecorch.h>
using std::shared_ptr;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::vector;
using swss::DBConnector;
using swss::FieldValueTuple;
using swss::ProducerTable;
extern sai_switch_api_t *sai_switch_api;
const string FLEX_COUNTER_ENABLE("enable");
const string FLEX_COUNTER_DISABLE("disable");
const unordered_map<StatsMode, string> FlexCounterManager::stats_mode_lookup =
{
{ StatsMode::READ, STATS_MODE_READ },
{ StatsMode::READ_AND_CLEAR, STATS_MODE_READ_AND_CLEAR },
};
const unordered_map<bool, string> FlexCounterManager::status_lookup =
{
{ false, FLEX_COUNTER_DISABLE },
{ true, FLEX_COUNTER_ENABLE }
};
const unordered_map<CounterType, string> FlexCounterManager::counter_id_field_lookup =
{
{ CounterType::PORT_DEBUG, PORT_DEBUG_COUNTER_ID_LIST },
{ CounterType::PORT_PHY_ATTR, PORT_PHY_ATTR_ID_LIST },
{ CounterType::PORT_PHY_SERDES_ATTR,PORT_PHY_SERDES_ATTR_ID_LIST },
{ CounterType::SWITCH_DEBUG, SWITCH_DEBUG_COUNTER_ID_LIST },
{ CounterType::PORT, PORT_COUNTER_ID_LIST },
{ CounterType::QUEUE, QUEUE_COUNTER_ID_LIST },
{ CounterType::QUEUE_ATTR, QUEUE_ATTR_ID_LIST },
{ CounterType::PRIORITY_GROUP, PG_COUNTER_ID_LIST },
{ CounterType::MACSEC_SA_ATTR, MACSEC_SA_ATTR_ID_LIST },
{ CounterType::MACSEC_SA, MACSEC_SA_COUNTER_ID_LIST },
{ CounterType::MACSEC_FLOW, MACSEC_FLOW_COUNTER_ID_LIST },
{ CounterType::ACL_COUNTER, ACL_COUNTER_ATTR_ID_LIST },
{ CounterType::TUNNEL, TUNNEL_COUNTER_ID_LIST },
{ CounterType::HOSTIF_TRAP, FLOW_COUNTER_ID_LIST },
{ CounterType::ROUTE, FLOW_COUNTER_ID_LIST },
{ CounterType::ENI, ENI_COUNTER_ID_LIST },
{ CounterType::DASH_METER, DASH_METER_COUNTER_ID_LIST },
{ CounterType::SRV6, SRV6_COUNTER_ID_LIST },
{ CounterType::SWITCH, SWITCH_COUNTER_ID_LIST },
{ CounterType::HA_SET, HA_SET_COUNTER_ID_LIST },
};
FlexManagerDirectory g_FlexManagerDirectory;
FlexCounterManager *FlexManagerDirectory::createFlexCounterManager(const string& group_name,
const StatsMode stats_mode,
const uint polling_interval,
const bool enabled,
FieldValueTuple fv_plugin)
{
if (m_managers.find(group_name) != m_managers.end())
{
if (stats_mode != m_managers[group_name]->getStatsMode())
{
SWSS_LOG_ERROR("Stats mode mismatch with already created flex counter manager %s",
group_name.c_str());
return NULL;
}
if (polling_interval != m_managers[group_name]->getPollingInterval())
{
SWSS_LOG_ERROR("Polling interval mismatch with already created flex counter manager %s",
group_name.c_str());
return NULL;
}
if (enabled != m_managers[group_name]->getEnabled())
{
SWSS_LOG_ERROR("Enabled field mismatch with already created flex counter manager %s",
group_name.c_str());
return NULL;
}
return m_managers[group_name];
}
FlexCounterManager *fc_manager = new FlexCounterManager(group_name, stats_mode, polling_interval,
enabled, fv_plugin);
m_managers[group_name] = fc_manager;
return fc_manager;
}
FlexCounterManager::FlexCounterManager(
const string& group_name,
const StatsMode stats_mode,
const uint polling_interval,
const bool enabled,
FieldValueTuple fv_plugin) :
FlexCounterManager(false, group_name, stats_mode,
polling_interval, enabled, fv_plugin)
{
}
FlexCounterManager::FlexCounterManager(
const bool is_gearbox,
const string& group_name,
const StatsMode stats_mode,
const uint polling_interval,
const bool enabled,
FieldValueTuple fv_plugin) :
group_name(group_name),
stats_mode(stats_mode),
polling_interval(polling_interval),
enabled(enabled),
fv_plugin(fv_plugin),
is_gearbox(is_gearbox)
{
SWSS_LOG_ENTER();
applyGroupConfiguration();
SWSS_LOG_DEBUG("Initialized flex counter group '%s'.", group_name.c_str());
}
FlexCounterManager::~FlexCounterManager()
{
SWSS_LOG_ENTER();
for (const auto& counter: installed_counters)
{
stopFlexCounterPolling(counter.second, getFlexCounterTableKey(group_name, counter.first));
}
delFlexCounterGroup(group_name, is_gearbox);
SWSS_LOG_DEBUG("Deleted flex counter group '%s'.", group_name.c_str());
}
void FlexCounterManager::applyGroupConfiguration()
{
SWSS_LOG_ENTER();
setFlexCounterGroupParameter(group_name,
std::to_string(polling_interval),
stats_mode_lookup.at(stats_mode),
fvField(fv_plugin),
fvValue(fv_plugin),
status_lookup.at(enabled),
is_gearbox);
}
void FlexCounterManager::updateGroupPollingInterval(
const uint polling_interval)
{
SWSS_LOG_ENTER();
setFlexCounterGroupPollInterval(group_name, std::to_string(polling_interval), is_gearbox);
SWSS_LOG_DEBUG("Set polling interval for flex counter group '%s' to %d ms.",
group_name.c_str(), polling_interval);
}
// enableFlexCounterGroup will do nothing if the flex counter group is already
// enabled.
void FlexCounterManager::enableFlexCounterGroup()
{
SWSS_LOG_ENTER();
if (enabled)
{
return;
}
setFlexCounterGroupOperation(group_name, FLEX_COUNTER_ENABLE, is_gearbox);
enabled = true;
SWSS_LOG_DEBUG("Enabling flex counters for group '%s'.",
group_name.c_str());
}
// disableFlexCounterGroup will do nothing if the flex counter group has been
// disabled.
void FlexCounterManager::disableFlexCounterGroup()
{
SWSS_LOG_ENTER();
if (!enabled)
{
return;
}
setFlexCounterGroupOperation(group_name, FLEX_COUNTER_DISABLE, is_gearbox);
enabled = false;
SWSS_LOG_DEBUG("Disabling flex counters for group '%s'.",
group_name.c_str());
}
// setCounterIdList configures a flex counter to poll the set of provided stats
// that are associated with the given object.
void FlexCounterManager::setCounterIdList(
const sai_object_id_t object_id,
const CounterType counter_type,
const unordered_set<string>& counter_stats,
const sai_object_id_t switch_id)
{
SWSS_LOG_ENTER();
auto counter_type_it = counter_id_field_lookup.find(counter_type);
if (counter_type_it == counter_id_field_lookup.end())
{
SWSS_LOG_ERROR("Could not update flex counter id list for group '%s': counter type not found.",
group_name.c_str());
return;
}
auto key = getFlexCounterTableKey(group_name, object_id);
auto counter_ids = serializeCounterStats(counter_stats);
auto effective_switch_id = switch_id == SAI_NULL_OBJECT_ID ? gSwitchId : switch_id;
startFlexCounterPolling(effective_switch_id, key, counter_ids, counter_type_it->second);
installed_counters[object_id] = effective_switch_id;
SWSS_LOG_DEBUG("Updated flex counter id list for object '%" PRIu64 "' in group '%s'.",
object_id,
group_name.c_str());
}
// clearCounterIdList clears all stats that are currently being polled from
// the given object.
void FlexCounterManager::clearCounterIdList(const sai_object_id_t object_id)
{
SWSS_LOG_ENTER();
auto counter_it = installed_counters.find(object_id);
if (counter_it == installed_counters.end())
{
SWSS_LOG_INFO("No counters found on object '%" PRIu64 "' in group '%s'.",
object_id,
group_name.c_str());
return;
}
auto key = getFlexCounterTableKey(group_name, object_id);
stopFlexCounterPolling(installed_counters[object_id], key);
installed_counters.erase(counter_it);
SWSS_LOG_DEBUG("Cleared flex counter id list for object '%" PRIu64 "' in group '%s'.",
object_id,
group_name.c_str());
}
string FlexCounterManager::getFlexCounterTableKey(
const string& group_name,
const sai_object_id_t object_id) const
{
SWSS_LOG_ENTER();
return group_name + ":" + sai_serialize_object_id(object_id);
}
// serializeCounterStats turns a set of stats into a format suitable for FLEX_COUNTER_DB.
string FlexCounterManager::serializeCounterStats(
const unordered_set<string>& counter_stats)
{
SWSS_LOG_ENTER();
string stats_string;
for (const auto& stat : counter_stats)
{
stats_string.append(stat);
stats_string.append(",");
}
if (!stats_string.empty())
{
// Fence post: remove the trailing comma
stats_string.pop_back();
}
return stats_string;
}