From 1bc4da2bf8e75a25c59cc16f270408d53777bcd4 Mon Sep 17 00:00:00 2001 From: yaqiangz Date: Tue, 10 Dec 2024 11:15:48 +0000 Subject: [PATCH] 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[] = {