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

[counter] Clear counter table when dhcp6relay init #45

Merged
merged 1 commit into from
Dec 19, 2023
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
20 changes: 19 additions & 1 deletion src/relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ void initialize_counter(std::shared_ptr<swss::DBConnector> state_db, std::string
std::string table_name = counter_table + ifname;

auto init_value = gen_counter_json_str(DHCPv6_MESSAGE_TYPE_UNKNOWN, 0);
state_db->del(table_name);
state_db->hset(table_name, "RX", init_value);
state_db->hset(table_name, "TX", init_value);
}
Expand Down Expand Up @@ -1484,6 +1483,23 @@ void prepare_socket_callback(event_base *base, int socket, void (*cb)(evutil_soc
event_add(event, NULL);
}

/**
* @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);
}
}

/**
* @code loop_relay(std::unordered_map<relay_config> &vlans);
*
Expand Down Expand Up @@ -1513,6 +1529,8 @@ void loop_relay(std::unordered_map<std::string, relay_config> &vlans) {
prepare_socket_callback(base, out_filter, outbound_callback, reinterpret_cast<void *>(state_db.get()));
sockets.push_back(out_filter);

clear_counter(state_db);

int lo_sock = -1;
if (dual_tor_sock) {
std::string lo_string(loopback);
Expand Down
10 changes: 10 additions & 0 deletions src/relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,13 @@ void packet_counting_handler(uint8_t *buffer, ssize_t length, std::string &ifnam
*
*/
void prepare_socket_callback(event_base *base, int socket, void (*cb)(evutil_socket_t, short, void *), 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);
11 changes: 11 additions & 0 deletions test/mock_relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ TEST(counter, initialize_counter)
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "TX"));
}

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", "RX"));
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "TX"));
clear_counter(state_db);
EXPECT_FALSE(state_db->exists("DHCPv6_COUNTER_TABLE|Vlan1000"));
}

TEST(counter, increase_counter)
{
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
Expand Down