diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 567aea2..2e0eee6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,8 +5,6 @@ add_subdirectory(packet_io) add_subdirectory(snowflake) add_subdirectory(ip_reassembly) add_subdirectory(tcp_reassembly) -add_subdirectory(duplicated_packet_filter) -add_subdirectory(evicted_session_filter) add_subdirectory(session) add_subdirectory(plugin) add_subdirectory(stellar) \ No newline at end of file diff --git a/src/duplicated_packet_filter/CMakeLists.txt b/src/duplicated_packet_filter/CMakeLists.txt deleted file mode 100644 index 1bec5bf..0000000 --- a/src/duplicated_packet_filter/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_library(duplicated_packet_filter duplicated_packet_filter.cpp) -target_include_directories(duplicated_packet_filter PUBLIC ${CMAKE_CURRENT_LIST_DIR}) -target_link_libraries(duplicated_packet_filter packet dablooms) - -add_subdirectory(test) \ No newline at end of file diff --git a/src/duplicated_packet_filter/duplicated_packet_filter.h b/src/duplicated_packet_filter/duplicated_packet_filter.h deleted file mode 100644 index 6ac4288..0000000 --- a/src/duplicated_packet_filter/duplicated_packet_filter.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -// Duplicated Packet Filter for IPv4 Packet - -struct duplicated_packet_filter; -struct duplicated_packet_filter *duplicated_packet_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now); -void duplicated_packet_filter_free(struct duplicated_packet_filter *filter); - -// return 1: found -// reutrn 0: no found -int duplicated_packet_filter_lookup(struct duplicated_packet_filter *filter, const struct packet *pkt, uint64_t now); -void duplicated_packet_filter_add(struct duplicated_packet_filter *filter, const struct packet *pkt, uint64_t now); - -#ifdef __cplusplus -} -#endif diff --git a/src/duplicated_packet_filter/test/CMakeLists.txt b/src/duplicated_packet_filter/test/CMakeLists.txt deleted file mode 100644 index eb8f8d2..0000000 --- a/src/duplicated_packet_filter/test/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable(gtest_duplicated_packet_filter gtest_duplicated_packet_filter.cpp) -target_link_libraries(gtest_duplicated_packet_filter duplicated_packet_filter gtest) - -include(GoogleTest) -gtest_discover_tests(gtest_duplicated_packet_filter) \ No newline at end of file diff --git a/src/packet/CMakeLists.txt b/src/packet/CMakeLists.txt index d644619..7ce8900 100644 --- a/src/packet/CMakeLists.txt +++ b/src/packet/CMakeLists.txt @@ -1,8 +1,17 @@ -add_library(packet packet_parse.cpp packet_craft.cpp packet_dump.cpp packet_utils.cpp packet_ldbc.cpp packet_layer.cpp packet_tunnel.cpp checksum.cpp) +add_library(packet + packet_parse.cpp + packet_craft.cpp + packet_filter.cpp + packet_dump.cpp + packet_utils.cpp + packet_ldbc.cpp + packet_layer.cpp + packet_tunnel.cpp + checksum.cpp) target_include_directories(packet PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash) target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/include) target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/src/utils) -target_link_libraries(packet tuple log) +target_link_libraries(packet tuple log dablooms) add_subdirectory(test) \ No newline at end of file diff --git a/src/duplicated_packet_filter/duplicated_packet_filter.cpp b/src/packet/packet_filter.cpp similarity index 50% rename from src/duplicated_packet_filter/duplicated_packet_filter.cpp rename to src/packet/packet_filter.cpp index e92d2d2..3a866c3 100644 --- a/src/duplicated_packet_filter/duplicated_packet_filter.cpp +++ b/src/packet/packet_filter.cpp @@ -1,29 +1,26 @@ #include #include "dablooms.h" -#include "tcp_utils.h" -#include "udp_utils.h" -#include "ip4_utils.h" #include "packet_def.h" #include "packet_layer.h" -#include "duplicated_packet_filter.h" +#include "packet_filter.h" -struct duplicated_packet_key +struct packet_key { // TCP or UDP - uint32_t seq; - uint32_t ack; - uint16_t src_port; /* host order */ - uint16_t dst_port; /* host order */ - uint16_t l4_checksum; + uint32_t seq; // network order + uint32_t ack; // network order + uint16_t src_port; // network order + uint16_t dst_port; // network order + uint16_t l4_checksum; // network order // IPv4 - uint16_t ip_id; - uint32_t src_addr; /* host order */ - uint32_t dst_addr; /* host order */ + uint16_t ip_id; // network order + uint32_t src_addr; // network order + uint32_t dst_addr; // network order } __attribute__((__packed__)); -struct duplicated_packet_filter +struct packet_filter { struct expiry_dablooms_handle *dablooms; }; @@ -34,7 +31,7 @@ struct duplicated_packet_filter // return 0: success // reutrn -1: error -static inline int duplicated_packet_key_get(const struct packet *packet, struct duplicated_packet_key *key) +static inline int packet_key_get(const struct packet *packet, struct packet_key *key) { const struct raw_layer *ip_layer = packet_get_innermost_raw_layer(packet, LAYER_PROTO_IPV4); const struct raw_layer *tcp_layer = packet_get_innermost_raw_layer(packet, LAYER_PROTO_TCP); @@ -44,28 +41,28 @@ static inline int duplicated_packet_key_get(const struct packet *packet, struct return -1; } - memset(key, 0, sizeof(struct duplicated_packet_key)); + memset(key, 0, sizeof(struct packet_key)); const struct ip *iphdr = (const struct ip *)ip_layer->hdr_ptr; - key->ip_id = ip4_hdr_get_ipid(iphdr); - key->src_addr = ip4_hdr_get_src_addr(iphdr); - key->dst_addr = ip4_hdr_get_dst_addr(iphdr); + key->ip_id = iphdr->ip_id; + key->src_addr = iphdr->ip_src.s_addr; + key->dst_addr = iphdr->ip_dst.s_addr; if (tcp_layer) { const struct tcphdr *tcphdr = (const struct tcphdr *)tcp_layer->hdr_ptr; - key->seq = tcp_hdr_get_seq(tcphdr); - key->ack = tcp_hdr_get_ack(tcphdr); - key->src_port = tcp_hdr_get_src_port(tcphdr); - key->dst_port = tcp_hdr_get_dst_port(tcphdr); - key->l4_checksum = tcp_hdr_get_checksum(tcphdr); + key->seq = tcphdr->th_seq; + key->ack = tcphdr->th_ack; + key->src_port = tcphdr->th_sport; + key->dst_port = tcphdr->th_dport; + key->l4_checksum = tcphdr->th_sum; } else { const struct udphdr *udphdr = (const struct udphdr *)udp_layer->hdr_ptr; - key->src_port = udp_hdr_get_src_port(udphdr); - key->dst_port = udp_hdr_get_dst_port(udphdr); - key->l4_checksum = udp_hdr_get_checksum(udphdr); + key->src_port = udphdr->uh_sport; + key->dst_port = udphdr->uh_dport; + key->l4_checksum = udphdr->uh_sum; } return 0; @@ -75,9 +72,9 @@ static inline int duplicated_packet_key_get(const struct packet *packet, struct * Public API ******************************************************************************/ -struct duplicated_packet_filter *duplicated_packet_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now) +struct packet_filter *packet_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now) { - struct duplicated_packet_filter *filter = (struct duplicated_packet_filter *)calloc(1, sizeof(struct duplicated_packet_filter)); + struct packet_filter *filter = (struct packet_filter *)calloc(1, sizeof(struct packet_filter)); if (filter == NULL) { return NULL; @@ -93,7 +90,7 @@ struct duplicated_packet_filter *duplicated_packet_filter_new(uint32_t capacity, return filter; } -void duplicated_packet_filter_free(struct duplicated_packet_filter *filter) +void packet_filter_free(struct packet_filter *filter) { if (filter) { @@ -109,15 +106,15 @@ void duplicated_packet_filter_free(struct duplicated_packet_filter *filter) // return 1: found // reutrn 0: no found -int duplicated_packet_filter_lookup(struct duplicated_packet_filter *filter, const struct packet *packet, uint64_t now) +int packet_filter_lookup(struct packet_filter *filter, const struct packet *packet, uint64_t now) { - struct duplicated_packet_key key; - if (duplicated_packet_key_get(packet, &key) == -1) + struct packet_key key; + if (packet_key_get(packet, &key) == -1) { return 0; } - if (expiry_dablooms_search(filter->dablooms, (const char *)&key, sizeof(struct duplicated_packet_key), now) == 1) + if (expiry_dablooms_search(filter->dablooms, (const char *)&key, sizeof(struct packet_key), now) == 1) { return 1; } @@ -125,13 +122,13 @@ int duplicated_packet_filter_lookup(struct duplicated_packet_filter *filter, con return 0; } -void duplicated_packet_filter_add(struct duplicated_packet_filter *filter, const struct packet *packet, uint64_t now) +void packet_filter_add(struct packet_filter *filter, const struct packet *packet, uint64_t now) { - struct duplicated_packet_key key; - if (duplicated_packet_key_get(packet, &key) == -1) + struct packet_key key; + if (packet_key_get(packet, &key) == -1) { return; } - expiry_dablooms_add(filter->dablooms, (const char *)&key, sizeof(struct duplicated_packet_key), now); + expiry_dablooms_add(filter->dablooms, (const char *)&key, sizeof(struct packet_key), now); } diff --git a/src/packet/packet_filter.h b/src/packet/packet_filter.h new file mode 100644 index 0000000..e5ccae4 --- /dev/null +++ b/src/packet/packet_filter.h @@ -0,0 +1,22 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +// 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/src/duplicated_packet_filter/test/gtest_duplicated_packet_filter.cpp b/src/packet/test/gtest_packet_filter.cpp similarity index 83% rename from src/duplicated_packet_filter/test/gtest_duplicated_packet_filter.cpp rename to src/packet/test/gtest_packet_filter.cpp index 3d458b8..f625628 100644 --- a/src/duplicated_packet_filter/test/gtest_duplicated_packet_filter.cpp +++ b/src/packet/test/gtest_packet_filter.cpp @@ -3,7 +3,7 @@ #include "tuple.h" #include "packet_def.h" #include "packet_parse.h" -#include "duplicated_packet_filter.h" +#include "packet_filter.h" /****************************************************************************** * [Protocols in frame: eth:ethertype:ip:ipv6:tcp] @@ -77,17 +77,17 @@ TEST(DUPLICATED_PACKET_FILTER, TEST) memset(&pkt, 0, sizeof(pkt)); packet_parse(&pkt, (const char *)data, sizeof(data)); - struct duplicated_packet_filter *filter = duplicated_packet_filter_new(capacity, timeout, error_rate, 1); + struct packet_filter *filter = packet_filter_new(capacity, timeout, error_rate, 1); EXPECT_TRUE(filter != nullptr); - EXPECT_TRUE(duplicated_packet_filter_lookup(filter, &pkt, 1) == 0); // no found - duplicated_packet_filter_add(filter, &pkt, 1); // add - EXPECT_TRUE(duplicated_packet_filter_lookup(filter, &pkt, 1) == 1); // found - EXPECT_TRUE(duplicated_packet_filter_lookup(filter, &pkt, 2) == 1); // found - EXPECT_TRUE(duplicated_packet_filter_lookup(filter, &pkt, 3) == 0); // not found - EXPECT_TRUE(duplicated_packet_filter_lookup(filter, &pkt, 4) == 0); // not found + 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 - duplicated_packet_filter_free(filter); + packet_filter_free(filter); } int main(int argc, char **argv) diff --git a/src/session/CMakeLists.txt b/src/session/CMakeLists.txt index d9dcc18..3f0f5db 100644 --- a/src/session/CMakeLists.txt +++ b/src/session/CMakeLists.txt @@ -11,6 +11,6 @@ target_include_directories(session_manager PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/stellar) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/include) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/utils) -target_link_libraries(session_manager timeout snowflake duplicated_packet_filter dablooms log tcp_reassembly) +target_link_libraries(session_manager timeout snowflake packet dablooms log tcp_reassembly) add_subdirectory(test) \ No newline at end of file diff --git a/src/session/session_manager.cpp b/src/session/session_manager.cpp index 23fdfb8..ba28165 100644 --- a/src/session/session_manager.cpp +++ b/src/session/session_manager.cpp @@ -8,6 +8,7 @@ #include "udp_utils.h" #include "packet_layer.h" #include "packet_utils.h" +#include "packet_filter.h" #include "snowflake.h" #include "session_def.h" #include "session_utils.h" @@ -17,7 +18,6 @@ #include "session_filter.h" #include "session_manager.h" #include "session_transition.h" -#include "duplicated_packet_filter.h" #define SESSION_LOG_ERROR(format, ...) LOG_ERROR("session", format, ##__VA_ARGS__) #define SESSION_LOG_DEBUG(format, ...) LOG_DEBUG("session", format, ##__VA_ARGS__) @@ -30,7 +30,7 @@ struct session_manager struct session_table *tcp_sess_table; struct session_table *udp_sess_table; - struct duplicated_packet_filter *dup_pkt_filter; + struct packet_filter *dup_pkt_filter; struct session_filter *evicte_sess_filter; struct session_manager_stat stat; @@ -499,7 +499,7 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session enum flow_direction dir = identify_direction_by_history(sess, key); if (session_get_stat(sess, dir, STAT_RAW_PACKETS_RECEIVED) < 3 || session_has_duplicate_traffic(sess)) { - if (duplicated_packet_filter_lookup(mgr->dup_pkt_filter, pkt, mgr->now_ms)) + if (packet_filter_lookup(mgr->dup_pkt_filter, pkt, mgr->now_ms)) { session_inc_stat(sess, dir, STAT_DUPLICATE_PACKETS_BYPASS, 1); session_inc_stat(sess, dir, STAT_DUPLICATE_BYTES_BYPASS, packet_get_raw_len(pkt)); @@ -523,7 +523,7 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session } else { - duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); + packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); return 0; } } @@ -729,7 +729,7 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m if (mgr->opts.duplicated_packet_filter_enable) { - duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); + packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); } SESS_MGR_STAT_INC(&mgr->stat, next_state, tcp); @@ -918,9 +918,9 @@ struct session_manager *session_manager_new(struct session_manager_options *opts } if (mgr->opts.duplicated_packet_filter_enable) { - mgr->dup_pkt_filter = duplicated_packet_filter_new(mgr->opts.duplicated_packet_filter_capacity, - mgr->opts.duplicated_packet_filter_timeout, - mgr->opts.duplicated_packet_filter_error_rate, now_ms); + mgr->dup_pkt_filter = packet_filter_new(mgr->opts.duplicated_packet_filter_capacity, + mgr->opts.duplicated_packet_filter_timeout, + mgr->opts.duplicated_packet_filter_error_rate, now_ms); if (mgr->dup_pkt_filter == NULL) { goto error; @@ -966,7 +966,7 @@ void session_manager_free(struct session_manager *mgr) } if (mgr->opts.duplicated_packet_filter_enable) { - duplicated_packet_filter_free(mgr->dup_pkt_filter); + packet_filter_free(mgr->dup_pkt_filter); } session_timer_free(mgr->sess_timer); session_table_free(mgr->udp_sess_table); @@ -981,7 +981,7 @@ void session_manager_record_duplicated_packet(struct session_manager *mgr, const { if (mgr->opts.duplicated_packet_filter_enable) { - duplicated_packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); + packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); } }