Skip to content

Commit b2c03d1

Browse files
authored
Egress Sflow Support (sonic-net#2731)
* [sflow] Added Egress Sflow support.
1 parent 90b34d4 commit b2c03d1

File tree

7 files changed

+405
-52
lines changed

7 files changed

+405
-52
lines changed

cfgmgr/sflowmgr.cpp

+118-27
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ SflowMgr::SflowMgr(DBConnector *cfgDb, DBConnector *appDb, const vector<string>
3131
{
3232
m_intfAllConf = true;
3333
m_gEnable = false;
34+
m_gDirection = "rx";
35+
m_intfAllDir = "rx";
3436
}
3537

3638
void SflowMgr::sflowHandleService(bool enable)
@@ -85,9 +87,11 @@ void SflowMgr::sflowUpdatePortInfo(Consumer &consumer)
8587
new_port = true;
8688
port_info.local_rate_cfg = false;
8789
port_info.local_admin_cfg = false;
90+
port_info.local_dir_cfg = false;
8891
port_info.speed = SFLOW_ERROR_SPEED_STR;
8992
port_info.rate = "";
9093
port_info.admin = "";
94+
port_info.dir = "";
9195
m_sflowPortConfMap[key] = port_info;
9296
}
9397

@@ -106,13 +110,19 @@ void SflowMgr::sflowUpdatePortInfo(Consumer &consumer)
106110
speed_change = true;
107111
}
108112

113+
string def_dir = "rx";
114+
if (m_sflowPortConfMap[key].dir != def_dir && !m_sflowPortConfMap[key].local_dir_cfg)
115+
{
116+
m_sflowPortConfMap[key].dir = def_dir;
117+
}
118+
109119
if (m_gEnable && m_intfAllConf)
110120
{
111121
// If the Local rate Conf is already present, dont't override it even though the speed is changed
112122
if (new_port || (speed_change && !m_sflowPortConfMap[key].local_rate_cfg))
113123
{
114124
vector<FieldValueTuple> fvs;
115-
sflowGetGlobalInfo(fvs, m_sflowPortConfMap[key].speed);
125+
sflowGetGlobalInfo(fvs, m_sflowPortConfMap[key].speed, m_sflowPortConfMap[key].dir);
116126
m_appSflowSessionTable.set(key, fvs);
117127
}
118128
}
@@ -123,7 +133,8 @@ void SflowMgr::sflowUpdatePortInfo(Consumer &consumer)
123133
if (sflowPortConf != m_sflowPortConfMap.end())
124134
{
125135
bool local_cfg = m_sflowPortConfMap[key].local_rate_cfg ||
126-
m_sflowPortConfMap[key].local_admin_cfg;
136+
m_sflowPortConfMap[key].local_admin_cfg ||
137+
m_sflowPortConfMap[key].local_dir_cfg;
127138

128139
m_sflowPortConfMap.erase(key);
129140
if ((m_intfAllConf && m_gEnable) || local_cfg)
@@ -136,25 +147,31 @@ void SflowMgr::sflowUpdatePortInfo(Consumer &consumer)
136147
}
137148
}
138149

139-
void SflowMgr::sflowHandleSessionAll(bool enable)
150+
void SflowMgr::sflowHandleSessionAll(bool enable, string direction)
140151
{
141152
for (auto it: m_sflowPortConfMap)
142153
{
143154
if (enable)
144155
{
145156
vector<FieldValueTuple> fvs;
146-
if (it.second.local_rate_cfg || it.second.local_admin_cfg)
157+
if (it.second.local_rate_cfg || it.second.local_admin_cfg || it.second.local_dir_cfg)
147158
{
148159
sflowGetPortInfo(fvs, it.second);
149160
/* Use global admin state if there is not a local one */
150161
if (!it.second.local_admin_cfg) {
151162
FieldValueTuple fv1("admin_state", "up");
152163
fvs.push_back(fv1);
153164
}
165+
166+
/* Use global sample direction state if there is not a local one */
167+
if (!it.second.local_dir_cfg) {
168+
FieldValueTuple fv2("sample_direction", direction);
169+
fvs.push_back(fv2);
170+
}
154171
}
155172
else
156173
{
157-
sflowGetGlobalInfo(fvs, it.second.speed);
174+
sflowGetGlobalInfo(fvs, it.second.speed, direction);
158175
}
159176
m_appSflowSessionTable.set(it.first, fvs);
160177
}
@@ -169,7 +186,7 @@ void SflowMgr::sflowHandleSessionLocal(bool enable)
169186
{
170187
for (auto it: m_sflowPortConfMap)
171188
{
172-
if (it.second.local_admin_cfg || it.second.local_rate_cfg)
189+
if (it.second.local_admin_cfg || it.second.local_rate_cfg || it.second.local_dir_cfg)
173190
{
174191
vector<FieldValueTuple> fvs;
175192
sflowGetPortInfo(fvs, it.second);
@@ -185,7 +202,7 @@ void SflowMgr::sflowHandleSessionLocal(bool enable)
185202
}
186203
}
187204

188-
void SflowMgr::sflowGetGlobalInfo(vector<FieldValueTuple> &fvs, string speed)
205+
void SflowMgr::sflowGetGlobalInfo(vector<FieldValueTuple> &fvs, string speed, string dir)
189206
{
190207
string rate;
191208
FieldValueTuple fv1("admin_state", "up");
@@ -201,6 +218,9 @@ void SflowMgr::sflowGetGlobalInfo(vector<FieldValueTuple> &fvs, string speed)
201218
}
202219
FieldValueTuple fv2("sample_rate",rate);
203220
fvs.push_back(fv2);
221+
222+
FieldValueTuple fv3("sample_direction",dir);
223+
fvs.push_back(fv3);
204224
}
205225

206226
void SflowMgr::sflowGetPortInfo(vector<FieldValueTuple> &fvs, SflowPortInfo &local_info)
@@ -213,6 +233,12 @@ void SflowMgr::sflowGetPortInfo(vector<FieldValueTuple> &fvs, SflowPortInfo &loc
213233

214234
FieldValueTuple fv2("sample_rate", local_info.rate);
215235
fvs.push_back(fv2);
236+
237+
if (local_info.local_dir_cfg)
238+
{
239+
FieldValueTuple fv3("sample_direction", local_info.dir);
240+
fvs.push_back(fv3);
241+
}
216242
}
217243

218244
void SflowMgr::sflowCheckAndFillValues(string alias, vector<FieldValueTuple> &values,
@@ -221,6 +247,7 @@ void SflowMgr::sflowCheckAndFillValues(string alias, vector<FieldValueTuple> &va
221247
string rate;
222248
bool admin_present = false;
223249
bool rate_present = false;
250+
bool dir_present = false;
224251

225252
for (auto i : values)
226253
{
@@ -240,6 +267,14 @@ void SflowMgr::sflowCheckAndFillValues(string alias, vector<FieldValueTuple> &va
240267
FieldValueTuple fv(fvField(i), fvValue(i));
241268
fvs.push_back(fv);
242269
}
270+
if (fvField(i) == "sample_direction")
271+
{
272+
dir_present = true;
273+
m_sflowPortConfMap[alias].dir = fvValue(i);
274+
m_sflowPortConfMap[alias].local_dir_cfg = true;
275+
FieldValueTuple fv(fvField(i), fvValue(i));
276+
fvs.push_back(fv);
277+
}
243278
if (fvField(i) == "NULL")
244279
{
245280
continue;
@@ -282,6 +317,18 @@ void SflowMgr::sflowCheckAndFillValues(string alias, vector<FieldValueTuple> &va
282317
FieldValueTuple fv("admin_state", m_sflowPortConfMap[alias].admin);
283318
fvs.push_back(fv);
284319
}
320+
321+
if (!dir_present)
322+
{
323+
if (m_sflowPortConfMap[alias].dir == "")
324+
{
325+
/* By default direction is set to global, if not set explicitly */
326+
m_sflowPortConfMap[alias].dir = m_gDirection;
327+
}
328+
m_sflowPortConfMap[alias].local_dir_cfg = false;
329+
FieldValueTuple fv("sample_direction", m_sflowPortConfMap[alias].dir);
330+
fvs.push_back(fv);
331+
}
285332
}
286333

287334
void SflowMgr::doTask(Consumer &consumer)
@@ -309,51 +356,92 @@ void SflowMgr::doTask(Consumer &consumer)
309356
{
310357
if (table == CFG_SFLOW_TABLE_NAME)
311358
{
359+
SWSS_LOG_DEBUG("Current Cfg admin %d dir %s ", (unsigned int)m_gEnable, m_gDirection.c_str());
360+
bool enable = false;
361+
string direction = "rx";
312362
for (auto i : values)
313363
{
314364
if (fvField(i) == "admin_state")
315365
{
316-
bool enable = false;
317366
if (fvValue(i) == "up")
318367
{
319368
enable = true;
320369
}
321-
if (enable == m_gEnable)
322-
{
323-
break;
324-
}
325-
m_gEnable = enable;
326-
sflowHandleService(enable);
327-
if (m_intfAllConf)
328-
{
329-
sflowHandleSessionAll(enable);
330-
}
331-
sflowHandleSessionLocal(enable);
370+
}
371+
else if (fvField(i) == "sample_direction")
372+
{
373+
direction = fvValue(i);
332374
}
333375
}
376+
377+
if (direction != m_gDirection)
378+
{
379+
m_gDirection = direction;
380+
}
381+
382+
if (m_gEnable != enable)
383+
{
384+
m_gEnable = enable;
385+
sflowHandleService(enable);
386+
}
387+
388+
if (m_intfAllConf)
389+
{
390+
sflowHandleSessionAll(m_gEnable, m_gDirection);
391+
}
392+
393+
sflowHandleSessionLocal(m_gEnable);
334394
m_appSflowTable.set(key, values);
395+
396+
SWSS_LOG_DEBUG("New config admin %d dir %s ", (unsigned int)m_gEnable, m_gDirection.c_str());
335397
}
336398
else if (table == CFG_SFLOW_SESSION_TABLE_NAME)
337399
{
338400
if (key == "all")
339401
{
402+
SWSS_LOG_DEBUG("current config gAdmin %d dir %s intfAllEna %d intfAllDir %s",
403+
(unsigned int)m_gEnable, m_gDirection.c_str(),
404+
(unsigned int)m_intfAllConf, m_intfAllDir.c_str());
405+
406+
string direction = m_intfAllDir;
407+
bool enable = m_intfAllConf;
340408
for (auto i : values)
341409
{
342410
if (fvField(i) == "admin_state")
343411
{
344-
bool enable = false;
345-
346412
if (fvValue(i) == "up")
347413
{
348414
enable = true;
349415
}
350-
if ((enable != m_intfAllConf) && (m_gEnable))
416+
else if (fvValue(i) == "down")
351417
{
352-
sflowHandleSessionAll(enable);
418+
enable = false;
353419
}
354-
m_intfAllConf = enable;
355420
}
421+
else if (fvField(i) == "sample_direction")
422+
{
423+
direction = fvValue(i);
424+
}
425+
}
426+
427+
if (m_intfAllDir != direction)
428+
{
429+
m_intfAllDir = direction;
430+
}
431+
432+
if (enable != m_intfAllConf)
433+
{
434+
m_intfAllConf = enable;
435+
}
436+
437+
if (m_gEnable)
438+
{
439+
sflowHandleSessionAll(m_intfAllConf, m_intfAllDir);
356440
}
441+
442+
SWSS_LOG_DEBUG("New config gAdmin %d dir %s intfAllEna %d intfAllDir %s",
443+
(unsigned int)m_gEnable, m_gDirection.c_str(),
444+
(unsigned int)m_intfAllConf, m_intfAllDir.c_str());
357445
}
358446
else
359447
{
@@ -380,10 +468,11 @@ void SflowMgr::doTask(Consumer &consumer)
380468
if (m_gEnable)
381469
{
382470
sflowHandleService(false);
383-
sflowHandleSessionAll(false);
471+
sflowHandleSessionAll(false, "");
384472
sflowHandleSessionLocal(false);
385473
}
386474
m_gEnable = false;
475+
m_gDirection = "rx";
387476
m_appSflowTable.del(key);
388477
}
389478
else if (table == CFG_SFLOW_SESSION_TABLE_NAME)
@@ -394,7 +483,7 @@ void SflowMgr::doTask(Consumer &consumer)
394483
{
395484
if (m_gEnable)
396485
{
397-
sflowHandleSessionAll(true);
486+
sflowHandleSessionAll(true, m_gDirection);
398487
}
399488
}
400489
m_intfAllConf = true;
@@ -404,14 +493,16 @@ void SflowMgr::doTask(Consumer &consumer)
404493
m_appSflowSessionTable.del(key);
405494
m_sflowPortConfMap[key].local_rate_cfg = false;
406495
m_sflowPortConfMap[key].local_admin_cfg = false;
496+
m_sflowPortConfMap[key].local_dir_cfg = false;
407497
m_sflowPortConfMap[key].rate = "";
408498
m_sflowPortConfMap[key].admin = "";
499+
m_sflowPortConfMap[key].dir = "";
409500

410501
/* If Global configured, set global session on port after local config is deleted */
411502
if (m_intfAllConf)
412503
{
413504
vector<FieldValueTuple> fvs;
414-
sflowGetGlobalInfo(fvs, m_sflowPortConfMap[key].speed);
505+
sflowGetGlobalInfo(fvs, m_sflowPortConfMap[key].speed, m_intfAllDir);
415506
m_appSflowSessionTable.set(key,fvs);
416507
}
417508
}

cfgmgr/sflowmgr.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ struct SflowPortInfo
3434
{
3535
bool local_rate_cfg;
3636
bool local_admin_cfg;
37+
bool local_dir_cfg;
3738
std::string speed;
3839
std::string rate;
3940
std::string admin;
41+
std::string dir;
4042
};
4143

4244
/* Port to Local config map */
@@ -56,15 +58,17 @@ class SflowMgr : public Orch
5658
SflowPortConfMap m_sflowPortConfMap;
5759
bool m_intfAllConf;
5860
bool m_gEnable;
61+
std::string m_intfAllDir;
62+
std::string m_gDirection;
5963

6064
void doTask(Consumer &consumer);
6165
void sflowHandleService(bool enable);
6266
void sflowUpdatePortInfo(Consumer &consumer);
63-
void sflowHandleSessionAll(bool enable);
67+
void sflowHandleSessionAll(bool enable, std::string direction);
6468
void sflowHandleSessionLocal(bool enable);
6569
void sflowCheckAndFillValues(std::string alias, std::vector<FieldValueTuple> &values, std::vector<FieldValueTuple> &fvs);
6670
void sflowGetPortInfo(std::vector<FieldValueTuple> &fvs, SflowPortInfo &local_info);
67-
void sflowGetGlobalInfo(std::vector<FieldValueTuple> &fvs, std::string speed);
71+
void sflowGetGlobalInfo(std::vector<FieldValueTuple> &fvs, std::string speed, std::string direction);
6872
};
6973

7074
}

0 commit comments

Comments
 (0)