refactor: move evicted_session_filter to session dir

This commit is contained in:
luwenpeng
2024-08-16 11:18:16 +08:00
parent f4d437d58b
commit da891dffa7
9 changed files with 52 additions and 66 deletions

View File

@@ -1,5 +0,0 @@
add_library(evicted_session_filter evicted_session_filter.cpp)
target_include_directories(evicted_session_filter PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(evicted_session_filter packet dablooms)
add_subdirectory(test)

View File

@@ -1,18 +0,0 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
struct evicted_session_filter *evicted_session_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now);
void evicted_session_filter_free(struct evicted_session_filter *filter);
// return 1: found
// reutrn 0: no found
int evicted_session_filter_lookup(struct evicted_session_filter *filter, const struct tuple6 *key, uint64_t now);
void evicted_session_filter_add(struct evicted_session_filter *filter, const struct tuple6 *key, uint64_t now);
#ifdef __cplusplus
}
#endif

View File

@@ -1,5 +0,0 @@
add_executable(gtest_evicted_session_filter gtest_evicted_session_filter.cpp)
target_link_libraries(gtest_evicted_session_filter evicted_session_filter gtest)
include(GoogleTest)
gtest_discover_tests(gtest_evicted_session_filter)

View File

@@ -3,6 +3,7 @@ add_library(session_manager
session_pool.cpp session_pool.cpp
session_table.cpp session_table.cpp
session_timer.cpp session_timer.cpp
session_filter.cpp
session_manager.cpp session_manager.cpp
session_transition.cpp session_transition.cpp
) )
@@ -10,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}/src/stellar)
target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/include) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/utils) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/utils)
target_link_libraries(session_manager timeout snowflake duplicated_packet_filter evicted_session_filter log tcp_reassembly) target_link_libraries(session_manager timeout snowflake duplicated_packet_filter dablooms log tcp_reassembly)
add_subdirectory(test) add_subdirectory(test)

View File

@@ -1,24 +1,15 @@
#include <string.h>
#include "tuple.h" #include "tuple.h"
#include "dablooms.h" #include "dablooms.h"
#include "udp_utils.h" #include "session_filter.h"
#include "ip4_utils.h"
#include "ip6_utils.h"
#include "evicted_session_filter.h"
struct evicted_session_filter struct session_filter
{ {
struct expiry_dablooms_handle *dablooms; struct expiry_dablooms_handle *dablooms;
}; };
/****************************************************************************** struct session_filter *session_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now)
* Public API
******************************************************************************/
struct evicted_session_filter *evicted_session_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now)
{ {
struct evicted_session_filter *filter = (struct evicted_session_filter *)calloc(1, sizeof(struct evicted_session_filter)); struct session_filter *filter = (struct session_filter *)calloc(1, sizeof(struct session_filter));
if (filter == NULL) if (filter == NULL)
{ {
return NULL; return NULL;
@@ -34,7 +25,7 @@ struct evicted_session_filter *evicted_session_filter_new(uint32_t capacity, uin
return filter; return filter;
} }
void evicted_session_filter_free(struct evicted_session_filter *filter) void session_filter_free(struct session_filter *filter)
{ {
if (filter) if (filter)
{ {
@@ -50,7 +41,7 @@ void evicted_session_filter_free(struct evicted_session_filter *filter)
// return 1: found // return 1: found
// reutrn 0: no found // reutrn 0: no found
int evicted_session_filter_lookup(struct evicted_session_filter *filter, const struct tuple6 *key, uint64_t now) int session_filter_lookup(struct session_filter *filter, const struct tuple6 *key, uint64_t now)
{ {
if (expiry_dablooms_search(filter->dablooms, (const char *)key, sizeof(struct tuple6), now) == 1) if (expiry_dablooms_search(filter->dablooms, (const char *)key, sizeof(struct tuple6), now) == 1)
{ {
@@ -60,7 +51,7 @@ int evicted_session_filter_lookup(struct evicted_session_filter *filter, const s
return 0; return 0;
} }
void evicted_session_filter_add(struct evicted_session_filter *filter, const struct tuple6 *key, uint64_t now) void session_filter_add(struct session_filter *filter, const struct tuple6 *key, uint64_t now)
{ {
struct tuple6 reverse_key; struct tuple6 reverse_key;
tuple6_reverse(key, &reverse_key); tuple6_reverse(key, &reverse_key);

View File

@@ -0,0 +1,18 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
struct session_filter *session_filter_new(uint32_t capacity, uint32_t timeout, double error_rate, uint64_t now);
void session_filter_free(struct session_filter *filter);
// return 1: found
// reutrn 0: no found
int session_filter_lookup(struct session_filter *filter, const struct tuple6 *key, uint64_t now);
void session_filter_add(struct session_filter *filter, const struct tuple6 *key, uint64_t now);
#ifdef __cplusplus
}
#endif

View File

@@ -14,9 +14,9 @@
#include "session_pool.h" #include "session_pool.h"
#include "session_table.h" #include "session_table.h"
#include "session_timer.h" #include "session_timer.h"
#include "session_filter.h"
#include "session_manager.h" #include "session_manager.h"
#include "session_transition.h" #include "session_transition.h"
#include "evicted_session_filter.h"
#include "duplicated_packet_filter.h" #include "duplicated_packet_filter.h"
#define SESSION_LOG_ERROR(format, ...) LOG_ERROR("session", format, ##__VA_ARGS__) #define SESSION_LOG_ERROR(format, ...) LOG_ERROR("session", format, ##__VA_ARGS__)
@@ -31,7 +31,7 @@ struct session_manager
struct session_table *udp_sess_table; struct session_table *udp_sess_table;
struct duplicated_packet_filter *dup_pkt_filter; struct duplicated_packet_filter *dup_pkt_filter;
struct evicted_session_filter *evicte_sess_filter; struct session_filter *evicte_sess_filter;
struct session_manager_stat stat; struct session_manager_stat stat;
struct session_manager_options opts; struct session_manager_options opts;
@@ -479,7 +479,7 @@ static int udp_overload_bypass(struct session_manager *mgr, const struct tuple6
static int evicted_session_bypass(struct session_manager *mgr, const struct tuple6 *key) static int evicted_session_bypass(struct session_manager *mgr, const struct tuple6 *key)
{ {
if (mgr->opts.evicted_session_filter_enable && evicted_session_filter_lookup(mgr->evicte_sess_filter, key, mgr->now_ms)) if (mgr->opts.evicted_session_filter_enable && session_filter_lookup(mgr->evicte_sess_filter, key, mgr->now_ms))
{ {
mgr->stat.udp_pkts_evctd_bypass++; mgr->stat.udp_pkts_evctd_bypass++;
return 1; return 1;
@@ -639,7 +639,7 @@ static void session_manager_evicte_session(struct session_manager *mgr, struct s
session_table_del(mgr->udp_sess_table, sess); session_table_del(mgr->udp_sess_table, sess);
if (mgr->opts.evicted_session_filter_enable) if (mgr->opts.evicted_session_filter_enable)
{ {
evicted_session_filter_add(mgr->evicte_sess_filter, session_get_tuple6(sess), mgr->now_ms); session_filter_add(mgr->evicte_sess_filter, session_get_tuple6(sess), mgr->now_ms);
} }
SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp); SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp);
mgr->stat.udp_sess_evicted++; mgr->stat.udp_sess_evicted++;
@@ -908,9 +908,9 @@ struct session_manager *session_manager_new(struct session_manager_options *opts
} }
if (mgr->opts.evicted_session_filter_enable) if (mgr->opts.evicted_session_filter_enable)
{ {
mgr->evicte_sess_filter = evicted_session_filter_new(mgr->opts.evicted_session_filter_capacity, mgr->evicte_sess_filter = session_filter_new(mgr->opts.evicted_session_filter_capacity,
mgr->opts.evicted_session_filter_timeout, mgr->opts.evicted_session_filter_timeout,
mgr->opts.evicted_session_filter_error_rate, now_ms); mgr->opts.evicted_session_filter_error_rate, now_ms);
if (mgr->evicte_sess_filter == NULL) if (mgr->evicte_sess_filter == NULL)
{ {
goto error; goto error;
@@ -962,7 +962,7 @@ void session_manager_free(struct session_manager *mgr)
} }
if (mgr->opts.evicted_session_filter_enable) if (mgr->opts.evicted_session_filter_enable)
{ {
evicted_session_filter_free(mgr->evicte_sess_filter); session_filter_free(mgr->evicte_sess_filter);
} }
if (mgr->opts.duplicated_packet_filter_enable) if (mgr->opts.duplicated_packet_filter_enable)
{ {

View File

@@ -14,6 +14,9 @@ target_link_libraries(gtest_session_table session_manager gtest)
add_executable(gtest_session_timer gtest_session_timer.cpp) add_executable(gtest_session_timer gtest_session_timer.cpp)
target_link_libraries(gtest_session_timer session_manager gtest) target_link_libraries(gtest_session_timer session_manager gtest)
add_executable(gtest_session_filter gtest_session_filter.cpp)
target_link_libraries(gtest_session_filter session_manager gtest)
############################################################################### ###############################################################################
# gtest state machine (TCP) # gtest state machine (TCP)
############################################################################### ###############################################################################
@@ -110,6 +113,7 @@ gtest_discover_tests(gtest_session)
gtest_discover_tests(gtest_session_pool) gtest_discover_tests(gtest_session_pool)
gtest_discover_tests(gtest_session_table) gtest_discover_tests(gtest_session_table)
gtest_discover_tests(gtest_session_timer) gtest_discover_tests(gtest_session_timer)
gtest_discover_tests(gtest_session_filter)
gtest_discover_tests(gtest_state_tcp_init_to_opening) gtest_discover_tests(gtest_state_tcp_init_to_opening)
gtest_discover_tests(gtest_state_tcp_opening_to_active) gtest_discover_tests(gtest_state_tcp_opening_to_active)

View File

@@ -1,9 +1,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "tuple.h" #include "tuple.h"
#include "evicted_session_filter.h" #include "session_filter.h"
TEST(EVICTED_SESSION_FILTER, TEST) TEST(SESSION_FILTER, TEST)
{ {
struct tuple6 c2s_key; struct tuple6 c2s_key;
struct tuple6 s2c_key; struct tuple6 s2c_key;
@@ -30,20 +30,20 @@ TEST(EVICTED_SESSION_FILTER, TEST)
s2c_key.ip_proto = 0x05; s2c_key.ip_proto = 0x05;
s2c_key.domain = 0x0606060606060606; s2c_key.domain = 0x0606060606060606;
struct evicted_session_filter *filter = evicted_session_filter_new(capacity, timeout, error_rate, 1); struct session_filter *filter = session_filter_new(capacity, timeout, error_rate, 1);
EXPECT_TRUE(filter != nullptr); EXPECT_TRUE(filter != nullptr);
EXPECT_TRUE(evicted_session_filter_lookup(filter, &c2s_key, 1) == 0); // no found EXPECT_TRUE(session_filter_lookup(filter, &c2s_key, 1) == 0); // no found
EXPECT_TRUE(evicted_session_filter_lookup(filter, &s2c_key, 1) == 0); // no found EXPECT_TRUE(session_filter_lookup(filter, &s2c_key, 1) == 0); // no found
evicted_session_filter_add(filter, &c2s_key, 1); // add session_filter_add(filter, &c2s_key, 1); // add
EXPECT_TRUE(evicted_session_filter_lookup(filter, &c2s_key, 1) == 1); // found EXPECT_TRUE(session_filter_lookup(filter, &c2s_key, 1) == 1); // found
EXPECT_TRUE(evicted_session_filter_lookup(filter, &s2c_key, 1) == 1); // found EXPECT_TRUE(session_filter_lookup(filter, &s2c_key, 1) == 1); // found
EXPECT_TRUE(evicted_session_filter_lookup(filter, &c2s_key, 2) == 1); // found EXPECT_TRUE(session_filter_lookup(filter, &c2s_key, 2) == 1); // found
EXPECT_TRUE(evicted_session_filter_lookup(filter, &s2c_key, 2) == 1); // found EXPECT_TRUE(session_filter_lookup(filter, &s2c_key, 2) == 1); // found
EXPECT_TRUE(evicted_session_filter_lookup(filter, &c2s_key, 3) == 0); // not found EXPECT_TRUE(session_filter_lookup(filter, &c2s_key, 3) == 0); // not found
EXPECT_TRUE(evicted_session_filter_lookup(filter, &s2c_key, 3) == 0); // not found EXPECT_TRUE(session_filter_lookup(filter, &s2c_key, 3) == 0); // not found
evicted_session_filter_free(filter); session_filter_free(filter);
} }
int main(int argc, char **argv) int main(int argc, char **argv)