Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action] [PR:51] Clear counter when dhcp6relay init (#51) #65

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
32 changes: 32 additions & 0 deletions test/mock_relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,38 @@ 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_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Advertise"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Request"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Confirm"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Renew"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Rebind"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Reply"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Release"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Decline"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Forward"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Reply"));
clear_counter(state_db);
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Solicit"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Advertise"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Request"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Confirm"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Renew"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Rebind"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Reply"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Release"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Decline"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Forward"));
EXPECT_FALSE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Relay-Reply"));
}

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