From 1bc4da2bf8e75a25c59cc16f270408d53777bcd4 Mon Sep 17 00:00:00 2001 From: yaqiangz Date: Tue, 10 Dec 2024 11:15:48 +0000 Subject: [PATCH 1/2] Clear counter when dhcp6relay init --- src/relay.cpp | 17 +++++++++++++++++ src/relay.h | 9 +++++++++ test/mock_relay.cpp | 10 ++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/relay.cpp b/src/relay.cpp index 60de068..10fe635 100644 --- a/src/relay.cpp +++ b/src/relay.cpp @@ -271,6 +271,7 @@ bool DHCPv6Msg::UnmarshalBinary(const uint8_t *packet, uint16_t len) { * @return none */ void initialize_counter(std::shared_ptr 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)); @@ -1334,3 +1335,19 @@ void shutdown_relay() { event_base_free(base); deinitialize_swss(); } + +/** + * @code clear_counter(std::shared_ptr state_db); + * + * @brief Clear all counter + * + * @param state_db state_db connector pointer + * + */ +void clear_counter(std::shared_ptr 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); + } +} diff --git a/src/relay.h b/src/relay.h index 5fb009d..fa10cf7 100644 --- a/src/relay.h +++ b/src/relay.h @@ -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 state_db); + * + * @brief Clear all counter + * + * @param state_db state_db connector pointer + * + */ +void clear_counter(std::shared_ptr state_db); diff --git a/test/mock_relay.cpp b/test/mock_relay.cpp index c4be1ee..15cfa75 100644 --- a/test/mock_relay.cpp +++ b/test/mock_relay.cpp @@ -345,6 +345,16 @@ TEST(counter, increase_counter) EXPECT_EQ(*ptr, "1"); } +TEST(counter, clear_counter) +{ + std::shared_ptr state_db = std::make_shared ("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[] = { From a82d4b6a38947e6b714dd48057b89d05df316a06 Mon Sep 17 00:00:00 2001 From: yaqiangz Date: Wed, 11 Dec 2024 13:33:02 +0000 Subject: [PATCH 2/2] Update ut --- test/mock_relay.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/mock_relay.cpp b/test/mock_relay.cpp index 15cfa75..6fe7a56 100644 --- a/test/mock_relay.cpp +++ b/test/mock_relay.cpp @@ -350,9 +350,31 @@ TEST(counter, clear_counter) std::shared_ptr state_db = std::make_shared ("STATE_DB", 0); std::string ifname = "Vlan1000"; initialize_counter(state_db, ifname); - EXPECT_FALSE(state_db->exists("DHCPv6_COUNTER_TABLE|Vlan1000")); + 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->exists("DHCPv6_COUNTER_TABLE|Vlan1000")); + 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)