diff --git a/infra/packet_manager/CMakeLists.txt b/infra/packet_manager/CMakeLists.txt index de5b0bb..05dc5ef 100644 --- a/infra/packet_manager/CMakeLists.txt +++ b/infra/packet_manager/CMakeLists.txt @@ -3,7 +3,7 @@ add_library(packet_manager packet_pool.c packet_parser.c packet_builder.c - packet_filter.c + packet_dabloom.c packet_dump.c packet_utils.c checksum.c) diff --git a/infra/packet_manager/packet_filter.c b/infra/packet_manager/packet_dabloom.c similarity index 70% rename from infra/packet_manager/packet_filter.c rename to infra/packet_manager/packet_dabloom.c index 04e6cb8..8bed791 100644 --- a/infra/packet_manager/packet_filter.c +++ b/infra/packet_manager/packet_dabloom.c @@ -1,5 +1,5 @@ #include "dablooms.h" -#include "packet_filter.h" +#include "packet_dabloom.h" #include "packet_internal.h" struct packet_key @@ -17,7 +17,7 @@ struct packet_key uint32_t dst_addr; // network order } __attribute__((__packed__)); -struct packet_filter +struct packet_dabloom { struct expiry_dablooms_handle *dablooms; }; @@ -69,41 +69,41 @@ static inline int packet_key_get(const struct packet *packet, struct packet_key * Public API ******************************************************************************/ -struct packet_filter *packet_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now) +struct packet_dabloom *packet_dabloom_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now) { - struct packet_filter *filter = (struct packet_filter *)calloc(1, sizeof(struct packet_filter)); - if (filter == NULL) + struct packet_dabloom *pkt_dab = (struct packet_dabloom *)calloc(1, sizeof(struct packet_dabloom)); + if (pkt_dab == NULL) { return NULL; } - filter->dablooms = expiry_dablooms_new(capacity, error_rate, now, timeout); - if (filter->dablooms == NULL) + pkt_dab->dablooms = expiry_dablooms_new(capacity, error_rate, now, timeout); + if (pkt_dab->dablooms == NULL) { - free(filter); + free(pkt_dab); return NULL; } - return filter; + return pkt_dab; } -void packet_filter_free(struct packet_filter *filter) +void packet_dabloom_free(struct packet_dabloom *pkt_dab) { - if (filter) + if (pkt_dab) { - if (filter->dablooms) + if (pkt_dab->dablooms) { - expiry_dablooms_free(filter->dablooms); - filter->dablooms = NULL; + expiry_dablooms_free(pkt_dab->dablooms); + pkt_dab->dablooms = NULL; } - free(filter); - filter = NULL; + free(pkt_dab); + pkt_dab = NULL; } } // return 1: found // reutrn 0: no found -int packet_filter_lookup(struct packet_filter *filter, const struct packet *packet, uint64_t now) +int packet_dabloom_lookup(struct packet_dabloom *pkt_dab, const struct packet *packet, uint64_t now) { struct packet_key key; if (packet_key_get(packet, &key) == -1) @@ -111,7 +111,7 @@ int packet_filter_lookup(struct packet_filter *filter, const struct packet *pack return 0; } - if (expiry_dablooms_search(filter->dablooms, (const char *)&key, sizeof(struct packet_key), now) == 1) + if (expiry_dablooms_search(pkt_dab->dablooms, (const char *)&key, sizeof(struct packet_key), now) == 1) { return 1; } @@ -119,7 +119,7 @@ int packet_filter_lookup(struct packet_filter *filter, const struct packet *pack return 0; } -void packet_filter_add(struct packet_filter *filter, const struct packet *packet, uint64_t now) +void packet_dabloom_add(struct packet_dabloom *pkt_dab, const struct packet *packet, uint64_t now) { struct packet_key key; if (packet_key_get(packet, &key) == -1) @@ -127,5 +127,5 @@ void packet_filter_add(struct packet_filter *filter, const struct packet *packet return; } - expiry_dablooms_add(filter->dablooms, (const char *)&key, sizeof(struct packet_key), now); + expiry_dablooms_add(pkt_dab->dablooms, (const char *)&key, sizeof(struct packet_key), now); } diff --git a/infra/packet_manager/packet_dabloom.h b/infra/packet_manager/packet_dabloom.h new file mode 100644 index 0000000..f19f78e --- /dev/null +++ b/infra/packet_manager/packet_dabloom.h @@ -0,0 +1,22 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct packet; + +// Duplicated Packet Filter for IPv4 Packet +struct packet_dabloom; +struct packet_dabloom *packet_dabloom_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now); +void packet_dabloom_free(struct packet_dabloom *pkt_dab); + +// return 1: found +// reutrn 0: no found +int packet_dabloom_lookup(struct packet_dabloom *pkt_dab, const struct packet *pkt, uint64_t now); +void packet_dabloom_add(struct packet_dabloom *pkt_dab, const struct packet *pkt, uint64_t now); + +#ifdef __cplusplus +} +#endif diff --git a/infra/packet_manager/packet_filter.h b/infra/packet_manager/packet_filter.h deleted file mode 100644 index 9a30f62..0000000 --- a/infra/packet_manager/packet_filter.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct packet; - -// Duplicated Packet Filter for IPv4 Packet -struct packet_filter; -struct packet_filter *packet_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now); -void packet_filter_free(struct packet_filter *filter); - -// return 1: found -// reutrn 0: no found -int packet_filter_lookup(struct packet_filter *filter, const struct packet *pkt, uint64_t now); -void packet_filter_add(struct packet_filter *filter, const struct packet *pkt, uint64_t now); - -#ifdef __cplusplus -} -#endif diff --git a/infra/packet_manager/test/CMakeLists.txt b/infra/packet_manager/test/CMakeLists.txt index 2d47c0d..184d9fc 100644 --- a/infra/packet_manager/test/CMakeLists.txt +++ b/infra/packet_manager/test/CMakeLists.txt @@ -49,8 +49,8 @@ target_link_libraries(gtest_packet_parser packet_manager gtest) add_executable(gtest_packet_builder gtest_packet_builder.cpp) target_link_libraries(gtest_packet_builder packet_manager gtest) -add_executable(gtest_packet_filter gtest_packet_filter.cpp) -target_link_libraries(gtest_packet_filter packet_manager gtest) +add_executable(gtest_packet_dabloom gtest_packet_dabloom.cpp) +target_link_libraries(gtest_packet_dabloom packet_manager gtest) add_executable(gtest_packet_ldbc gtest_packet_ldbc.cpp) target_link_libraries(gtest_packet_ldbc packet_manager gtest) @@ -79,7 +79,7 @@ gtest_discover_tests(gtest_gtp2_utils) gtest_discover_tests(gtest_packet_frag) gtest_discover_tests(gtest_packet_parser) gtest_discover_tests(gtest_packet_builder) -gtest_discover_tests(gtest_packet_filter) +gtest_discover_tests(gtest_packet_dabloom) gtest_discover_tests(gtest_packet_ldbc) gtest_discover_tests(gtest_packet_pool) gtest_discover_tests(gtest_packet_manager) diff --git a/infra/packet_manager/test/gtest_packet_filter.cpp b/infra/packet_manager/test/gtest_packet_dabloom.cpp similarity index 85% rename from infra/packet_manager/test/gtest_packet_filter.cpp rename to infra/packet_manager/test/gtest_packet_dabloom.cpp index dd76720..0e172e0 100644 --- a/infra/packet_manager/test/gtest_packet_filter.cpp +++ b/infra/packet_manager/test/gtest_packet_dabloom.cpp @@ -3,7 +3,7 @@ #include "tuple.h" #include "packet_internal.h" #include "packet_parser.h" -#include "packet_filter.h" +#include "packet_dabloom.h" /****************************************************************************** * [Protocols in frame: eth:ethertype:ip:ipv6:tcp] @@ -67,7 +67,7 @@ unsigned char data[] = { 0x81, 0x80, 0x5c, 0x76, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x20, 0x00, 0xf7, 0x57, 0x00, 0x00, 0x02, 0x04, 0x04, 0xc4, 0x01, 0x03, 0x03, 0x08, 0x01, 0x01, 0x04, 0x02}; -TEST(DUPLICATED_PACKET_FILTER, TEST) +TEST(PACKET_DABLOOM, TEST) { struct packet pkt; uint32_t capacity = 1000000; @@ -77,17 +77,17 @@ TEST(DUPLICATED_PACKET_FILTER, TEST) memset(&pkt, 0, sizeof(pkt)); packet_parse(&pkt, (const char *)data, sizeof(data)); - struct packet_filter *filter = packet_filter_new(capacity, timeout, error_rate, 1); + struct packet_dabloom *filter = packet_dabloom_new(capacity, timeout, error_rate, 1); EXPECT_TRUE(filter != nullptr); - EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 1) == 0); // no found - packet_filter_add(filter, &pkt, 1); // add - EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 1) == 1); // found - EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 2) == 1); // found - EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 3) == 0); // not found - EXPECT_TRUE(packet_filter_lookup(filter, &pkt, 4) == 0); // not found + EXPECT_TRUE(packet_dabloom_lookup(filter, &pkt, 1) == 0); // no found + packet_dabloom_add(filter, &pkt, 1); // add + EXPECT_TRUE(packet_dabloom_lookup(filter, &pkt, 1) == 1); // found + EXPECT_TRUE(packet_dabloom_lookup(filter, &pkt, 2) == 1); // found + EXPECT_TRUE(packet_dabloom_lookup(filter, &pkt, 3) == 0); // not found + EXPECT_TRUE(packet_dabloom_lookup(filter, &pkt, 4) == 0); // not found - packet_filter_free(filter); + packet_dabloom_free(filter); } int main(int argc, char **argv)