Skip to content

Commit 1e7b13a

Browse files
mssonicbldyaqiangz
andauthored
[202411] Clear counter when dhcp6relay init (#51) (#65)
[202411] Clear counter when dhcp6relay init Co-authored-by: Yaqiang Zhu <[email protected]>
1 parent 5100549 commit 1e7b13a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/relay.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ bool DHCPv6Msg::UnmarshalBinary(const uint8_t *packet, uint16_t len) {
271271
* @return none
272272
*/
273273
void initialize_counter(std::shared_ptr<swss::DBConnector> state_db, std::string &ifname) {
274+
clear_counter(state_db);
274275
std::string table_name = counter_table + ifname;
275276
for (auto &intr : counterMap) {
276277
state_db->hset(table_name, intr.second, toString(0));
@@ -1334,3 +1335,19 @@ void shutdown_relay() {
13341335
event_base_free(base);
13351336
deinitialize_swss();
13361337
}
1338+
1339+
/**
1340+
* @code clear_counter(std::shared_ptr<swss::DBConnector> state_db);
1341+
*
1342+
* @brief Clear all counter
1343+
*
1344+
* @param state_db state_db connector pointer
1345+
*
1346+
*/
1347+
void clear_counter(std::shared_ptr<swss::DBConnector> state_db) {
1348+
std::string match_pattern = counter_table + std::string("*");
1349+
auto keys = state_db->keys(match_pattern);
1350+
for (auto &itr : keys) {
1351+
state_db->del(itr);
1352+
}
1353+
}

src/relay.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,12 @@ void client_packet_handler(uint8_t *buffer, ssize_t length, struct relay_config
474474
*/
475475
void server_callback(evutil_socket_t fd, short event, void *arg);
476476

477+
/**
478+
* @code clear_counter(std::shared_ptr<swss::DBConnector> state_db);
479+
*
480+
* @brief Clear all counter
481+
*
482+
* @param state_db state_db connector pointer
483+
*
484+
*/
485+
void clear_counter(std::shared_ptr<swss::DBConnector> state_db);

test/mock_relay.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,38 @@ TEST(counter, increase_counter)
345345
EXPECT_EQ(*ptr, "1");
346346
}
347347

348+
TEST(counter, clear_counter)
349+
{
350+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
351+
std::string ifname = "Vlan1000";
352+
initialize_counter(state_db, ifname);
353+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
354+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit"));
355+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Advertise"));
356+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Request"));
357+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Confirm"));
358+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Renew"));
359+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Rebind"));
360+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Reply"));
361+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Release"));
362+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Decline"));
363+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Forward"));
364+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Reply"));
365+
clear_counter(state_db);
366+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
367+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit"));
368+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Advertise"));
369+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Request"));
370+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Confirm"));
371+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Renew"));
372+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Rebind"));
373+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Reply"));
374+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Release"));
375+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Decline"));
376+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Forward"));
377+
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Reply"));
378+
}
379+
348380
TEST(relay, relay_client)
349381
{
350382
uint8_t msg[] = {

0 commit comments

Comments
 (0)