Skip to content

Commit

Permalink
Clear counter when dhcp6relay init
Browse files Browse the repository at this point in the history
  • Loading branch information
yaqiangz committed Dec 10, 2024
1 parent 84e4419 commit 1bc4da2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ bool DHCPv6Msg::UnmarshalBinary(const uint8_t *packet, uint16_t len) {
* @return none
*/
void initialize_counter(std::shared_ptr<swss::DBConnector> state_db, std::string &ifname) {
clear_counter(state_db);
std::string table_name = counter_table + ifname;
for (auto &intr : counterMap) {
state_db->hset(table_name, intr.second, toString(0));
Expand Down Expand Up @@ -1334,3 +1335,19 @@ void shutdown_relay() {
event_base_free(base);
deinitialize_swss();
}

/**
* @code clear_counter(std::shared_ptr<swss::DBConnector> state_db);
*
* @brief Clear all counter
*
* @param state_db state_db connector pointer
*
*/
void clear_counter(std::shared_ptr<swss::DBConnector> state_db) {
std::string match_pattern = counter_table + std::string("*");
auto keys = state_db->keys(match_pattern);
for (auto &itr : keys) {
state_db->del(itr);
}
}
9 changes: 9 additions & 0 deletions src/relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,12 @@ void client_packet_handler(uint8_t *buffer, ssize_t length, struct relay_config
*/
void server_callback(evutil_socket_t fd, short event, void *arg);

/**
* @code clear_counter(std::shared_ptr<swss::DBConnector> state_db);
*
* @brief Clear all counter
*
* @param state_db state_db connector pointer
*
*/
void clear_counter(std::shared_ptr<swss::DBConnector> state_db);
10 changes: 10 additions & 0 deletions test/mock_relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ TEST(counter, increase_counter)
EXPECT_EQ(*ptr, "1");
}

TEST(counter, clear_counter)
{
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
std::string ifname = "Vlan1000";
initialize_counter(state_db, ifname);
EXPECT_FALSE(state_db->exists("DHCPv6_COUNTER_TABLE|Vlan1000"));
clear_counter(state_db);
EXPECT_FALSE(state_db->exists("DHCPv6_COUNTER_TABLE|Vlan1000"));
}

TEST(relay, relay_client)
{
uint8_t msg[] = {
Expand Down

0 comments on commit 1bc4da2

Please sign in to comment.