Skip to content

Commit 353fee2

Browse files
committed
Prometheus UT for rddrop
- passing the right cookie, ensuring cookie's msb is preserved - waiting for the flows and stats state to be setup so that prom metric gets created Mock policy stats mgr to be added Signed-off-by: Gautam Venkataramanan <gautam.chennai@gmail.com> Change-Id: I5ace2a2c3b33ec3412ea159a3a790f758ec4d6cb
1 parent 2ed834c commit 353fee2

File tree

2 files changed

+86
-11
lines changed

2 files changed

+86
-11
lines changed

agent-ovs/ovs/include/ContractStatsManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class ContractStatsManager : public PolicyStatsManager {
123123

124124
void updatePolicyStatsDropCounters(const std::string& rdURI,
125125
PolicyDropCounters_t& counters);
126+
127+
friend class ContractStatsManagerFixture;
126128
};
127129

128130
} /* namespace opflexagent */

agent-ovs/ovs/test/ContractStatsManager_test.cpp

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,39 @@ class ContractStatsManagerFixture : public PolicyStatsManagerFixture {
5252

5353
public:
5454
ContractStatsManagerFixture() : PolicyStatsManagerFixture(),
55+
intFlowManager(agent, switchManager, idGen,
56+
ctZoneManager, tunnelEpManager),
5557
contractStatsManager(&agent, idGen,
5658
switchManager, 10),
5759
policyManager(agent.getPolicyManager()) {
60+
switchManager.setMaxFlowTables(IntFlowManager::NUM_FLOW_TABLES);
61+
intFlowManager.start();
5862
createObjects();
5963
createPolicyObjects();
6064
idGen.initNamespace("l24classifierRule");
6165
idGen.initNamespace("routingDomain");
6266
switchManager.setMaxFlowTables(IntFlowManager::NUM_FLOW_TABLES);
6367
}
6468
virtual ~ContractStatsManagerFixture() {
69+
intFlowManager.stop();
6570
stop();
6671
}
6772
void verifyRoutingDomainDropStats(shared_ptr<RoutingDomain> rd,
6873
uint32_t packet_count,
6974
uint32_t byte_count);
75+
void waitForRdDropEntry(void);
7076
#ifdef HAVE_PROMETHEUS_SUPPORT
7177
virtual void verifyPromMetrics(shared_ptr<L24Classifier> classifier,
7278
uint32_t pkts,
7379
uint32_t bytes,
7480
bool isTx=false) override;
81+
void verifyRdDropPromMetrics(uint32_t pkts, uint32_t bytes);
7582
#endif
83+
IntFlowManager intFlowManager;
7684
ContractStatsManager contractStatsManager;
7785
PolicyManager& policyManager;
7886
private:
87+
bool checkNewFlowMapSize(size_t pol_table_size);
7988
};
8089

8190
#ifdef HAVE_PROMETHEUS_SUPPORT
@@ -103,6 +112,23 @@ verifyPromMetrics (shared_ptr<L24Classifier> classifier,
103112
pos = output.find(s_bytes);
104113
BOOST_CHECK_NE(pos, std::string::npos);
105114
}
115+
116+
void ContractStatsManagerFixture::
117+
verifyRdDropPromMetrics (uint32_t pkts,
118+
uint32_t bytes)
119+
{
120+
const std::string& s_pkts = "opflex_policy_drop_packets{routing_domain=\"tenant0:rd0\"} "\
121+
+ boost::lexical_cast<std::string>(pkts) + ".000000";
122+
const std::string& s_bytes = "opflex_policy_drop_bytes{routing_domain=\"tenant0:rd0\"} "\
123+
+ boost::lexical_cast<std::string>(bytes) + ".000000";
124+
125+
const std::string& output = BaseFixture::getOutputFromCommand(cmd);
126+
size_t pos = std::string::npos;
127+
pos = output.find(s_pkts);
128+
BOOST_CHECK_NE(pos, std::string::npos);
129+
pos = output.find(s_bytes);
130+
BOOST_CHECK_NE(pos, std::string::npos);
131+
}
106132
#endif
107133

108134
void ContractStatsManagerFixture::
@@ -115,21 +141,26 @@ verifyRoutingDomainDropStats(shared_ptr<RoutingDomain> rd,
115141

116142
auto uuid =
117143
boost::lexical_cast<string>(contractStatsManager.getAgentUUID());
144+
WAIT_FOR_DO_ONFAIL(su.get()->resolveGbpeRoutingDomainDropCounter(uuid,
145+
contractStatsManager.getCurrDropGenId(),
146+
rd->getURI().toString()),
147+
500,, LOG(ERROR) << "Obj not resolved";);
118148
optional<shared_ptr<RoutingDomainDropCounter> > myCounter =
119149
su.get()->resolveGbpeRoutingDomainDropCounter(uuid,
120150
contractStatsManager
121151
.getCurrDropGenId(),
122152
rd->getURI().toString());
123-
if (myCounter) {
124-
BOOST_CHECK_EQUAL(myCounter.get()->getPackets().get(),
125-
packet_count);
126-
BOOST_CHECK_EQUAL(myCounter.get()->getBytes().get(),
127-
byte_count);
128-
}
153+
BOOST_CHECK(myCounter);
154+
BOOST_CHECK_EQUAL(myCounter.get()->getPackets().get(), packet_count);
155+
BOOST_CHECK_EQUAL(myCounter.get()->getBytes().get(), byte_count);
156+
157+
#ifdef HAVE_PROMETHEUS_SUPPORT
158+
verifyRdDropPromMetrics(packet_count, byte_count);
159+
#endif
129160
}
130161

131162
struct ofpbuf *makeFlowStatReplyMessage(MockConnection *pConn,
132-
uint32_t priority, uint32_t cookie,
163+
uint32_t priority, uint64_t cookie,
133164
uint32_t packet_count,
134165
uint32_t byte_count,
135166
uint32_t reg0, uint32_t reg2,
@@ -159,7 +190,7 @@ struct ofpbuf *makeFlowStatReplyMessage(MockConnection *pConn,
159190
bzero(fs, sizeof(struct ofputil_flow_stats));
160191
fs->table_id = IntFlowManager::POL_TABLE_ID;
161192
fs->priority = priority;
162-
fs->cookie = ovs_htonll((uint64_t)cookie);
193+
fs->cookie = cookie;
163194
fs->packet_count = packet_count;
164195
fs->byte_count = byte_count;
165196
fs->flags = OFPUTIL_FF_SEND_FLOW_REM;
@@ -179,6 +210,44 @@ struct ofpbuf *makeFlowStatReplyMessage(MockConnection *pConn,
179210

180211
}
181212

213+
bool ContractStatsManagerFixture::checkNewFlowMapSize (size_t pol_table_size)
214+
{
215+
// Call on_timer function to process the flow entries received from
216+
// switchManager.
217+
boost::system::error_code ec;
218+
ec = make_error_code(boost::system::errc::success);
219+
contractStatsManager.on_timer(ec);
220+
221+
std::lock_guard<std::mutex> lock(contractStatsManager.pstatMtx);
222+
if (contractStatsManager.contractState.newFlowCounterMap.size() == pol_table_size)
223+
return true;
224+
225+
return false;
226+
}
227+
228+
// Wait for IntFlowManager to create rddrop flow and stats tables to get initialized
229+
void ContractStatsManagerFixture::waitForRdDropEntry (void)
230+
{
231+
// 1 table-drop static entry in POL table with stats enabled
232+
WAIT_FOR_DO_ONFAIL(checkNewFlowMapSize(1),
233+
500,,
234+
LOG(ERROR) << "##### flow state not fully setup ####";);
235+
236+
intFlowManager.domainUpdated(RoutingDomain::CLASS_ID, rd0->getURI());
237+
238+
// 1 entry is installed in policy table per VRF for collecting rddrop stats
239+
WAIT_FOR_DO_ONFAIL(checkNewFlowMapSize(2),
240+
500,,
241+
LOG(ERROR) << "##### flow state not fully setup ####";);
242+
243+
// rdid for this rd should have been allocated
244+
WAIT_FOR_DO_ONFAIL(
245+
(idGen.getIdNoAlloc(IntFlowManager::getIdNamespace(RoutingDomain::CLASS_ID),
246+
rd0->getURI().toString()) != (uint32_t)-1),
247+
500,,
248+
LOG(ERROR) << "rdId not yet alloc'd for rd0");
249+
}
250+
182251
BOOST_AUTO_TEST_SUITE(ContractStatsManager_test)
183252

184253
BOOST_FIXTURE_TEST_CASE(testFlowMatchStats, ContractStatsManagerFixture) {
@@ -209,21 +278,25 @@ BOOST_FIXTURE_TEST_CASE(testRdDropStats, ContractStatsManagerFixture) {
209278
contractStatsManager.registerConnection(&integrationPortConn);
210279
contractStatsManager.start();
211280
LOG(DEBUG) << "### rddrop stats start";
281+
waitForRdDropEntry();
212282

213283
// get rdId
214284
uint32_t rdId =
215-
idGen.getId(IntFlowManager::getIdNamespace(RoutingDomain::CLASS_ID),
216-
rd0->getURI().toString());
285+
idGen.getIdNoAlloc(IntFlowManager::getIdNamespace(RoutingDomain::CLASS_ID),
286+
rd0->getURI().toString());
217287
uint32_t priority = 1;
218288
uint32_t packet_count = 39;
219289
uint32_t byte_count = 6994;
220290

221291
/* create per RD flow drop stats */
222292
struct ofpbuf *res_msg = makeFlowStatReplyMessage(&integrationPortConn,
223-
priority, 0,
293+
priority,
294+
flow::cookie::RD_POL_DROP_FLOW,
224295
packet_count, byte_count,
225296
0, 0, rdId);
226297
BOOST_REQUIRE(res_msg!=0);
298+
ofp_header *msgHdr = (ofp_header *)res_msg->data;
299+
contractStatsManager.testInjectTxnId(msgHdr->xid);
227300

228301
contractStatsManager.Handle(&integrationPortConn,
229302
OFPTYPE_FLOW_STATS_REPLY, res_msg);

0 commit comments

Comments
 (0)