Skip to content

Commit 5ae186f

Browse files
authored
[counter] Clear counter table when init (sonic-net#45)
1 parent 40c6877 commit 5ae186f

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/relay.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ void initialize_counter(std::shared_ptr<swss::DBConnector> state_db, std::string
322322
std::string table_name = counter_table + ifname;
323323

324324
auto init_value = gen_counter_json_str(DHCPv6_MESSAGE_TYPE_UNKNOWN, 0);
325-
state_db->del(table_name);
326325
state_db->hset(table_name, "RX", init_value);
327326
state_db->hset(table_name, "TX", init_value);
328327
}
@@ -1484,6 +1483,23 @@ void prepare_socket_callback(event_base *base, int socket, void (*cb)(evutil_soc
14841483
event_add(event, NULL);
14851484
}
14861485

1486+
/**
1487+
* @code clear_counter(std::shared_ptr<swss::DBConnector> state_db);
1488+
*
1489+
* @brief Clear all counter
1490+
*
1491+
* @param state_db state_db connector pointer
1492+
*
1493+
*/
1494+
void clear_counter(std::shared_ptr<swss::DBConnector> state_db) {
1495+
std::string match_pattern = counter_table + std::string("*");
1496+
auto keys = state_db->keys(match_pattern);
1497+
1498+
for (auto &itr : keys) {
1499+
state_db->del(itr);
1500+
}
1501+
}
1502+
14871503
/**
14881504
* @code loop_relay(std::unordered_map<relay_config> &vlans);
14891505
*
@@ -1513,6 +1529,8 @@ void loop_relay(std::unordered_map<std::string, relay_config> &vlans) {
15131529
prepare_socket_callback(base, out_filter, outbound_callback, reinterpret_cast<void *>(state_db.get()));
15141530
sockets.push_back(out_filter);
15151531

1532+
clear_counter(state_db);
1533+
15161534
int lo_sock = -1;
15171535
if (dual_tor_sock) {
15181536
std::string lo_string(loopback);

src/relay.h

+10
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,13 @@ void packet_counting_handler(uint8_t *buffer, ssize_t length, std::string &ifnam
559559
*
560560
*/
561561
void prepare_socket_callback(event_base *base, int socket, void (*cb)(evutil_socket_t, short, void *), void *arg);
562+
563+
/**
564+
* @code clear_counter(std::shared_ptr<swss::DBConnector> state_db);
565+
*
566+
* @brief Clear all counter
567+
*
568+
* @param state_db state_db connector pointer
569+
*
570+
*/
571+
void clear_counter(std::shared_ptr<swss::DBConnector> state_db);

test/mock_relay.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,17 @@ TEST(counter, initialize_counter)
325325
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "TX"));
326326
}
327327

328+
TEST(counter, clear_counter)
329+
{
330+
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);
331+
std::string ifname = "Vlan1000";
332+
initialize_counter(state_db, ifname);
333+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "RX"));
334+
EXPECT_TRUE(state_db->hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "TX"));
335+
clear_counter(state_db);
336+
EXPECT_FALSE(state_db->exists("DHCPv6_COUNTER_TABLE|Vlan1000"));
337+
}
338+
328339
TEST(counter, increase_counter)
329340
{
330341
std::shared_ptr<swss::DBConnector> state_db = std::make_shared<swss::DBConnector> ("STATE_DB", 0);

0 commit comments

Comments
 (0)