diff --git a/conf/stellar.toml b/conf/stellar.toml index 0325d03..ef9cb0c 100644 --- a/conf/stellar.toml +++ b/conf/stellar.toml @@ -10,7 +10,7 @@ dev_symbol = "nf_0_fw" dumpfile_path = "/tmp/dumpfile/dumpfile.pcap" #dumpfile_path = "/tmp/dumpfile/dumpfilelist" -nr_threads = 1 # [1, 256] +nr_worker_thread = 1 # [1, 256] cpu_mask = [5, 6, 7, 8, 9, 10, 11, 12] [ip_reassembly] @@ -29,40 +29,39 @@ tcp_overload_evict_old_sess = 1 # 1: evict old session, 0: bypass new session udp_overload_evict_old_sess = 1 # 1: evict old session, 0: bypass new session # TCP timeout -tcp_init_timeout = 5000 # range: [1, 60000] (ms) -tcp_handshake_timeout = 5000 # range: [1, 60000] (ms) -tcp_data_timeout = 5000 # range: [1, 15999999000] (ms) -tcp_half_closed_timeout = 5000 # range: [1, 604800000] (ms) -tcp_time_wait_timeout = 5000 # range: [1, 600000] (ms) -tcp_discard_timeout = 10000 # range: [1, 15999999000] (ms) -tcp_unverified_rst_timeout = 5000 # range: [1, 600000] (ms) +tcp_init_timeout_ms = 5000 # range: [1, 60000] (ms) +tcp_handshake_timeout_ms = 5000 # range: [1, 60000] (ms) +tcp_data_timeout_ms = 5000 # range: [1, 15999999000] (ms) +tcp_half_closed_timeout_ms = 5000 # range: [1, 604800000] (ms) +tcp_time_wait_timeout_ms = 5000 # range: [1, 600000] (ms) +tcp_discard_timeout_ms = 10000 # range: [1, 15999999000] (ms) +tcp_unverified_rst_timeout_ms = 5000 # range: [1, 600000] (ms) # UDP timeout -udp_data_timeout = 5000 # range: [1, 15999999000] (ms) -udp_discard_timeout = 5000 # range: [1, 15999999000] (ms) +udp_data_timeout_ms = 5000 # range: [1, 15999999000] (ms) +udp_discard_timeout_ms = 5000 # range: [1, 15999999000] (ms) + +# limit +session_expire_polling_interval_ms = 0 # range: [0, 60000] (ms) +session_expire_polling_limit = 1024 # range: [1, 1024] # duplicate packet filter duplicated_packet_filter_enable = 1 duplicated_packet_filter_capacity = 1000000 # range: [1, 4294967295] -duplicated_packet_filter_timeout = 10000 # range: [1, 60000] (ms) +duplicated_packet_filter_timeout_ms = 10000 # range: [1, 60000] (ms) duplicated_packet_filter_error_rate = 0.00001 # range: [0.0, 1.0] # evicted session filter evicted_session_filter_enable = 1 evicted_session_filter_capacity = 1000000 # range: [1, 4294967295] -evicted_session_filter_timeout = 10000 # range: [1, 60000] (ms) +evicted_session_filter_timeout_ms = 10000 # range: [1, 60000] (ms) evicted_session_filter_error_rate = 0.00001 # range: [0.0, 1.0] # TCP reassembly (Per direction) tcp_reassembly_enable = 1 -tcp_reassembly_max_timeout = 10000 # range: [1, 60000] (ms) -tcp_reassembly_max_segments = 256 # range: [2, 4096] +tcp_reassembly_max_timeout_ms = 10000 # range: [1, 60000] (ms) +tcp_reassembly_max_segments = 256 # range: [2, 4096] [schedule] -# Note: free_expired_session_interval determines the precision of session_manager timeout -free_expired_session_interval = 50 # range: [1, 60000] (ms) -free_expired_session_batch = 1000 # range: [1, 60000] -force_session_expire_before_exit = 0 # 1: force session to expire before exit, 0: wait for session to naturally expire before exit. - # Note: free_expired_ip_frag_interval determines the precision of ip_reassembly timeout free_expired_ip_frag_interval = 50 # range: [1, 60000] (ms) free_expired_ip_frag_batch = 1000 # range: [1, 60000] diff --git a/infra/core/stellar_config.c b/infra/core/stellar_config.c index da0c2d5..47bded2 100644 --- a/infra/core/stellar_config.c +++ b/infra/core/stellar_config.c @@ -115,18 +115,18 @@ static int parse_packet_io_section(toml_table_t *root, struct packet_io_options strcpy(opts->dev_symbol, ptr_dev_symbol); } - ptr = toml_raw_in(table, "nr_threads"); + ptr = toml_raw_in(table, "nr_worker_thread"); if (ptr == NULL) { - CONFIG_LOG_ERROR("config file missing packet_io->nr_threads"); + CONFIG_LOG_ERROR("config file missing packet_io->nr_worker_thread"); goto error_out; } if (atoi(ptr) <= 0 || atoi(ptr) > MAX_THREAD_NUM) { - CONFIG_LOG_ERROR("config file invalid packet_io->nr_threads %d, range [1, %d]", atoi(ptr), MAX_THREAD_NUM); + CONFIG_LOG_ERROR("config file invalid packet_io->nr_worker_thread %d, range [1, %d]", atoi(ptr), MAX_THREAD_NUM); goto error_out; } - opts->nr_threads = atoi(ptr); + opts->nr_worker_thread = atoi(ptr); mask_array = toml_array_in(table, "cpu_mask"); if (mask_array == NULL) @@ -134,7 +134,7 @@ static int parse_packet_io_section(toml_table_t *root, struct packet_io_options CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask"); goto error_out; } - for (uint16_t i = 0; i < opts->nr_threads; i++) + for (uint16_t i = 0; i < opts->nr_worker_thread; i++) { ptr = toml_raw_at(mask_array, i); if (ptr == NULL) @@ -217,222 +217,6 @@ static int parse_ip_reassembly_section(toml_table_t *root, struct ip_reassembly_ return 0; } -// return 0: success -// retuun -1: failed -static int parse_session_manager_section(toml_table_t *root, struct session_manager_options *opts) -{ - const char *ptr; - toml_table_t *table; - - table = toml_table_in(root, "session_manager"); - if (table == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager section"); - return -1; - } - - // max session number - ptr = toml_raw_in(table, "max_tcp_session_num"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->max_tcp_session_num"); - return -1; - } - opts->max_tcp_session_num = atoll(ptr); - - ptr = toml_raw_in(table, "max_udp_session_num"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->max_udp_session_num"); - return -1; - } - opts->max_udp_session_num = atoll(ptr); - - // session overload (1: evict old session, 0: bypass new session) - ptr = toml_raw_in(table, "tcp_overload_evict_old_sess"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_overload_evict_old_sess"); - return -1; - } - opts->tcp_overload_evict_old_sess = atoi(ptr); - - ptr = toml_raw_in(table, "udp_overload_evict_old_sess"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->udp_overload_evict_old_sess"); - return -1; - } - opts->udp_overload_evict_old_sess = atoi(ptr); - - // TCP timeout - ptr = toml_raw_in(table, "tcp_init_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_init_timeout"); - return -1; - } - opts->tcp_init_timeout = atoll(ptr); - - ptr = toml_raw_in(table, "tcp_handshake_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_handshake_timeout"); - return -1; - } - opts->tcp_handshake_timeout = atoll(ptr); - - ptr = toml_raw_in(table, "tcp_data_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_data_timeout"); - return -1; - } - opts->tcp_data_timeout = atoll(ptr); - - ptr = toml_raw_in(table, "tcp_half_closed_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_half_closed_timeout"); - return -1; - } - opts->tcp_half_closed_timeout = atoll(ptr); - - ptr = toml_raw_in(table, "tcp_time_wait_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_time_wait_timeout"); - return -1; - } - opts->tcp_time_wait_timeout = atoll(ptr); - - ptr = toml_raw_in(table, "tcp_discard_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_discard_timeout"); - return -1; - } - opts->tcp_discard_timeout = atoll(ptr); - - ptr = toml_raw_in(table, "tcp_unverified_rst_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_unverified_rst_timeout"); - return -1; - } - opts->tcp_unverified_rst_timeout = atoll(ptr); - - // UDP timeout - ptr = toml_raw_in(table, "udp_data_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->udp_data_timeout"); - return -1; - } - opts->udp_data_timeout = atoll(ptr); - - ptr = toml_raw_in(table, "udp_discard_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->udp_discard_timeout"); - return -1; - } - opts->udp_discard_timeout = atoll(ptr); - - // duplicate packet filter - ptr = toml_raw_in(table, "duplicated_packet_filter_enable"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->duplicated_packet_filter_enable"); - return -1; - } - opts->duplicated_packet_filter_enable = atoi(ptr); - - ptr = toml_raw_in(table, "duplicated_packet_filter_capacity"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->duplicated_packet_filter_capacity"); - return -1; - } - opts->duplicated_packet_filter_capacity = atoi(ptr); - - ptr = toml_raw_in(table, "duplicated_packet_filter_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->duplicated_packet_filter_timeout"); - return -1; - } - opts->duplicated_packet_filter_timeout = atoi(ptr); - - ptr = toml_raw_in(table, "duplicated_packet_filter_error_rate"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->duplicated_packet_filter_error_rate"); - return -1; - } - opts->duplicated_packet_filter_error_rate = atof(ptr); - - // eviction session filter - ptr = toml_raw_in(table, "evicted_session_filter_enable"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->evicted_session_filter_enable"); - return -1; - } - opts->evicted_session_filter_enable = atoi(ptr); - - ptr = toml_raw_in(table, "evicted_session_filter_capacity"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->evicted_session_filter_capacity"); - return -1; - } - opts->evicted_session_filter_capacity = atoi(ptr); - - ptr = toml_raw_in(table, "evicted_session_filter_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->evicted_session_filter_timeout"); - return -1; - } - opts->evicted_session_filter_timeout = atoi(ptr); - - ptr = toml_raw_in(table, "evicted_session_filter_error_rate"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->evicted_session_filter_error_rate"); - return -1; - } - opts->evicted_session_filter_error_rate = atof(ptr); - - // TCP reassembly - ptr = toml_raw_in(table, "tcp_reassembly_enable"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_reassembly_enable"); - return -1; - } - opts->tcp_reassembly_enable = atoi(ptr); - - ptr = toml_raw_in(table, "tcp_reassembly_max_timeout"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_reassembly_max_timeout"); - return -1; - } - opts->tcp_reassembly_max_timeout = atoi(ptr); - - ptr = toml_raw_in(table, "tcp_reassembly_max_segments"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing session_manager->tcp_reassembly_max_segments"); - return -1; - } - opts->tcp_reassembly_max_segments = atoi(ptr); - - return 0; -} - // return 0: success // retuun -1: failed static int parse_schedule_options(toml_table_t *root, struct schedule_options *opts) @@ -447,45 +231,6 @@ static int parse_schedule_options(toml_table_t *root, struct schedule_options *o return -1; } - ptr = toml_raw_in(table, "free_expired_session_interval"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing schedule->free_expired_session_interval"); - return -1; - } - opts->free_expired_session_interval = atoll(ptr); - if (opts->free_expired_session_interval < 1 || opts->free_expired_session_interval > 60000) - { - CONFIG_LOG_ERROR("config file invalid schedule->free_expired_session_interval %ld, range [1, 60000]", opts->free_expired_session_interval); - return -1; - } - - ptr = toml_raw_in(table, "free_expired_session_batch"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing schedule->free_expired_session_batch"); - return -1; - } - opts->free_expired_session_batch = atoll(ptr); - if (opts->free_expired_session_batch < 1 || opts->free_expired_session_batch > 60000) - { - CONFIG_LOG_ERROR("config file invalid schedule->free_expired_session_batch %ld, range [1, 60000]", opts->free_expired_session_batch); - return -1; - } - - ptr = toml_raw_in(table, "force_session_expire_before_exit"); - if (ptr == NULL) - { - CONFIG_LOG_ERROR("config file missing schedule->force_session_expire_before_exit"); - return -1; - } - opts->force_session_expire_before_exit = atoll(ptr); - if (opts->force_session_expire_before_exit != 0 && opts->force_session_expire_before_exit != 1) - { - CONFIG_LOG_ERROR("config file invalid schedule->force_session_expire_before_exit %ld, range [0, 1]", opts->force_session_expire_before_exit); - return -1; - } - ptr = toml_raw_in(table, "free_expired_ip_frag_interval"); if (ptr == NULL) { @@ -592,11 +337,6 @@ int stellar_config_load(struct stellar_config *config, const char *file) goto error_out; } - if (parse_session_manager_section(table, &config->sess_mgr_opts) != 0) - { - goto error_out; - } - if (parse_schedule_options(table, &config->sched_opts) != 0) { goto error_out; @@ -628,7 +368,6 @@ void stellar_config_print(const struct stellar_config *config) const struct packet_io_options *pkt_io_opts = &config->pkt_io_opts; const struct snowflake_options *snowflake_opts = &config->snowflake_opts; const struct ip_reassembly_options *ip_reass_opts = &config->ip_reass_opts; - const struct session_manager_options *sess_mgr_opts = &config->sess_mgr_opts; // snowflake config CONFIG_LOG_DEBUG("snowflake->snowflake_base : %d", snowflake_opts->snowflake_base); @@ -645,8 +384,8 @@ void stellar_config_print(const struct stellar_config *config) CONFIG_LOG_DEBUG("packet_io->app_symbol : %s", pkt_io_opts->app_symbol); CONFIG_LOG_DEBUG("packet_io->dev_symbol : %s", pkt_io_opts->dev_symbol); } - CONFIG_LOG_DEBUG("packet_io->nr_threads : %d", pkt_io_opts->nr_threads); - for (uint16_t i = 0; i < pkt_io_opts->nr_threads; i++) + CONFIG_LOG_DEBUG("packet_io->nr_worker_thread : %d", pkt_io_opts->nr_worker_thread); + for (uint16_t i = 0; i < pkt_io_opts->nr_worker_thread; i++) { CONFIG_LOG_DEBUG("packet_io->cpu_mask[%3d] : %d", i, pkt_io_opts->cpu_mask[i]); } @@ -657,45 +396,7 @@ void stellar_config_print(const struct stellar_config *config) CONFIG_LOG_DEBUG("ip_reassembly->bucket_entries : %d", ip_reass_opts->bucket_entries); CONFIG_LOG_DEBUG("ip_reassembly->bucket_num : %d", ip_reass_opts->bucket_num); - // session manager config -> max session number - CONFIG_LOG_DEBUG("session_manager->max_tcp_session_num : %ld", sess_mgr_opts->max_tcp_session_num); - CONFIG_LOG_DEBUG("session_manager->max_udp_session_num : %ld", sess_mgr_opts->max_udp_session_num); - - // session manager config -> session overload evict - CONFIG_LOG_DEBUG("session_manager->tcp_overload_evict_old_sess : %d", sess_mgr_opts->tcp_overload_evict_old_sess); - CONFIG_LOG_DEBUG("session_manager->udp_overload_evict_old_sess : %d", sess_mgr_opts->udp_overload_evict_old_sess); - - // session manager config -> session timeout - CONFIG_LOG_DEBUG("session_manager->tcp_init_timeout : %ld", sess_mgr_opts->tcp_init_timeout); - CONFIG_LOG_DEBUG("session_manager->tcp_handshake_timeout : %ld", sess_mgr_opts->tcp_handshake_timeout); - CONFIG_LOG_DEBUG("session_manager->tcp_data_timeout : %ld", sess_mgr_opts->tcp_data_timeout); - CONFIG_LOG_DEBUG("session_manager->tcp_half_closed_timeout : %ld", sess_mgr_opts->tcp_half_closed_timeout); - CONFIG_LOG_DEBUG("session_manager->tcp_time_wait_timeout : %ld", sess_mgr_opts->tcp_time_wait_timeout); - CONFIG_LOG_DEBUG("session_manager->tcp_discard_timeout : %ld", sess_mgr_opts->tcp_discard_timeout); - CONFIG_LOG_DEBUG("session_manager->tcp_unverified_rst_timeout : %ld", sess_mgr_opts->tcp_unverified_rst_timeout); - CONFIG_LOG_DEBUG("session_manager->udp_data_timeout : %ld", sess_mgr_opts->udp_data_timeout); - CONFIG_LOG_DEBUG("session_manager->udp_discard_timeout : %ld", sess_mgr_opts->udp_discard_timeout); - - // session manager config -> duplicated packet filter - CONFIG_LOG_DEBUG("session_manager->duplicated_packet_filter_enable : %d", sess_mgr_opts->duplicated_packet_filter_enable); - CONFIG_LOG_DEBUG("session_manager->duplicated_packet_filter_capacity : %d", sess_mgr_opts->duplicated_packet_filter_capacity); - CONFIG_LOG_DEBUG("session_manager->duplicated_packet_filter_timeout : %d", sess_mgr_opts->duplicated_packet_filter_timeout); - CONFIG_LOG_DEBUG("session_manager->duplicated_packet_filter_error_rate : %f", sess_mgr_opts->duplicated_packet_filter_error_rate); - - // session manager config -> evicted session filter - CONFIG_LOG_DEBUG("session_manager->evicted_session_filter_enable : %d", sess_mgr_opts->evicted_session_filter_enable); - CONFIG_LOG_DEBUG("session_manager->evicted_session_filter_capacity : %d", sess_mgr_opts->evicted_session_filter_capacity); - CONFIG_LOG_DEBUG("session_manager->evicted_session_filter_timeout : %d", sess_mgr_opts->evicted_session_filter_timeout); - CONFIG_LOG_DEBUG("session_manager->evicted_session_filter_error_rate : %f", sess_mgr_opts->evicted_session_filter_error_rate); - - // session manager config -> TCP reassembly - CONFIG_LOG_DEBUG("session_manager->tcp_reassembly_enable : %d", sess_mgr_opts->tcp_reassembly_enable); - CONFIG_LOG_DEBUG("session_manager->tcp_reassembly_max_timeout : %d", sess_mgr_opts->tcp_reassembly_max_timeout); - CONFIG_LOG_DEBUG("session_manager->tcp_reassembly_max_segments : %d", sess_mgr_opts->tcp_reassembly_max_segments); - // schedule config - CONFIG_LOG_DEBUG("schedule->free_expired_session_interval : %ld", config->sched_opts.free_expired_session_interval); - CONFIG_LOG_DEBUG("schedule->free_expired_session_batch : %ld", config->sched_opts.free_expired_session_batch); CONFIG_LOG_DEBUG("schedule->free_expired_ip_frag_interval : %ld", config->sched_opts.free_expired_ip_frag_interval); CONFIG_LOG_DEBUG("schedule->free_expired_ip_frag_batch : %ld", config->sched_opts.free_expired_ip_frag_batch); CONFIG_LOG_DEBUG("schedule->merge_stat_interval : %ld", config->sched_opts.merge_stat_interval); diff --git a/infra/core/stellar_config.h b/infra/core/stellar_config.h index 8086331..c91cd46 100644 --- a/infra/core/stellar_config.h +++ b/infra/core/stellar_config.h @@ -7,15 +7,9 @@ extern "C" #include "packet_io.h" #include "ip_reassembly.h" -#include "session_manager.h" struct schedule_options { - // Note: free_expired_session_interval determines the precision of session_manager timeout - uint64_t free_expired_session_interval; // range: [1, 60000] (ms) - uint64_t free_expired_session_batch; // range: [1, 60000] - uint64_t force_session_expire_before_exit; // range: [0, 1] - // Note: free_expired_ip_frag_interval determines the precision of ip_reassembly timeout uint64_t free_expired_ip_frag_interval; // range: [1, 60000] (ms) uint64_t free_expired_ip_frag_batch; // range: [1, 60000] @@ -37,7 +31,6 @@ struct stellar_config struct packet_io_options pkt_io_opts; struct snowflake_options snowflake_opts; struct ip_reassembly_options ip_reass_opts; - struct session_manager_options sess_mgr_opts; struct schedule_options sched_opts; }; diff --git a/infra/core/stellar_core.c b/infra/core/stellar_core.c index 12d3639..e43abc4 100644 --- a/infra/core/stellar_core.c +++ b/infra/core/stellar_core.c @@ -42,7 +42,6 @@ struct stellar_thread pthread_t tid; uint16_t idx; uint64_t is_runing; - uint64_t last_free_expired_session_timestamp; uint64_t last_free_expired_ip_frag_timestamp; uint64_t last_merge_thread_stat_timestamp; struct snowflake *snowflake; @@ -60,6 +59,7 @@ struct stellar_runtime struct packet_io *packet_io; struct plugin_manager_schema *plug_mgr; struct stellar_thread threads[MAX_THREAD_NUM]; + struct session_manager_config *sess_mgr_cfg; }; struct stellar @@ -83,101 +83,45 @@ uint64_t stellar_generate_session_id(uint64_t now_sec); static void update_session_stat(struct session *sess, struct packet *pkt) { - if (sess) + enum flow_direction dir = session_get_current_flow_direction(sess); + assert(dir != FLOW_DIRECTION_NONE); + int is_ctrl = packet_is_ctrl(pkt); + uint16_t len = packet_get_raw_len(pkt); + switch (packet_get_action(pkt)) { - enum flow_direction dir = session_get_current_flow_direction(sess); - assert(dir != FLOW_DIRECTION_NONE); - int is_ctrl = packet_is_ctrl(pkt); - uint16_t len = packet_get_raw_len(pkt); - switch (packet_get_action(pkt)) - { - case PACKET_ACTION_DROP: - session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_DROPPED : STAT_RAW_PACKETS_DROPPED), 1); - session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_DROPPED : STAT_RAW_BYTES_DROPPED), len); - break; - case PACKET_ACTION_FORWARD: - session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_TRANSMITTED : STAT_RAW_PACKETS_TRANSMITTED), 1); - session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_TRANSMITTED : STAT_RAW_BYTES_TRANSMITTED), len); - break; - default: - assert(0); - break; - } - - session_set_current_packet(sess, NULL); - session_set_current_flow_direction(sess, FLOW_DIRECTION_NONE); + case PACKET_ACTION_DROP: + session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_DROPPED : STAT_RAW_PACKETS_DROPPED), 1); + session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_DROPPED : STAT_RAW_BYTES_DROPPED), len); + break; + case PACKET_ACTION_FORWARD: + session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_PACKETS_TRANSMITTED : STAT_RAW_PACKETS_TRANSMITTED), 1); + session_inc_stat(sess, dir, (is_ctrl ? STAT_CONTROL_BYTES_TRANSMITTED : STAT_RAW_BYTES_TRANSMITTED), len); + break; + default: + assert(0); + break; } } -static inline void free_evicted_sessions(struct session_manager *sess_mgr, uint64_t max_free) +static inline void clean_session(struct session_manager *sess_mgr, uint64_t now_ms) { - void *plugin_ctx = NULL; + struct plugin_manager_runtime *plugin_ctx = NULL; struct session *sess = NULL; - for (uint64_t i = 0; i < max_free; i++) + struct session *cleaned_sess[RX_BURST_MAX * 16]; + uint64_t nr_sess_cleaned = session_manager_clean_session(sess_mgr, now_ms, cleaned_sess, sizeof(cleaned_sess) / sizeof(cleaned_sess[0])); + for (uint64_t j = 0; j < nr_sess_cleaned; j++) { - sess = session_manager_get_evicted_session(sess_mgr); - if (sess) - { - plugin_ctx = session_get_user_data(sess); - plugin_manager_on_session_closing(sess); - plugin_manager_session_runtime_free((struct plugin_manager_runtime *)plugin_ctx); - session_manager_free_session(sess_mgr, sess); - } - else - { - break; - } + sess = cleaned_sess[j]; + plugin_ctx = (struct plugin_manager_runtime *)session_get_user_data(sess); + plugin_manager_on_session_closing(sess); + plugin_manager_session_runtime_free(plugin_ctx); + session_manager_free_session(sess_mgr, sess); } } -static inline void free_expired_sessions(struct session_manager *sess_mgr, uint64_t max_free, uint64_t now_ms) -{ - void *plugin_ctx = NULL; - struct session *sess = NULL; - for (uint64_t i = 0; i < max_free; i++) - { - sess = session_manager_get_expired_session(sess_mgr, now_ms); - if (sess) - { - plugin_ctx = session_get_user_data(sess); - plugin_manager_on_session_closing(sess); - plugin_manager_session_runtime_free((struct plugin_manager_runtime *)plugin_ctx); - session_manager_free_session(sess_mgr, sess); - } - else - { - break; - } - } -} - -static inline void merge_thread_stat(struct stellar_thread *thread) -{ - struct stellar *st = thread->st; - struct stellar_runtime *runtime = &st->runtime; - struct thread_stat thr_stat = { - .packet_io = packet_io_stat(runtime->packet_io, thread->idx), - .ip_reassembly = ip_reassembly_stat(thread->ip_mgr), - .session_mgr = session_manager_stat(thread->sess_mgr), - }; - stellar_stat_merge(runtime->stat, &thr_stat, thread->idx); -} - -static inline void print_thread_stat(struct stellar_thread *thread) -{ - struct stellar *st = thread->st; - struct stellar_runtime *runtime = &st->runtime; - struct thread_stat thr_stat = { - .packet_io = packet_io_stat(runtime->packet_io, thread->idx), - .ip_reassembly = ip_reassembly_stat(thread->ip_mgr), - .session_mgr = session_manager_stat(thread->sess_mgr), - }; - stellar_stat_print(runtime->stat, &thr_stat, thread->idx); -} - static void *worker_thread(void *arg) { - int nr_recv; + int nr_pkt_received = 0; uint64_t now_ms = 0; char thd_name[16] = {0}; void *plugin_ctx = NULL; @@ -194,9 +138,12 @@ static void *worker_thread(void *arg) struct stellar_runtime *runtime = &st->runtime; struct packet_io *packet_io = runtime->packet_io; struct plugin_manager_schema *plug_mgr = runtime->plug_mgr; - uint64_t free_expired_session_interval = config->sched_opts.free_expired_session_interval; - uint64_t free_expired_session_batch = config->sched_opts.free_expired_session_batch; - uint64_t force_session_expire_before_exit = config->sched_opts.force_session_expire_before_exit; + struct thread_stat thr_stat = { + .packet_io = packet_io_stat(packet_io, thread->idx), + .ip_reassembly = ip_reassembly_stat(ip_reass), + .session_mgr = session_manager_stat(sess_mgr), + }; + uint64_t free_expired_ip_frag_interval = config->sched_opts.free_expired_ip_frag_interval; uint64_t free_expired_ip_frag_batch = config->sched_opts.free_expired_ip_frag_batch; uint64_t merge_stat_interval = config->sched_opts.merge_stat_interval; @@ -237,13 +184,13 @@ static void *worker_thread(void *arg) * Suggestion: After modifying the system time, restart the service to ensure consistent timing. */ now_ms = clock_get_real_time_ms(); - nr_recv = packet_io_input(packet_io, thr_idx, packets, RX_BURST_MAX); - if (nr_recv == 0) + nr_pkt_received = packet_io_input(packet_io, thr_idx, packets, RX_BURST_MAX); + if (nr_pkt_received == 0) { goto idle_tasks; } - for (int i = 0; i < nr_recv; i++) + for (int i = 0; i < nr_pkt_received; i++) { sess = NULL; defraged_pkt = NULL; @@ -300,11 +247,19 @@ static void *worker_thread(void *arg) plugin_manager_on_packet_output(plug_mgr, pkt); } - if (sess && session_get_current_state(sess) == SESSION_STATE_DISCARD) + if (sess) { - packet_set_action(pkt, PACKET_ACTION_DROP); + if (session_get_current_state(sess) == SESSION_STATE_DISCARD) + + { + packet_set_action(pkt, PACKET_ACTION_DROP); + } + + update_session_stat(sess, pkt); + session_set_current_packet(sess, NULL); + session_set_current_flow_direction(sess, FLOW_DIRECTION_NONE); } - update_session_stat(sess, pkt); + if (packet_get_action(pkt) == PACKET_ACTION_DROP) { if (pkt == defraged_pkt) @@ -334,21 +289,13 @@ static void *worker_thread(void *arg) } idle_tasks: - // nr_recv packet atmost trigger nr_recv session evicted - free_evicted_sessions(sess_mgr, nr_recv); + clean_session(sess_mgr, now_ms); plugin_manager_on_polling(plug_mgr); - // per free_expired_session_interval MAX free_expired_session_batch sessions are released - if (now_ms - thread->last_free_expired_session_timestamp >= free_expired_session_interval) - { - free_expired_sessions(sess_mgr, free_expired_session_batch, now_ms); - thread->last_free_expired_session_timestamp = now_ms; - } - // per merge_stat_interval merge thread stat if (now_ms - thread->last_merge_thread_stat_timestamp >= merge_stat_interval) { - merge_thread_stat(thread); + stellar_stat_merge(runtime->stat, &thr_stat, thread->idx); thread->last_merge_thread_stat_timestamp = now_ms; } @@ -359,30 +306,22 @@ static void *worker_thread(void *arg) thread->last_free_expired_ip_frag_timestamp = now_ms; } - if (nr_recv == 0) + if (nr_pkt_received == 0) { packet_io_yield(packet_io, thr_idx, packet_io_yield_interval); } } + // before exit wait all session close while (sess_stat->tcp_sess_used > 0 || sess_stat->udp_sess_used > 0) { - // force session expire - if (force_session_expire_before_exit) - { - free_expired_sessions(sess_mgr, free_expired_session_batch, UINT64_MAX); - } - // wait session expire - else - { - now_ms = clock_get_real_time_ms(); - free_expired_sessions(sess_mgr, free_expired_session_batch, now_ms); - usleep(1000); // 1ms - } + now_ms = clock_get_real_time_ms(); + clean_session(sess_mgr, now_ms); + usleep(1000); // 1ms } - merge_thread_stat(thread); - print_thread_stat(thread); + stellar_stat_merge(runtime->stat, &thr_stat, thread->idx); + stellar_stat_print(runtime->stat, &thr_stat, thread->idx); ATOMIC_SET(&thread->is_runing, 0); CORE_LOG_FATAL("worker thread %d exit", thr_idx); @@ -394,19 +333,32 @@ static void *worker_thread(void *arg) * Stellar Main Function ******************************************************************************/ +uint64_t stellar_generate_session_id(uint64_t now_sec) +{ + if (__current_thread_snowflake == NULL) + { + printf("get current thread snowflake before set\n"); + abort(); + return 0; + } + else + { + return snowflake_generate(__current_thread_snowflake, now_sec); + } +} + static int stellar_thread_init(struct stellar *st) { struct stellar_runtime *runtime = &st->runtime; struct stellar_config *config = &st->config; uint64_t now_ms = clock_get_real_time_ms(); - for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_worker_thread; i++) { struct stellar_thread *thread = &runtime->threads[i]; thread->idx = i; thread->is_runing = 0; - thread->last_free_expired_session_timestamp = now_ms; thread->last_free_expired_ip_frag_timestamp = now_ms; thread->last_merge_thread_stat_timestamp = now_ms; @@ -416,7 +368,7 @@ static int stellar_thread_init(struct stellar *st) CORE_LOG_ERROR("unable to create snowflake id generator"); return -1; } - thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now_ms); + thread->sess_mgr = session_manager_new(runtime->sess_mgr_cfg, now_ms); if (thread->sess_mgr == NULL) { CORE_LOG_ERROR("unable to create session manager"); @@ -441,7 +393,7 @@ static void stellar_thread_clean(struct stellar *st) struct stellar_config *config = &st->config; CORE_LOG_FATAL("cleaning worker thread context ..."); - for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_worker_thread; i++) { struct stellar_thread *thread = &runtime->threads[i]; if (ATOMIC_READ(&thread->is_runing) == 0) @@ -459,7 +411,7 @@ static int stellar_thread_run(struct stellar *st) struct stellar_runtime *runtime = &st->runtime; struct stellar_config *config = &st->config; - for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_worker_thread; i++) { struct stellar_thread *thread = &runtime->threads[i]; if (pthread_create(&thread->tid, NULL, worker_thread, (void *)thread) < 0) @@ -478,7 +430,7 @@ static void stellar_thread_join(struct stellar *st) struct stellar_config *config = &st->config; CORE_LOG_FATAL("waiting worker thread stop ..."); - for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_worker_thread; i++) { struct stellar_thread *thread = &runtime->threads[i]; while (ATOMIC_READ(&thread->is_runing) == 1) @@ -532,6 +484,14 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg CORE_LOG_FATAL("plugin config file : %s", st->plugin_cfg_file); CORE_LOG_FATAL("log config file : %s", st->log_cfg_file); + runtime->sess_mgr_cfg = session_manager_config_new(st->stellar_cfg_file); + if (runtime->sess_mgr_cfg == NULL) + { + CORE_LOG_ERROR("unable to create session manager config"); + goto error_out; + } + session_manager_config_print(runtime->sess_mgr_cfg); + if (stellar_config_load(config, st->stellar_cfg_file) != 0) { CORE_LOG_ERROR("unable to load config file"); @@ -539,7 +499,7 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg } stellar_config_print(config); - runtime->stat = stellar_stat_new(config->pkt_io_opts.nr_threads); + runtime->stat = stellar_stat_new(config->pkt_io_opts.nr_worker_thread); if (runtime->stat == NULL) { CORE_LOG_ERROR("unable to create stellar stat"); @@ -627,6 +587,7 @@ void stellar_free(struct stellar *st) packet_io_free(runtime->packet_io); plugin_manager_exit(runtime->plug_mgr); stellar_stat_free(runtime->stat); + session_manager_config_free(runtime->sess_mgr_cfg); CORE_LOG_FATAL("stellar exit\n"); log_free(runtime->logger); @@ -691,20 +652,6 @@ uint16_t stellar_get_current_thread_index() } } -uint64_t stellar_generate_session_id(uint64_t now_sec) -{ - if (__current_thread_snowflake == NULL) - { - printf("get current thread snowflake before set\n"); - abort(); - return 0; - } - else - { - return snowflake_generate(__current_thread_snowflake, now_sec); - } -} - // only send user crafted packet, can't send packet which come from network void stellar_send_build_packet(struct stellar *st, struct packet *pkt) { @@ -727,7 +674,7 @@ void stellar_send_build_packet(struct stellar *st, struct packet *pkt) int stellar_get_worker_thread_num(struct stellar *st) { - return st->config.pkt_io_opts.nr_threads; + return st->config.pkt_io_opts.nr_worker_thread; } struct logger *stellar_get_logger(struct stellar *st) diff --git a/infra/packet_io/dumpfile_io.c b/infra/packet_io/dumpfile_io.c index cc6faaf..ed8fb58 100644 --- a/infra/packet_io/dumpfile_io.c +++ b/infra/packet_io/dumpfile_io.c @@ -25,7 +25,7 @@ struct dumpfile_io { enum packet_io_mode mode; - uint16_t nr_threads; + uint16_t nr_worker_thread; char dumpfile_path[256]; pcap_t *pcap; @@ -152,7 +152,7 @@ static void pcap_pkt_handler(u_char *user, const struct pcap_pkthdr *h, const u_ uint64_t hash = packet_ldbc_hash(&pkt, PKT_LDBC_METH_OUTERMOST_INT_EXT_IP, PACKET_DIRECTION_OUTGOING); // push packet to queue - struct packet_queue *queue = handle->queue[hash % handle->nr_threads]; + struct packet_queue *queue = handle->queue[hash % handle->nr_worker_thread]; while (packet_queue_push(queue, pcap_pkt) == -1) { if (ATOMIC_READ(&handle->io_thread_need_exit)) @@ -199,7 +199,7 @@ static int all_packet_consumed(struct dumpfile_io *handle) { uint64_t consumed_pkts = 0; uint64_t read_pcap_pkts = ATOMIC_READ(&handle->read_pcap_pkts); - for (uint16_t i = 0; i < handle->nr_threads; i++) + for (uint16_t i = 0; i < handle->nr_worker_thread; i++) { consumed_pkts += ATOMIC_READ(&handle->stat[i].pkts_rx); } @@ -287,7 +287,7 @@ erro_out: * Public API ******************************************************************************/ -struct dumpfile_io *dumpfile_io_new(const char *dumpfile_path, enum packet_io_mode mode, uint16_t nr_threads) +struct dumpfile_io *dumpfile_io_new(const char *dumpfile_path, enum packet_io_mode mode, uint16_t nr_worker_thread) { pthread_t tid; struct dumpfile_io *handle = (struct dumpfile_io *)calloc(1, sizeof(struct dumpfile_io)); @@ -298,11 +298,11 @@ struct dumpfile_io *dumpfile_io_new(const char *dumpfile_path, enum packet_io_mo } handle->mode = mode; - handle->nr_threads = nr_threads; + handle->nr_worker_thread = nr_worker_thread; handle->logger = __thread_local_logger; strncpy(handle->dumpfile_path, dumpfile_path, MIN(strlen(dumpfile_path), sizeof(handle->dumpfile_path))); - for (uint16_t i = 0; i < handle->nr_threads; i++) + for (uint16_t i = 0; i < handle->nr_worker_thread; i++) { handle->queue[i] = packet_queue_new(MAX_PACKET_QUEUE_SIZE); if (handle->queue[i] == NULL) @@ -336,7 +336,7 @@ void dumpfile_io_free(struct dumpfile_io *handle) } struct pcap_pkt *pcap_pkt = NULL; - for (uint16_t i = 0; i < handle->nr_threads; i++) + for (uint16_t i = 0; i < handle->nr_worker_thread; i++) { while (1) { diff --git a/infra/packet_io/dumpfile_io.h b/infra/packet_io/dumpfile_io.h index 4728608..05f4ccc 100644 --- a/infra/packet_io/dumpfile_io.h +++ b/infra/packet_io/dumpfile_io.h @@ -8,7 +8,7 @@ extern "C" #include "packet_io.h" struct dumpfile_io; -struct dumpfile_io *dumpfile_io_new(const char *dumpfile_path, enum packet_io_mode mode, uint16_t nr_threads); +struct dumpfile_io *dumpfile_io_new(const char *dumpfile_path, enum packet_io_mode mode, uint16_t nr_worker_thread); void dumpfile_io_free(struct dumpfile_io *handle); int dumpfile_io_isbreak(struct dumpfile_io *handle); diff --git a/infra/packet_io/marsio_io.c b/infra/packet_io/marsio_io.c index be57462..96d39fd 100644 --- a/infra/packet_io/marsio_io.c +++ b/infra/packet_io/marsio_io.c @@ -175,12 +175,12 @@ static inline int is_keepalive_packet(const char *data, int len) * Public API ******************************************************************************/ -struct marsio_io *marsio_io_new(const char *app_symbol, const char *dev_symbol, uint16_t *cpu_mask, uint16_t nr_threads) +struct marsio_io *marsio_io_new(const char *app_symbol, const char *dev_symbol, uint16_t *cpu_mask, uint16_t nr_worker_thread) { int opt = 1; cpu_set_t coremask; CPU_ZERO(&coremask); - for (uint16_t i = 0; i < nr_threads; i++) + for (uint16_t i = 0; i < nr_worker_thread; i++) { CPU_SET(cpu_mask[i], &coremask); } @@ -205,7 +205,7 @@ struct marsio_io *marsio_io_new(const char *app_symbol, const char *dev_symbol, PACKET_IO_LOG_ERROR("unable to init marsio instance"); goto error_out; } - handle->mr_dev = marsio_open_device(handle->mr_ins, dev_symbol, nr_threads, nr_threads); + handle->mr_dev = marsio_open_device(handle->mr_ins, dev_symbol, nr_worker_thread, nr_worker_thread); if (handle->mr_dev == NULL) { PACKET_IO_LOG_ERROR("unable to open marsio device"); diff --git a/infra/packet_io/marsio_io.h b/infra/packet_io/marsio_io.h index c18976c..daccc4e 100644 --- a/infra/packet_io/marsio_io.h +++ b/infra/packet_io/marsio_io.h @@ -8,7 +8,7 @@ extern "C" #include "packet_io.h" struct marsio_io; -struct marsio_io *marsio_io_new(const char *app_symbol, const char *dev_symbol, uint16_t *cpu_mask, uint16_t nr_threads); +struct marsio_io *marsio_io_new(const char *app_symbol, const char *dev_symbol, uint16_t *cpu_mask, uint16_t nr_worker_thread); void marsio_io_free(struct marsio_io *handle); int marsio_io_init(struct marsio_io *handle, uint16_t thr_idx); diff --git a/infra/packet_io/packet_io.c b/infra/packet_io/packet_io.c index 1684adb..3bd00e4 100644 --- a/infra/packet_io/packet_io.c +++ b/infra/packet_io/packet_io.c @@ -22,11 +22,11 @@ struct packet_io *packet_io_new(struct packet_io_options *opts) packet_io->mode = opts->mode; if (opts->mode == PACKET_IO_MARSIO) { - packet_io->marsio = marsio_io_new(opts->app_symbol, opts->dev_symbol, opts->cpu_mask, opts->nr_threads); + packet_io->marsio = marsio_io_new(opts->app_symbol, opts->dev_symbol, opts->cpu_mask, opts->nr_worker_thread); } else { - packet_io->dumpfile = dumpfile_io_new(opts->dumpfile_path, packet_io->mode, opts->nr_threads); + packet_io->dumpfile = dumpfile_io_new(opts->dumpfile_path, packet_io->mode, opts->nr_worker_thread); } if (packet_io->marsio == NULL && packet_io->dumpfile == NULL) { diff --git a/infra/packet_io/packet_io.h b/infra/packet_io/packet_io.h index e48178f..733b6c3 100644 --- a/infra/packet_io/packet_io.h +++ b/infra/packet_io/packet_io.h @@ -64,7 +64,7 @@ struct packet_io_options char app_symbol[64]; char dev_symbol[64]; - uint16_t nr_threads; + uint16_t nr_worker_thread; uint16_t cpu_mask[MAX_THREAD_NUM]; }; diff --git a/infra/session_manager/session_manager.c b/infra/session_manager/session_manager.c index 1594e20..2df188a 100644 --- a/infra/session_manager/session_manager.c +++ b/infra/session_manager/session_manager.c @@ -1,8 +1,10 @@ #include #include #include +#include #include "utils.h" +#include "toml.h" #include "log_private.h" #include "packet_helper.h" #include "packet_filter.h" @@ -16,6 +18,7 @@ #define SESSION_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "session", format, ##__VA_ARGS__) #define SESSION_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "session", format, ##__VA_ARGS__) +#define SESSION_LOG_INFO(format, ...) STELLAR_LOG_INFO(__thread_local_logger, "session", format, ##__VA_ARGS__) struct session_manager { @@ -29,20 +32,21 @@ struct session_manager struct session_filter *evicte_sess_filter; struct session_manager_stat stat; - struct session_manager_options opts; + struct session_manager_config cfg; /* * only used for session_set_discard() or session_manager record_duplicated_packet(), * because the function is called by pluin and has no time input. */ uint64_t now_ms; + uint64_t last_clean_expired_sess_ts; session_id_generate_fn id_generator; }; #define EVICTE_SESSION_BURST (RX_BURST_MAX) /****************************************************************************** - * Session Manager Stat + * session manager stat macro ******************************************************************************/ #define SESS_MGR_STAT_INC(stat, state, proto) \ @@ -103,136 +107,7 @@ struct session_manager } /****************************************************************************** - * Session Manager Options - ******************************************************************************/ - -static int check_options(const struct session_manager_options *opts) -{ - if (opts == NULL) - { - SESSION_LOG_ERROR("invalid options"); - return -1; - } - - // max session number - if (opts->max_tcp_session_num < EVICTE_SESSION_BURST * 2) - { - SESSION_LOG_ERROR("invalid max_tcp_session_num: %lu, supported range: [%u, %lu]", opts->max_tcp_session_num, EVICTE_SESSION_BURST * 2, UINT64_MAX); - return -1; - } - if (opts->max_udp_session_num < EVICTE_SESSION_BURST * 2) - { - SESSION_LOG_ERROR("invalid max_udp_session_num: %lu, supported range: [%u, %lu]", opts->max_udp_session_num, EVICTE_SESSION_BURST * 2, UINT64_MAX); - return -1; - } - - // session overload (skip) - - // TCP timeout - if (opts->tcp_init_timeout < 1 || opts->tcp_init_timeout > 60000) - { - SESSION_LOG_ERROR("invalid tcp_init_timeout: %lu, supported range: [1, 60000]", opts->tcp_init_timeout); - return -1; - } - if (opts->tcp_handshake_timeout < 1 || opts->tcp_handshake_timeout > 60000) - { - SESSION_LOG_ERROR("invalid tcp_handshake_timeout: %lu, supported range: [1, 60000]", opts->tcp_handshake_timeout); - return -1; - } - if (opts->tcp_data_timeout < 1 || opts->tcp_data_timeout > 15999999000) - { - SESSION_LOG_ERROR("invalid tcp_data_timeout: %lu, supported range: [1, 15999999000]", opts->tcp_data_timeout); - return -1; - } - if (opts->tcp_half_closed_timeout < 1 || opts->tcp_half_closed_timeout > 604800000) - { - SESSION_LOG_ERROR("invalid tcp_half_closed_timeout: %lu, supported range: [1, 604800000]", opts->tcp_half_closed_timeout); - return -1; - } - if (opts->tcp_time_wait_timeout < 1 || opts->tcp_time_wait_timeout > 600000) - { - SESSION_LOG_ERROR("invalid tcp_time_wait_timeout: %lu, supported range: [1, 600000]", opts->tcp_time_wait_timeout); - return -1; - } - if (opts->tcp_discard_timeout < 1 || opts->tcp_discard_timeout > 15999999000) - { - SESSION_LOG_ERROR("invalid tcp_discard_timeout: %lu, supported range: [1, 15999999000]", opts->tcp_discard_timeout); - return -1; - } - if (opts->tcp_unverified_rst_timeout < 1 || opts->tcp_unverified_rst_timeout > 600000) - { - SESSION_LOG_ERROR("invalid tcp_unverified_rst_timeout: %lu, supported range: [1, 600000]", opts->tcp_unverified_rst_timeout); - return -1; - } - // UDP timeout - if (opts->udp_data_timeout < 1 || opts->udp_data_timeout > 15999999000) - { - SESSION_LOG_ERROR("invalid udp_data_timeout: %lu, supported range: [1, 15999999000]", opts->udp_data_timeout); - return -1; - } - - // duplicate packet filter - if (opts->duplicated_packet_filter_enable) - { - if (opts->duplicated_packet_filter_capacity == 0) - { - // UINT32_MAX = 4294967295 - SESSION_LOG_ERROR("invalid duplicated_packet_filter_capacity: %u, supported range: [1, 4294967295]", opts->duplicated_packet_filter_capacity); - return -1; - } - if (opts->duplicated_packet_filter_timeout < 1 || opts->duplicated_packet_filter_timeout > 60000) - { - SESSION_LOG_ERROR("invalid duplicated_packet_filter_timeout: %u, supported range: [1, 60000]", opts->duplicated_packet_filter_timeout); - return -1; - } - if (opts->duplicated_packet_filter_error_rate < 0.0 || opts->duplicated_packet_filter_error_rate > 1.0) - { - SESSION_LOG_ERROR("invalid duplicated_packet_filter_error_rate: %f, supported range: [0.0, 1.0]", opts->duplicated_packet_filter_error_rate); - return -1; - } - } - - // evicted session filter - if (opts->evicted_session_filter_enable) - { - if (opts->evicted_session_filter_capacity == 0) - { - // UINT32_MAX = 4294967295 - SESSION_LOG_ERROR("invalid evicted_session_filter_capacity: %u, supported range: [1, 4294967295]", opts->evicted_session_filter_capacity); - return -1; - } - if (opts->evicted_session_filter_timeout < 1 || opts->evicted_session_filter_timeout > 60000) - { - SESSION_LOG_ERROR("invalid evicted_session_filter_timeout: %u, supported range: [1, 60000]", opts->evicted_session_filter_timeout); - return -1; - } - if (opts->evicted_session_filter_error_rate < 0.0 || opts->evicted_session_filter_error_rate > 1.0) - { - SESSION_LOG_ERROR("invalid evicted_session_filter_error_rate: %f, supported range: [0.0, 1.0]", opts->evicted_session_filter_error_rate); - return -1; - } - } - - // TCP reassembly - if (opts->tcp_reassembly_enable) - { - if (opts->tcp_reassembly_max_timeout < 1 || opts->tcp_reassembly_max_timeout > 60000) - { - SESSION_LOG_ERROR("invalid tcp_reassembly_max_timeout: %u, supported range: [1, 60000]", opts->tcp_reassembly_max_timeout); - return -1; - } - if (opts->tcp_reassembly_max_segments < 2 || opts->tcp_reassembly_max_segments > 4096) - { - SESSION_LOG_ERROR("invalid tcp_reassembly_max_segments: %u, supported range: [2, 4096]", opts->tcp_reassembly_max_segments); - return -1; - } - } - - return 0; -} - -/****************************************************************************** - * TCP + * TCP utils ******************************************************************************/ static void tcp_clean(struct session_manager *mgr, struct session *sess) @@ -266,13 +141,13 @@ static void tcp_clean(struct session_manager *mgr, struct session *sess) static int tcp_init(struct session_manager *mgr, struct session *sess) { - if (!mgr->opts.tcp_reassembly_enable) + if (!mgr->cfg.tcp_reassembly_enable) { return 0; } - sess->tcp_halfs[FLOW_DIRECTION_C2S].assembler = tcp_reassembly_new(mgr->opts.tcp_reassembly_max_timeout, mgr->opts.tcp_reassembly_max_segments); - sess->tcp_halfs[FLOW_DIRECTION_S2C].assembler = tcp_reassembly_new(mgr->opts.tcp_reassembly_max_timeout, mgr->opts.tcp_reassembly_max_segments); + sess->tcp_halfs[FLOW_DIRECTION_C2S].assembler = tcp_reassembly_new(mgr->cfg.tcp_reassembly_max_timeout_ms, mgr->cfg.tcp_reassembly_max_segments); + sess->tcp_halfs[FLOW_DIRECTION_S2C].assembler = tcp_reassembly_new(mgr->cfg.tcp_reassembly_max_timeout_ms, mgr->cfg.tcp_reassembly_max_segments); if (sess->tcp_halfs[FLOW_DIRECTION_C2S].assembler == NULL || sess->tcp_halfs[FLOW_DIRECTION_S2C].assembler == NULL) { tcp_clean(mgr, sess); @@ -305,7 +180,7 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum f half->ack = tcp_hdr_get_ack(hdr); half->len = tcp_layer->pld_len; - if (!mgr->opts.tcp_reassembly_enable) + if (!mgr->cfg.tcp_reassembly_enable) { if (len) { @@ -415,7 +290,7 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum f } /****************************************************************************** - * Session Direction + * session direction identify ******************************************************************************/ static enum flow_direction identify_direction_by_port(uint16_t src_port, uint16_t dst_port) @@ -449,13 +324,13 @@ static enum flow_direction identify_direction_by_history(const struct session *s } /****************************************************************************** - * Session Filter + * session filter bypass utils ******************************************************************************/ // on new session static int tcp_overload_bypass(struct session_manager *mgr, const struct tuple6 *key) { - if (key->ip_proto == IPPROTO_TCP && mgr->stat.tcp_sess_used >= mgr->opts.max_tcp_session_num) + if (key->ip_proto == IPPROTO_TCP && mgr->stat.tcp_sess_used >= mgr->cfg.max_tcp_session_num) { mgr->stat.tcp_pkts_bypass_table_full++; return 1; @@ -465,7 +340,7 @@ static int tcp_overload_bypass(struct session_manager *mgr, const struct tuple6 static int udp_overload_bypass(struct session_manager *mgr, const struct tuple6 *key) { - if (key->ip_proto == IPPROTO_UDP && mgr->stat.udp_sess_used >= mgr->opts.max_udp_session_num) + if (key->ip_proto == IPPROTO_UDP && mgr->stat.udp_sess_used >= mgr->cfg.max_udp_session_num) { mgr->stat.udp_pkts_bypass_table_full++; return 1; @@ -475,7 +350,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) { - if (mgr->opts.evicted_session_filter_enable && session_filter_lookup(mgr->evicte_sess_filter, key, mgr->now_ms)) + if (mgr->cfg.evicted_session_filter_enable && session_filter_lookup(mgr->evicte_sess_filter, key, mgr->now_ms)) { mgr->stat.udp_pkts_bypass_session_evicted++; return 1; @@ -487,7 +362,7 @@ static int evicted_session_bypass(struct session_manager *mgr, const struct tupl // on update session static int duplicated_packet_bypass(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key) { - if (mgr->opts.duplicated_packet_filter_enable == 0) + if (mgr->cfg.duplicated_packet_filter_enable == 0) { return 0; } @@ -528,7 +403,7 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session } /****************************************************************************** - * Session Manager + * session manager utils ******************************************************************************/ static void session_update(struct session_manager *mgr, struct session *sess, enum session_state next_state, const struct packet *pkt, const struct tuple6 *key, enum flow_direction dir) @@ -642,7 +517,7 @@ static void session_manager_evicte_session(struct session_manager *mgr, struct s case SESSION_TYPE_UDP: SESSION_LOG_DEBUG("evicte udp old session: %lu", session_get_id(sess)); session_table_del(mgr->udp_sess_table, sess); - if (mgr->opts.evicted_session_filter_enable) + if (mgr->cfg.evicted_session_filter_enable) { session_filter_add(mgr->evicte_sess_filter, session_get_tuple6(sess), mgr->now_ms); } @@ -699,7 +574,7 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m } // tcp table full evict old session - if (mgr->opts.tcp_overload_evict_old_sess && mgr->stat.tcp_sess_used >= mgr->opts.max_tcp_session_num - EVICTE_SESSION_BURST) + if (mgr->cfg.tcp_overload_evict_old_sess && mgr->stat.tcp_sess_used >= mgr->cfg.max_tcp_session_num - EVICTE_SESSION_BURST) { struct session *evic_sess = session_table_find_lru(mgr->tcp_sess_table); session_manager_evicte_session(mgr, evic_sess, LRU_EVICT); @@ -728,11 +603,11 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m } tcp_update(mgr, sess, dir, tcp_layer); - uint64_t timeout = (flags & TH_ACK) ? mgr->opts.tcp_handshake_timeout : mgr->opts.tcp_init_timeout; + uint64_t timeout = (flags & TH_ACK) ? mgr->cfg.tcp_handshake_timeout_ms : mgr->cfg.tcp_init_timeout_ms; session_timer_update(mgr->sess_timer, sess, mgr->now_ms + timeout); session_table_add(mgr->tcp_sess_table, sess); - if (mgr->opts.duplicated_packet_filter_enable) + if (mgr->cfg.duplicated_packet_filter_enable) { packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); } @@ -747,7 +622,7 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m static struct session *session_manager_new_udp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key) { // udp table full evict old session - if (mgr->opts.udp_overload_evict_old_sess && mgr->stat.udp_sess_used >= mgr->opts.max_udp_session_num - EVICTE_SESSION_BURST) + if (mgr->cfg.udp_overload_evict_old_sess && mgr->stat.udp_sess_used >= mgr->cfg.max_udp_session_num - EVICTE_SESSION_BURST) { struct session *evic_sess = session_table_find_lru(mgr->udp_sess_table); session_manager_evicte_session(mgr, evic_sess, LRU_EVICT); @@ -768,7 +643,7 @@ static struct session *session_manager_new_udp_session(struct session_manager *m session_update(mgr, sess, next_state, pkt, key, dir); session_transition_log(sess, SESSION_STATE_INIT, next_state, UDP_DATA); - session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_data_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->cfg.udp_data_timeout_ms); session_table_add(mgr->udp_sess_table, sess); SESS_MGR_STAT_INC(&mgr->stat, next_state, udp); @@ -823,34 +698,34 @@ static int session_manager_update_tcp_session(struct session_manager *mgr, struc case SESSION_STATE_OPENING: if (flags & TH_SYN) { - timeout = (flags & TH_ACK) ? mgr->opts.tcp_handshake_timeout : mgr->opts.tcp_init_timeout; + timeout = (flags & TH_ACK) ? mgr->cfg.tcp_handshake_timeout_ms : mgr->cfg.tcp_init_timeout_ms; } else { - timeout = mgr->opts.tcp_data_timeout; + timeout = mgr->cfg.tcp_data_timeout_ms; } break; case SESSION_STATE_ACTIVE: - timeout = mgr->opts.tcp_data_timeout; + timeout = mgr->cfg.tcp_data_timeout_ms; break; case SESSION_STATE_CLOSING: if (flags & TH_FIN) { - timeout = (peer->history & TH_FIN) ? mgr->opts.tcp_time_wait_timeout : mgr->opts.tcp_half_closed_timeout; + timeout = (peer->history & TH_FIN) ? mgr->cfg.tcp_time_wait_timeout_ms : mgr->cfg.tcp_half_closed_timeout_ms; } else if (flags & TH_RST) { // if fin is received, the expected sequence number should be increased by 1 uint32_t expected = (peer->history & TH_FIN) ? peer->ack + 1 : peer->ack; - timeout = (expected == curr->seq) ? mgr->opts.tcp_time_wait_timeout : mgr->opts.tcp_unverified_rst_timeout; + timeout = (expected == curr->seq) ? mgr->cfg.tcp_time_wait_timeout_ms : mgr->cfg.tcp_unverified_rst_timeout_ms; } else { - timeout = mgr->opts.tcp_data_timeout; + timeout = mgr->cfg.tcp_data_timeout_ms; } break; case SESSION_STATE_DISCARD: - timeout = mgr->opts.tcp_discard_timeout; + timeout = mgr->cfg.tcp_discard_timeout_ms; break; default: assert(0); @@ -873,11 +748,11 @@ static int session_manager_update_udp_session(struct session_manager *mgr, struc if (session_get_current_state(sess) == SESSION_STATE_DISCARD) { - session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_discard_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->cfg.udp_discard_timeout_ms); } else { - session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_data_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->cfg.udp_data_timeout_ms); } SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp); @@ -885,25 +760,230 @@ static int session_manager_update_udp_session(struct session_manager *mgr, struc return 0; } +static inline uint8_t ipv4_in_range(const struct in_addr *addr, const struct in_addr *start, const struct in_addr *end) +{ + return (memcmp(addr, start, sizeof(struct in_addr)) >= 0 && memcmp(addr, end, sizeof(struct in_addr)) <= 0); +} + +static inline uint8_t ipv6_in_range(const struct in6_addr *addr, const struct in6_addr *start, const struct in6_addr *end) +{ + return (memcmp(addr, start, sizeof(struct in6_addr)) >= 0 && memcmp(addr, end, sizeof(struct in6_addr)) <= 0); +} + /****************************************************************************** - * Public API + * session manager public API ******************************************************************************/ -struct session_manager *session_manager_new(struct session_manager_options *opts, uint64_t now_ms) +#define PARSE_AND_CHECK_NUM(table, key, val, min, max) \ + do \ + { \ + const char *ptr = toml_raw_in(table, (key)); \ + if (ptr == NULL) \ + { \ + SESSION_LOG_ERROR("config file missing session_manager.%s", (key)); \ + goto error_out; \ + } \ + (val) = atoll(ptr); \ + if ((val) < (min) || (val) > (max)) \ + { \ + SESSION_LOG_ERROR("invalid session_manager.%s: %lu, supported range: [%lu, %lu]", (key), (val), (min), (max)); \ + goto error_out; \ + } \ + } while (0) + +#define PARSE_AND_CHECK_DOUBLE(table, key, val, min, max) \ + do \ + { \ + const char *ptr = toml_raw_in(table, (key)); \ + if (ptr == NULL) \ + { \ + SESSION_LOG_ERROR("config file missing session_manager.%s", (key)); \ + goto error_out; \ + } \ + (val) = atof(ptr); \ + if ((val) < (min) || (val) > (max)) \ + { \ + SESSION_LOG_ERROR("invalid session_manager.%s: %lu, supported range: [%f, %f]", (key), (val), (min), (max)); \ + goto error_out; \ + } \ + } while (0) + +int session_manager_config_load(struct session_manager_config *cfg, const char *toml_file) { - if (check_options(opts) == -1) + int ret = -1; + char errbuf[200]; + FILE *fp = NULL; + toml_table_t *root = NULL; + toml_table_t *table = NULL; + + uint64_t zero = 0; // make compiler happy + + fp = fopen(toml_file, "r"); + if (fp == NULL) + { + SESSION_LOG_ERROR("config file %s open failed, %s", toml_file, strerror(errno)); + goto error_out; + } + + root = toml_parse_file(fp, errbuf, sizeof(errbuf)); + if (root == NULL) + { + SESSION_LOG_ERROR("config file %s parse failed, %s", toml_file, errbuf); + goto error_out; + } + + table = toml_table_in(root, "session_manager"); + if (table == NULL) + { + SESSION_LOG_ERROR("config file %s missing session_manager", toml_file); + goto error_out; + } + + // max session number + PARSE_AND_CHECK_NUM(table, "max_tcp_session_num", cfg->max_tcp_session_num, EVICTE_SESSION_BURST * 2, UINT64_MAX); + PARSE_AND_CHECK_NUM(table, "max_udp_session_num", cfg->max_udp_session_num, EVICTE_SESSION_BURST * 2, UINT64_MAX); + + // session overload + PARSE_AND_CHECK_NUM(table, "tcp_overload_evict_old_sess", cfg->tcp_overload_evict_old_sess, zero, 1); + PARSE_AND_CHECK_NUM(table, "udp_overload_evict_old_sess", cfg->udp_overload_evict_old_sess, zero, 1); + + // TCP timeout + PARSE_AND_CHECK_NUM(table, "tcp_init_timeout_ms", cfg->tcp_init_timeout_ms, 1, 60000); + PARSE_AND_CHECK_NUM(table, "tcp_handshake_timeout_ms", cfg->tcp_handshake_timeout_ms, 1, 60000); + PARSE_AND_CHECK_NUM(table, "tcp_data_timeout_ms", cfg->tcp_data_timeout_ms, 1, 15999999000); + PARSE_AND_CHECK_NUM(table, "tcp_half_closed_timeout_ms", cfg->tcp_half_closed_timeout_ms, 1, 604800000); + PARSE_AND_CHECK_NUM(table, "tcp_time_wait_timeout_ms", cfg->tcp_time_wait_timeout_ms, 1, 60000); + PARSE_AND_CHECK_NUM(table, "tcp_discard_timeout_ms", cfg->tcp_discard_timeout_ms, 1, 15999999000); + PARSE_AND_CHECK_NUM(table, "tcp_unverified_rst_timeout_ms", cfg->tcp_unverified_rst_timeout_ms, 1, 60000); + + // UDP timeout + PARSE_AND_CHECK_NUM(table, "udp_data_timeout_ms", cfg->udp_data_timeout_ms, 1, 15999999000); + PARSE_AND_CHECK_NUM(table, "udp_discard_timeout_ms", cfg->udp_discard_timeout_ms, 1, 15999999000); + + // limit + PARSE_AND_CHECK_NUM(table, "session_expire_polling_interval_ms", cfg->session_expire_polling_interval_ms, zero, 60000); + PARSE_AND_CHECK_NUM(table, "session_expire_polling_limit", cfg->session_expire_polling_limit, 1, 1024); + + // duplicated packet filter + PARSE_AND_CHECK_NUM(table, "duplicated_packet_filter_enable", cfg->duplicated_packet_filter_enable, zero, 1); + PARSE_AND_CHECK_NUM(table, "duplicated_packet_filter_capacity", cfg->duplicated_packet_filter_capacity, 1, 4294967295); + PARSE_AND_CHECK_NUM(table, "duplicated_packet_filter_timeout_ms", cfg->duplicated_packet_filter_timeout_ms, 1, 60000); + PARSE_AND_CHECK_DOUBLE(table, "duplicated_packet_filter_error_rate", cfg->duplicated_packet_filter_error_rate, 0.0, 1.0); + + // eviction session filter + PARSE_AND_CHECK_NUM(table, "evicted_session_filter_enable", cfg->evicted_session_filter_enable, zero, 1); + PARSE_AND_CHECK_NUM(table, "evicted_session_filter_capacity", cfg->evicted_session_filter_capacity, 1, 4294967295); + PARSE_AND_CHECK_NUM(table, "evicted_session_filter_timeout_ms", cfg->evicted_session_filter_timeout_ms, 1, 60000); + PARSE_AND_CHECK_DOUBLE(table, "evicted_session_filter_error_rate", cfg->evicted_session_filter_error_rate, 0.0, 1.0); + + // TCP reassembly + PARSE_AND_CHECK_NUM(table, "tcp_reassembly_enable", cfg->tcp_reassembly_enable, zero, 1); + PARSE_AND_CHECK_NUM(table, "tcp_reassembly_max_timeout_ms", cfg->tcp_reassembly_max_timeout_ms, 1, 60000); + PARSE_AND_CHECK_NUM(table, "tcp_reassembly_max_segments", cfg->tcp_reassembly_max_segments, 1, 512); + + ret = 0; +error_out: + if (root) + { + toml_free(root); + } + if (fp) + { + fclose(fp); + } + + return ret; +} + +struct session_manager_config *session_manager_config_new(const char *toml_file) +{ + if (toml_file == NULL) { return NULL; } + struct session_manager_config *cfg = (struct session_manager_config *)calloc(1, sizeof(struct session_manager_config)); + if (cfg == NULL) + { + return NULL; + } + + if (session_manager_config_load(cfg, toml_file) == -1) + { + session_manager_config_free(cfg); + return NULL; + } + + return cfg; +} + +void session_manager_config_free(struct session_manager_config *cfg) +{ + if (cfg) + { + free(cfg); + cfg = NULL; + } +} + +void session_manager_config_print(struct session_manager_config *cfg) +{ + if (cfg) + { + // max session number + SESSION_LOG_INFO("session_manager.max_tcp_session_num : %lu", cfg->max_tcp_session_num); + SESSION_LOG_INFO("session_manager.max_udp_session_num : %lu", cfg->max_udp_session_num); + + // session overload + SESSION_LOG_INFO("session_manager.tcp_overload_evict_old_sess : %d", cfg->tcp_overload_evict_old_sess); + SESSION_LOG_INFO("session_manager.udp_overload_evict_old_sess : %d", cfg->udp_overload_evict_old_sess); + + // TCP timeout + SESSION_LOG_INFO("session_manager.tcp_init_timeout_ms : %lu", cfg->tcp_init_timeout_ms); + SESSION_LOG_INFO("session_manager.tcp_handshake_timeout_ms : %lu", cfg->tcp_handshake_timeout_ms); + SESSION_LOG_INFO("session_manager.tcp_data_timeout_ms : %lu", cfg->tcp_data_timeout_ms); + SESSION_LOG_INFO("session_manager.tcp_half_closed_timeout_ms : %lu", cfg->tcp_half_closed_timeout_ms); + SESSION_LOG_INFO("session_manager.tcp_time_wait_timeout_ms : %lu", cfg->tcp_time_wait_timeout_ms); + SESSION_LOG_INFO("session_manager.tcp_discard_timeout_ms : %lu", cfg->tcp_discard_timeout_ms); + SESSION_LOG_INFO("session_manager.tcp_unverified_rst_timeout_ms : %lu", cfg->tcp_unverified_rst_timeout_ms); + + // UDP timeout + SESSION_LOG_INFO("session_manager.udp_data_timeout_ms : %lu", cfg->udp_data_timeout_ms); + SESSION_LOG_INFO("session_manager.udp_discard_timeout_ms : %lu", cfg->udp_discard_timeout_ms); + + // limit + SESSION_LOG_INFO("session_manager.session_expire_polling_interval_ms : %lu", cfg->session_expire_polling_interval_ms); + SESSION_LOG_INFO("session_manager.session_expire_polling_limit : %lu", cfg->session_expire_polling_limit); + + // duplicated packet filter + SESSION_LOG_INFO("session_manager.duplicated_packet_filter_enable : %d", cfg->duplicated_packet_filter_enable); + SESSION_LOG_INFO("session_manager.duplicated_packet_filter_capacity : %lu", cfg->duplicated_packet_filter_capacity); + SESSION_LOG_INFO("session_manager.duplicated_packet_filter_timeout_ms : %lu", cfg->duplicated_packet_filter_timeout_ms); + SESSION_LOG_INFO("session_manager.duplicated_packet_filter_error_rate : %f", cfg->duplicated_packet_filter_error_rate); + + // eviction session filter + SESSION_LOG_INFO("session_manager.evicted_session_filter_enable : %d", cfg->evicted_session_filter_enable); + SESSION_LOG_INFO("session_manager.evicted_session_filter_capacity : %lu", cfg->evicted_session_filter_capacity); + SESSION_LOG_INFO("session_manager.evicted_session_filter_timeout_ms : %lu", cfg->evicted_session_filter_timeout_ms); + SESSION_LOG_INFO("session_manager.evicted_session_filter_error_rate : %f", cfg->evicted_session_filter_error_rate); + + // TCP reassembly + SESSION_LOG_INFO("session_manager.tcp_reassembly_enable : %d", cfg->tcp_reassembly_enable); + SESSION_LOG_INFO("session_manager.tcp_reassembly_max_timeout_ms : %lu", cfg->tcp_reassembly_max_timeout_ms); + SESSION_LOG_INFO("session_manager.tcp_reassembly_max_segments : %lu", cfg->tcp_reassembly_max_segments); + } +} + +struct session_manager *session_manager_new(const struct session_manager_config *cfg, uint64_t now_ms) +{ struct session_manager *mgr = (struct session_manager *)calloc(1, sizeof(struct session_manager)); if (mgr == NULL) { return NULL; } - memcpy(&mgr->opts, opts, sizeof(struct session_manager_options)); + memcpy(&mgr->cfg, cfg, sizeof(struct session_manager_config)); - mgr->sess_pool = session_pool_new(mgr->opts.max_tcp_session_num + mgr->opts.max_udp_session_num); + mgr->sess_pool = session_pool_new(mgr->cfg.max_tcp_session_num + mgr->cfg.max_udp_session_num); mgr->tcp_sess_table = session_table_new(); mgr->udp_sess_table = session_table_new(); mgr->sess_timer = session_timer_new(now_ms); @@ -911,21 +991,21 @@ struct session_manager *session_manager_new(struct session_manager_options *opts { goto error; } - if (mgr->opts.evicted_session_filter_enable) + if (mgr->cfg.evicted_session_filter_enable) { - mgr->evicte_sess_filter = session_filter_new(mgr->opts.evicted_session_filter_capacity, - mgr->opts.evicted_session_filter_timeout, - mgr->opts.evicted_session_filter_error_rate, now_ms); + mgr->evicte_sess_filter = session_filter_new(mgr->cfg.evicted_session_filter_capacity, + mgr->cfg.evicted_session_filter_timeout_ms, + mgr->cfg.evicted_session_filter_error_rate, now_ms); if (mgr->evicte_sess_filter == NULL) { goto error; } } - if (mgr->opts.duplicated_packet_filter_enable) + if (mgr->cfg.duplicated_packet_filter_enable) { - 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); + mgr->dup_pkt_filter = packet_filter_new(mgr->cfg.duplicated_packet_filter_capacity, + mgr->cfg.duplicated_packet_filter_timeout_ms, + mgr->cfg.duplicated_packet_filter_error_rate, now_ms); if (mgr->dup_pkt_filter == NULL) { goto error; @@ -935,6 +1015,7 @@ struct session_manager *session_manager_new(struct session_manager_options *opts INIT_LIST_HEAD(&mgr->evicte_queue); session_transition_init(); mgr->now_ms = now_ms; + mgr->last_clean_expired_sess_ts = now_ms; return mgr; @@ -965,11 +1046,11 @@ void session_manager_free(struct session_manager *mgr) { session_manager_free_session(mgr, sess); } - if (mgr->opts.evicted_session_filter_enable) + if (mgr->cfg.evicted_session_filter_enable) { session_filter_free(mgr->evicte_sess_filter); } - if (mgr->opts.duplicated_packet_filter_enable) + if (mgr->cfg.duplicated_packet_filter_enable) { packet_filter_free(mgr->dup_pkt_filter); } @@ -989,7 +1070,7 @@ void session_manager_set_session_id_generator(struct session_manager *mgr, sessi void session_manager_record_duplicated_packet(struct session_manager *mgr, const struct packet *pkt) { - if (mgr->opts.duplicated_packet_filter_enable) + if (mgr->cfg.duplicated_packet_filter_enable) { packet_filter_add(mgr->dup_pkt_filter, pkt, mgr->now_ms); } @@ -1174,10 +1255,10 @@ struct session *session_manager_get_expired_session(struct session_manager *mgr, switch (session_get_type(sess)) { case SESSION_TYPE_TCP: - session_timer_update(mgr->sess_timer, sess, now_ms + mgr->opts.tcp_data_timeout); + session_timer_update(mgr->sess_timer, sess, now_ms + mgr->cfg.tcp_data_timeout_ms); break; case SESSION_TYPE_UDP: - session_timer_update(mgr->sess_timer, sess, now_ms + mgr->opts.udp_data_timeout); + session_timer_update(mgr->sess_timer, sess, now_ms + mgr->cfg.udp_data_timeout_ms); break; default: assert(0); @@ -1204,6 +1285,55 @@ struct session *session_manager_get_evicted_session(struct session_manager *mgr) } } +// array_size at least EVICTE_SESSION_BURST, suggest 2 * EVICTE_SESSION_BURST +uint64_t session_manager_clean_session(struct session_manager *mgr, uint64_t now_ms, struct session *cleaned_sess[], uint64_t array_size) +{ + mgr->now_ms = now_ms; + struct session *sess = NULL; + uint64_t cleaned_sess_num = 0; + uint64_t expired_sess_num = 0; + + uint8_t expired_sess_canbe_clean = 0; + if (now_ms - mgr->last_clean_expired_sess_ts >= mgr->cfg.session_expire_polling_interval_ms) + { + expired_sess_canbe_clean = 1; + } + + for (uint64_t i = 0; i < array_size; i++) + { + // frist clean evicted session + sess = session_manager_get_evicted_session(mgr); + if (sess) + { + cleaned_sess[cleaned_sess_num++] = sess; + } + // then clean expired session + else + { + if (expired_sess_canbe_clean && expired_sess_num < mgr->cfg.session_expire_polling_limit) + { + mgr->last_clean_expired_sess_ts = now_ms; + sess = session_manager_get_expired_session(mgr, now_ms); + if (sess) + { + cleaned_sess[cleaned_sess_num++] = sess; + expired_sess_num++; + } + else + { + break; + } + } + else + { + break; + } + } + } + + return cleaned_sess_num; +} + uint64_t session_manager_get_expire_interval(struct session_manager *mgr) { return session_timer_next_expire_interval(mgr->sess_timer); @@ -1226,11 +1356,11 @@ void session_set_discard(struct session *sess) switch (type) { case SESSION_TYPE_TCP: - session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.tcp_discard_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->cfg.tcp_discard_timeout_ms); SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, tcp); break; case SESSION_TYPE_UDP: - session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->opts.udp_discard_timeout); + session_timer_update(mgr->sess_timer, sess, mgr->now_ms + mgr->cfg.udp_discard_timeout_ms); SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp); break; default: @@ -1239,16 +1369,6 @@ void session_set_discard(struct session *sess) } } -static inline uint8_t ipv4_in_range(const struct in_addr *addr, const struct in_addr *start, const struct in_addr *end) -{ - return (memcmp(addr, start, sizeof(struct in_addr)) >= 0 && memcmp(addr, end, sizeof(struct in_addr)) <= 0); -} - -static inline uint8_t ipv6_in_range(const struct in6_addr *addr, const struct in6_addr *start, const struct in6_addr *end) -{ - return (memcmp(addr, start, sizeof(struct in6_addr)) >= 0 && memcmp(addr, end, sizeof(struct in6_addr)) <= 0); -} - uint64_t session_manager_scan(const struct session_manager *mgr, const struct session_scan_opts *opts, uint64_t mached_sess_ids[], uint64_t array_size) { uint64_t capacity = 0; diff --git a/infra/session_manager/session_manager.h b/infra/session_manager/session_manager.h index 32bc8e7..89434f3 100644 --- a/infra/session_manager/session_manager.h +++ b/infra/session_manager/session_manager.h @@ -8,7 +8,7 @@ extern "C" #include "tuple.h" #include "stellar/session.h" -struct session_manager_options +struct session_manager_config { // max session number uint64_t max_tcp_session_num; @@ -19,33 +19,37 @@ struct session_manager_options uint8_t udp_overload_evict_old_sess; // 1: evict old session, 0: bypass new session // TCP timeout - uint64_t tcp_init_timeout; // range: [1, 60000] (ms) - uint64_t tcp_handshake_timeout; // range: [1, 60000] (ms) - uint64_t tcp_data_timeout; // range: [1, 15999999000] (ms) - uint64_t tcp_half_closed_timeout; // range: [1, 604800000] (ms) - uint64_t tcp_time_wait_timeout; // range: [1, 600000] (ms) - uint64_t tcp_discard_timeout; // range: [1, 15999999000] (ms) - uint64_t tcp_unverified_rst_timeout; // range: [1, 600000] (ms) + uint64_t tcp_init_timeout_ms; // range: [1, 60000] (ms) + uint64_t tcp_handshake_timeout_ms; // range: [1, 60000] (ms) + uint64_t tcp_data_timeout_ms; // range: [1, 15999999000] (ms) + uint64_t tcp_half_closed_timeout_ms; // range: [1, 604800000] (ms) + uint64_t tcp_time_wait_timeout_ms; // range: [1, 600000] (ms) + uint64_t tcp_discard_timeout_ms; // range: [1, 15999999000] (ms) + uint64_t tcp_unverified_rst_timeout_ms; // range: [1, 600000] (ms) // UDP timeout - uint64_t udp_data_timeout; // range: [1, 15999999000] (ms) - uint64_t udp_discard_timeout; // range: [1, 15999999000] (ms) + uint64_t udp_data_timeout_ms; // range: [1, 15999999000] (ms) + uint64_t udp_discard_timeout_ms; // range: [1, 15999999000] (ms) + + // limit + uint64_t session_expire_polling_interval_ms; // range: [0, 60000] (ms) + uint64_t session_expire_polling_limit; // range: [1, 1024] // duplicate packet filter uint8_t duplicated_packet_filter_enable; - uint32_t duplicated_packet_filter_capacity; // range: [1, 4294967295] - uint32_t duplicated_packet_filter_timeout; // range: [1, 60000] (ms) - double duplicated_packet_filter_error_rate; // range: [0.0, 1.0] + uint32_t duplicated_packet_filter_capacity; // range: [1, 4294967295] + uint32_t duplicated_packet_filter_timeout_ms; // range: [1, 60000] (ms) + double duplicated_packet_filter_error_rate; // range: [0.0, 1.0] // evicted session filter uint8_t evicted_session_filter_enable; - uint32_t evicted_session_filter_capacity; // range: [1, 4294967295] - uint32_t evicted_session_filter_timeout; // range: [1, 60000] (ms) - double evicted_session_filter_error_rate; // range: [0.0, 1.0] + uint32_t evicted_session_filter_capacity; // range: [1, 4294967295] + uint32_t evicted_session_filter_timeout_ms; // range: [1, 60000] (ms) + double evicted_session_filter_error_rate; // range: [0.0, 1.0] // TCP reassembly uint8_t tcp_reassembly_enable; - uint32_t tcp_reassembly_max_timeout; // range: [1, 60000] (ms) - uint32_t tcp_reassembly_max_segments; // range: [2, 512] + uint32_t tcp_reassembly_max_timeout_ms; // range: [1, 60000] (ms) + uint32_t tcp_reassembly_max_segments; // range: [2, 512] }; struct __attribute__((aligned(64))) session_manager_stat @@ -129,8 +133,12 @@ struct session_scan_opts uint64_t last_pkt_time_ms[2]; }; +struct session_manager_config *session_manager_config_new(const char *toml_file); +void session_manager_config_free(struct session_manager_config *cfg); +void session_manager_config_print(struct session_manager_config *cfg); + struct session_manager; -struct session_manager *session_manager_new(struct session_manager_options *opts, uint64_t now_ms); +struct session_manager *session_manager_new(const struct session_manager_config *cfg, uint64_t now_ms); void session_manager_free(struct session_manager *mgr); typedef uint64_t (*session_id_generate_fn)(uint64_t now_ms); @@ -147,6 +155,7 @@ int session_manager_update_session(struct session_manager *mgr, struct session * // return session need free by session_manager_free_session() struct session *session_manager_get_expired_session(struct session_manager *mgr, uint64_t now_ms); struct session *session_manager_get_evicted_session(struct session_manager *mgr); +uint64_t session_manager_clean_session(struct session_manager *mgr, uint64_t now_ms, struct session *cleaned_sess[], uint64_t array_size); // return 0: have already timeout session // return >0: next expire interval diff --git a/infra/session_manager/test/gtest_case_tcp_fast_open.cpp b/infra/session_manager/test/gtest_case_tcp_fast_open.cpp index 3680faa..55ea792 100644 --- a/infra/session_manager/test/gtest_case_tcp_fast_open.cpp +++ b/infra/session_manager/test/gtest_case_tcp_fast_open.cpp @@ -6,7 +6,7 @@ #include "session_private.h" #include "session_manager.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -16,33 +16,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -312,7 +316,7 @@ TEST(CASE, TCP_FAST_OPEN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -402,8 +406,8 @@ TEST(CASE, TCP_FAST_OPEN) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout) == NULL); // opening -> closing - sess = session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout + opts.tcp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms) == NULL); // opening -> closing + sess = session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms + cfg.tcp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_filter_tcp_dupkt.cpp b/infra/session_manager/test/gtest_filter_tcp_dupkt.cpp index 93c3243..9bb3fa2 100644 --- a/infra/session_manager/test/gtest_filter_tcp_dupkt.cpp +++ b/infra/session_manager/test/gtest_filter_tcp_dupkt.cpp @@ -6,7 +6,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -16,33 +16,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -68,7 +72,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYN_DUP) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -135,7 +139,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYNACK_DUP) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -203,7 +207,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP) struct session_manager_stat *stat = NULL; char syn_retransmission[1500] = {0}; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -286,11 +290,11 @@ TEST(TCP_DUPKT_FILTER_DISABLE, SYN_DUP) struct session *sess = NULL; struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - struct session_manager_options _opts; - memcpy(&_opts, &opts, sizeof(struct session_manager_options)); - _opts.duplicated_packet_filter_enable = 0; + struct session_manager_config _cfg; + memcpy(&_cfg, &cfg, sizeof(struct session_manager_config)); + _cfg.duplicated_packet_filter_enable = 0; - mgr = session_manager_new(&_opts, 1); + mgr = session_manager_new(&_cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -337,11 +341,11 @@ TEST(TCP_DUPKT_FILTER_DISABLE, SYNACK_DUP) struct session *sess = NULL; struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - struct session_manager_options _opts; - memcpy(&_opts, &opts, sizeof(struct session_manager_options)); - _opts.duplicated_packet_filter_enable = 0; + struct session_manager_config _cfg; + memcpy(&_cfg, &cfg, sizeof(struct session_manager_config)); + _cfg.duplicated_packet_filter_enable = 0; - mgr = session_manager_new(&_opts, 1); + mgr = session_manager_new(&_cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); diff --git a/infra/session_manager/test/gtest_overload_evict_tcp_sess.cpp b/infra/session_manager/test/gtest_overload_evict_tcp_sess.cpp index a50d858..9dbfe2b 100644 --- a/infra/session_manager/test/gtest_overload_evict_tcp_sess.cpp +++ b/infra/session_manager/test/gtest_overload_evict_tcp_sess.cpp @@ -7,7 +7,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = RX_BURST_MAX * 2, .max_udp_session_num = RX_BURST_MAX * 2, @@ -17,33 +17,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -68,7 +72,7 @@ TEST(TCP_OVERLOAD, EVICT_OLD_SESS) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -79,15 +83,15 @@ TEST(TCP_OVERLOAD, EVICT_OLD_SESS) printf("<= Packet Parse: done\n\n"); // new session - for (uint32_t i = 0; i < opts.max_tcp_session_num; i++) + for (uint32_t i = 0; i < cfg.max_tcp_session_num; i++) { packet_set_ip_src_addr(&pkt, i); EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1)); } - printf("=> Session Manager: after add %lu new sessions\n", opts.max_tcp_session_num); + printf("=> Session Manager: after add %lu new sessions\n", cfg.max_tcp_session_num); stat = session_manager_stat(mgr); EXPECT_TRUE(stat); - EXPECT_TRUE(stat->tcp_sess_used == opts.max_tcp_session_num); + EXPECT_TRUE(stat->tcp_sess_used == cfg.max_tcp_session_num); EXPECT_TRUE(stat->tcp_sess_opening == RX_BURST_MAX); EXPECT_TRUE(stat->tcp_sess_active == 0); EXPECT_TRUE(stat->tcp_sess_closing == 0); @@ -106,11 +110,11 @@ TEST(TCP_OVERLOAD, EVICT_NEW_SESS) struct packet pkt; struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - struct session_manager_options _opts; - memcpy(&_opts, &opts, sizeof(struct session_manager_options)); - _opts.tcp_overload_evict_old_sess = 0; + struct session_manager_config _cfg; + memcpy(&_cfg, &cfg, sizeof(struct session_manager_config)); + _cfg.tcp_overload_evict_old_sess = 0; - mgr = session_manager_new(&_opts, 1); + mgr = session_manager_new(&_cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -121,16 +125,16 @@ TEST(TCP_OVERLOAD, EVICT_NEW_SESS) printf("<= Packet Parse: done\n\n"); // new session - for (uint32_t i = 0; i < opts.max_tcp_session_num; i++) + for (uint32_t i = 0; i < cfg.max_tcp_session_num; i++) { packet_set_ip_src_addr(&pkt, i); EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1)); } - printf("=> Session Manager: after add %lu new sessions\n", opts.max_tcp_session_num); + printf("=> Session Manager: after add %lu new sessions\n", cfg.max_tcp_session_num); stat = session_manager_stat(mgr); EXPECT_TRUE(stat); - EXPECT_TRUE(stat->tcp_sess_used == opts.max_tcp_session_num); - EXPECT_TRUE(stat->tcp_sess_opening == opts.max_tcp_session_num); + EXPECT_TRUE(stat->tcp_sess_used == cfg.max_tcp_session_num); + EXPECT_TRUE(stat->tcp_sess_opening == cfg.max_tcp_session_num); EXPECT_TRUE(stat->tcp_sess_active == 0); EXPECT_TRUE(stat->tcp_sess_closing == 0); EXPECT_TRUE(stat->tcp_sess_closed == 0); @@ -141,15 +145,15 @@ TEST(TCP_OVERLOAD, EVICT_NEW_SESS) // table full, evict new session for (uint32_t i = 0; i < RX_BURST_MAX; i++) { - packet_set_ip_src_addr(&pkt, opts.max_tcp_session_num + i); + packet_set_ip_src_addr(&pkt, cfg.max_tcp_session_num + i); EXPECT_TRUE(session_manager_lookup_session_by_packet(mgr, &pkt) == NULL); EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1) == NULL); } printf("=> Session Manager: after evicte new session\n"); stat = session_manager_stat(mgr); EXPECT_TRUE(stat); - EXPECT_TRUE(stat->tcp_sess_used == opts.max_tcp_session_num); - EXPECT_TRUE(stat->tcp_sess_opening == opts.max_tcp_session_num); + EXPECT_TRUE(stat->tcp_sess_used == cfg.max_tcp_session_num); + EXPECT_TRUE(stat->tcp_sess_opening == cfg.max_tcp_session_num); EXPECT_TRUE(stat->tcp_sess_active == 0); EXPECT_TRUE(stat->tcp_sess_closing == 0); EXPECT_TRUE(stat->tcp_sess_closed == 0); diff --git a/infra/session_manager/test/gtest_overload_evict_udp_sess.cpp b/infra/session_manager/test/gtest_overload_evict_udp_sess.cpp index 7137896..2f15528 100644 --- a/infra/session_manager/test/gtest_overload_evict_udp_sess.cpp +++ b/infra/session_manager/test/gtest_overload_evict_udp_sess.cpp @@ -7,7 +7,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = RX_BURST_MAX * 2, .max_udp_session_num = RX_BURST_MAX * 2, @@ -17,33 +17,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -69,7 +73,7 @@ TEST(UDP_OVERLOAD, EVICT_OLD_SESS) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -80,15 +84,15 @@ TEST(UDP_OVERLOAD, EVICT_OLD_SESS) printf("<= Packet Parse: done\n\n"); // new session - for (uint32_t i = 0; i < opts.max_udp_session_num; i++) + for (uint32_t i = 0; i < cfg.max_udp_session_num; i++) { packet_set_ip_src_addr(&pkt, i); EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1)); } - printf("=> Session Manager: after add %lu new sessions\n", opts.max_udp_session_num); + printf("=> Session Manager: after add %lu new sessions\n", cfg.max_udp_session_num); stat = session_manager_stat(mgr); EXPECT_TRUE(stat); - EXPECT_TRUE(stat->udp_sess_used == opts.max_udp_session_num); + EXPECT_TRUE(stat->udp_sess_used == cfg.max_udp_session_num); EXPECT_TRUE(stat->udp_sess_opening == RX_BURST_MAX); EXPECT_TRUE(stat->udp_sess_active == 0); EXPECT_TRUE(stat->udp_sess_closing == 0); @@ -132,7 +136,7 @@ TEST(UDP_OVERLOAD, EVICT_OLD_SESS) // evicted session timeout packet_set_ip_src_addr(&pkt, 0); EXPECT_TRUE(session_manager_lookup_session_by_packet(mgr, &pkt) == NULL); - EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1 + opts.evicted_session_filter_timeout)); + EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1 + cfg.evicted_session_filter_timeout_ms)); printf("=> Session Manager: after evicted session timeout\n"); stat = session_manager_stat(mgr); EXPECT_TRUE(stat); @@ -155,11 +159,11 @@ TEST(UDP_OVERLOAD, EVICT_NEW_SESS) struct packet pkt; struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - struct session_manager_options _opts; - memcpy(&_opts, &opts, sizeof(struct session_manager_options)); - _opts.udp_overload_evict_old_sess = 0; + struct session_manager_config _cfg; + memcpy(&_cfg, &cfg, sizeof(struct session_manager_config)); + _cfg.udp_overload_evict_old_sess = 0; - mgr = session_manager_new(&_opts, 1); + mgr = session_manager_new(&_cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -170,16 +174,16 @@ TEST(UDP_OVERLOAD, EVICT_NEW_SESS) printf("<= Packet Parse: done\n\n"); // new session - for (uint32_t i = 0; i < opts.max_udp_session_num; i++) + for (uint32_t i = 0; i < cfg.max_udp_session_num; i++) { packet_set_ip_src_addr(&pkt, i); EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1)); } - printf("=> Session Manager: after add %lu new sessions\n", opts.max_udp_session_num); + printf("=> Session Manager: after add %lu new sessions\n", cfg.max_udp_session_num); stat = session_manager_stat(mgr); EXPECT_TRUE(stat); - EXPECT_TRUE(stat->udp_sess_used == opts.max_udp_session_num); - EXPECT_TRUE(stat->udp_sess_opening == opts.max_udp_session_num); + EXPECT_TRUE(stat->udp_sess_used == cfg.max_udp_session_num); + EXPECT_TRUE(stat->udp_sess_opening == cfg.max_udp_session_num); EXPECT_TRUE(stat->udp_sess_active == 0); EXPECT_TRUE(stat->udp_sess_closing == 0); EXPECT_TRUE(stat->udp_sess_closed == 0); @@ -193,15 +197,15 @@ TEST(UDP_OVERLOAD, EVICT_NEW_SESS) // table full, evict new session for (uint32_t i = 0; i < RX_BURST_MAX; i++) { - packet_set_ip_src_addr(&pkt, opts.max_udp_session_num + i); + packet_set_ip_src_addr(&pkt, cfg.max_udp_session_num + i); EXPECT_TRUE(session_manager_lookup_session_by_packet(mgr, &pkt) == NULL); EXPECT_TRUE(session_manager_new_session(mgr, &pkt, 1) == NULL); } printf("=> Session Manager: after readd %d evicted session\n", RX_BURST_MAX); stat = session_manager_stat(mgr); EXPECT_TRUE(stat); - EXPECT_TRUE(stat->udp_sess_used == opts.max_udp_session_num); - EXPECT_TRUE(stat->udp_sess_opening == opts.max_udp_session_num); + EXPECT_TRUE(stat->udp_sess_used == cfg.max_udp_session_num); + EXPECT_TRUE(stat->udp_sess_opening == cfg.max_udp_session_num); EXPECT_TRUE(stat->udp_sess_active == 0); EXPECT_TRUE(stat->udp_sess_closing == 0); EXPECT_TRUE(stat->udp_sess_closed == 0); diff --git a/infra/session_manager/test/gtest_sess_mgr_scan.cpp b/infra/session_manager/test/gtest_sess_mgr_scan.cpp index 708554c..5d353db 100644 --- a/infra/session_manager/test/gtest_sess_mgr_scan.cpp +++ b/infra/session_manager/test/gtest_sess_mgr_scan.cpp @@ -6,7 +6,7 @@ #include "packet_parser.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -16,33 +16,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -131,7 +135,7 @@ TEST(SESS_MGR_SCAN, OPTS) inet_pton(AF_INET6, "cafe::0000", &v6_dst_subnet_beg); inet_pton(AF_INET6, "cafe::ffff", &v6_dst_subnet_end); - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); diff --git a/infra/session_manager/test/gtest_sess_mgr_tcp_reassembly.cpp b/infra/session_manager/test/gtest_sess_mgr_tcp_reassembly.cpp index 9e5d0da..952852b 100644 --- a/infra/session_manager/test/gtest_sess_mgr_tcp_reassembly.cpp +++ b/infra/session_manager/test/gtest_sess_mgr_tcp_reassembly.cpp @@ -6,7 +6,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -16,33 +16,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -74,7 +78,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER) struct session *sess = NULL; struct session_manager *mgr = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -245,8 +249,8 @@ TEST(SESS_MGR_TCP_REASSEMBLY, OUT_OF_ORDER) session_free_tcp_segment(sess, seg); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 7 + opts.tcp_data_timeout) == NULL); // active -> closing - sess = session_manager_get_expired_session(mgr, 7 + opts.tcp_data_timeout + opts.tcp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 7 + cfg.tcp_data_timeout_ms) == NULL); // active -> closing + sess = session_manager_get_expired_session(mgr, 7 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -266,7 +270,7 @@ TEST(SESS_MGR_TCP_REASSEMBLY, SEQ_WRAPAROUND) struct session *sess = NULL; struct session_manager *mgr = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -339,8 +343,8 @@ TEST(SESS_MGR_TCP_REASSEMBLY, SEQ_WRAPAROUND) session_free_tcp_segment(sess, seg); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 4 + opts.tcp_data_timeout) == NULL); // active -> closing - sess = session_manager_get_expired_session(mgr, 4 + opts.tcp_data_timeout + opts.tcp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 4 + cfg.tcp_data_timeout_ms) == NULL); // active -> closing + sess = session_manager_get_expired_session(mgr, 4 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_state_tcp_active_to_closing.cpp b/infra/session_manager/test/gtest_state_tcp_active_to_closing.cpp index 5f79726..60cbbbe 100644 --- a/infra/session_manager/test/gtest_state_tcp_active_to_closing.cpp +++ b/infra/session_manager/test/gtest_state_tcp_active_to_closing.cpp @@ -8,7 +8,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -18,33 +18,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -96,7 +100,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_FIN_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -155,7 +159,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_FIN_FIN) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_time_wait_timeout); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_time_wait_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_CLIENT_FIN); @@ -188,7 +192,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -241,7 +245,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_RST) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_unverified_rst_timeout); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_unverified_rst_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_CLIENT_RST); @@ -274,7 +278,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -327,7 +331,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_RST) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_unverified_rst_timeout); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_unverified_rst_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_SERVER_RST); @@ -358,7 +362,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_DATA_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -374,8 +378,8 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_DATA_TIMEOUT) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -408,7 +412,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_HALF_CLOSED_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -455,7 +459,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_HALF_CLOSED_TIMEOUT) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_half_closed_timeout); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_half_closed_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_CLIENT_FIN); @@ -488,7 +492,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_HALF_CLOSED_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -535,7 +539,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_HALF_CLOSED_TIMEOUT) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_half_closed_timeout); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_half_closed_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_SERVER_FIN); diff --git a/infra/session_manager/test/gtest_state_tcp_init_to_opening.cpp b/infra/session_manager/test/gtest_state_tcp_init_to_opening.cpp index 30c07fc..6434a3b 100644 --- a/infra/session_manager/test/gtest_state_tcp_init_to_opening.cpp +++ b/infra/session_manager/test/gtest_state_tcp_init_to_opening.cpp @@ -8,7 +8,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -18,33 +18,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -67,7 +71,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -111,8 +115,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout) == NULL); // opening -> closing - sess = session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout + opts.tcp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms) == NULL); // opening -> closing + sess = session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms + cfg.tcp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -145,7 +149,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -189,8 +193,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.tcp_handshake_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 1 + opts.tcp_handshake_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.tcp_handshake_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 1 + cfg.tcp_handshake_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -223,7 +227,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -279,8 +283,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_handshake_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_handshake_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_handshake_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_handshake_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -313,7 +317,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK_ACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -381,8 +385,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK_ACK) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 3 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 3 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -417,7 +421,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -478,8 +482,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_init_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_init_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_init_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_init_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -514,7 +518,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -576,8 +580,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_handshake_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_handshake_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_handshake_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_handshake_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -610,7 +614,7 @@ TEST(TCP_INIT_TO_OPENING, BY_C2S_ASMMETRIC) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -666,8 +670,8 @@ TEST(TCP_INIT_TO_OPENING, BY_C2S_ASMMETRIC) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -700,7 +704,7 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -756,8 +760,8 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp b/infra/session_manager/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp index 98b4ea2..4d569e7 100644 --- a/infra/session_manager/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp +++ b/infra/session_manager/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp @@ -8,7 +8,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -18,33 +18,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -63,7 +67,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -417,7 +421,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 11 + opts.tcp_time_wait_timeout); + sess = session_manager_get_expired_session(mgr, 11 + cfg.tcp_time_wait_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_CLIENT_FIN); diff --git a/infra/session_manager/test/gtest_state_tcp_opening_to_active.cpp b/infra/session_manager/test/gtest_state_tcp_opening_to_active.cpp index d0fb0aa..1e5f533 100644 --- a/infra/session_manager/test/gtest_state_tcp_opening_to_active.cpp +++ b/infra/session_manager/test/gtest_state_tcp_opening_to_active.cpp @@ -8,7 +8,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -18,33 +18,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -67,7 +71,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYN_C2S_DATA) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -123,8 +127,8 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYN_C2S_DATA) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -157,7 +161,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYNACK_S2C_DATA) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -213,8 +217,8 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYNACK_S2C_DATA) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_state_tcp_opening_to_closing.cpp b/infra/session_manager/test/gtest_state_tcp_opening_to_closing.cpp index e53630f..a0951f8 100644 --- a/infra/session_manager/test/gtest_state_tcp_opening_to_closing.cpp +++ b/infra/session_manager/test/gtest_state_tcp_opening_to_closing.cpp @@ -8,7 +8,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -18,33 +18,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -67,7 +71,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_FIN_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -135,7 +139,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_FIN_FIN) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_time_wait_timeout); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_time_wait_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_CLIENT_FIN); @@ -168,7 +172,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -230,7 +234,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_RST) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_unverified_rst_timeout); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_unverified_rst_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_CLIENT_RST); @@ -263,7 +267,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -325,7 +329,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_RST) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_unverified_rst_timeout); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_unverified_rst_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_SERVER_RST); @@ -357,7 +361,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_INIT_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -382,8 +386,8 @@ TEST(TCP_OPENING_TO_CLOSING, BY_INIT_TIMEOUT) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -416,7 +420,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_HANDSHAKE_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -472,8 +476,8 @@ TEST(TCP_OPENING_TO_CLOSING, BY_HANDSHAKE_TIMEOUT) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_handshake_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_handshake_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_handshake_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_handshake_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -506,7 +510,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -574,8 +578,8 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT) EXPECT_TRUE(stat->tcp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 3 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 3 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 3 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 3 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -608,7 +612,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_HALF_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -664,7 +668,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_HALF_FIN) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_half_closed_timeout); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_half_closed_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_CLIENT_FIN); @@ -697,7 +701,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_HALF_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -753,7 +757,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_HALF_FIN) EXPECT_TRUE(stat->tcp_sess_closing == 1); // expire session - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_half_closed_timeout); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_half_closed_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_SERVER_FIN); diff --git a/infra/session_manager/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp b/infra/session_manager/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp index 90a01a0..41ee468 100644 --- a/infra/session_manager/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp +++ b/infra/session_manager/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp @@ -8,7 +8,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -18,33 +18,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -63,7 +67,7 @@ TEST(UDP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING, TEST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -137,8 +141,8 @@ TEST(UDP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING, TEST) EXPECT_TRUE(stat->udp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.udp_data_timeout) == NULL); // active -> closing - sess = session_manager_get_expired_session(mgr, 2 + opts.udp_data_timeout + opts.udp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.udp_data_timeout_ms) == NULL); // active -> closing + sess = session_manager_get_expired_session(mgr, 2 + cfg.udp_data_timeout_ms + cfg.udp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_state_udp_init_to_opening_to_closing.cpp b/infra/session_manager/test/gtest_state_udp_init_to_opening_to_closing.cpp index bc59eb1..06dae39 100644 --- a/infra/session_manager/test/gtest_state_udp_init_to_opening_to_closing.cpp +++ b/infra/session_manager/test/gtest_state_udp_init_to_opening_to_closing.cpp @@ -8,7 +8,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -18,33 +18,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -68,7 +72,7 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_C2S) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -112,8 +116,8 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_C2S) EXPECT_TRUE(stat->udp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.udp_data_timeout) == NULL); // opening -> closing - sess = session_manager_get_expired_session(mgr, 1 + opts.udp_data_timeout + opts.udp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.udp_data_timeout_ms) == NULL); // opening -> closing + sess = session_manager_get_expired_session(mgr, 1 + cfg.udp_data_timeout_ms + cfg.udp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -147,7 +151,7 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_S2C) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -191,8 +195,8 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_S2C) EXPECT_TRUE(stat->udp_sess_closing == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.udp_data_timeout) == NULL); // opening -> closing - sess = session_manager_get_expired_session(mgr, 1 + opts.udp_data_timeout + opts.udp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.udp_data_timeout_ms) == NULL); // opening -> closing + sess = session_manager_get_expired_session(mgr, 1 + cfg.udp_data_timeout_ms + cfg.udp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); diff --git a/infra/session_manager/test/gtest_timeout_tcp_data.cpp b/infra/session_manager/test/gtest_timeout_tcp_data.cpp index 47ff9cd..0582b7b 100644 --- a/infra/session_manager/test/gtest_timeout_tcp_data.cpp +++ b/infra/session_manager/test/gtest_timeout_tcp_data.cpp @@ -7,7 +7,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -17,33 +17,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -60,7 +64,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_DATA) struct session *sess = NULL; struct session_manager *mgr = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -89,8 +93,8 @@ TEST(TIMEOUT, TCP_TIMEOUT_DATA) EXPECT_TRUE(session_manager_update_session(mgr, sess, &pkt, 2) == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 2 + opts.tcp_data_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 2 + cfg.tcp_data_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_timeout_tcp_handshake.cpp b/infra/session_manager/test/gtest_timeout_tcp_handshake.cpp index 68d10f5..4ea2e62 100644 --- a/infra/session_manager/test/gtest_timeout_tcp_handshake.cpp +++ b/infra/session_manager/test/gtest_timeout_tcp_handshake.cpp @@ -7,7 +7,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -17,33 +17,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -60,7 +64,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_HANDSHAKE) struct session *sess = NULL; struct session_manager *mgr = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -77,8 +81,8 @@ TEST(TIMEOUT, TCP_TIMEOUT_HANDSHAKE) EXPECT_TRUE(sess); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.tcp_handshake_timeout) == NULL); - sess = session_manager_get_expired_session(mgr, 1 + opts.tcp_handshake_timeout + opts.tcp_data_timeout); + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.tcp_handshake_timeout_ms) == NULL); + sess = session_manager_get_expired_session(mgr, 1 + cfg.tcp_handshake_timeout_ms + cfg.tcp_data_timeout_ms); EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_timeout_tcp_init.cpp b/infra/session_manager/test/gtest_timeout_tcp_init.cpp index 7d9e1bb..f0136a2 100644 --- a/infra/session_manager/test/gtest_timeout_tcp_init.cpp +++ b/infra/session_manager/test/gtest_timeout_tcp_init.cpp @@ -7,7 +7,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -17,33 +17,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -60,7 +64,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_INIT) struct session *sess = NULL; struct session_manager *mgr = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -77,8 +81,8 @@ TEST(TIMEOUT, TCP_TIMEOUT_INIT) EXPECT_TRUE(sess); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout) == NULL); // opening -> closing - sess = session_manager_get_expired_session(mgr, 1 + opts.tcp_init_timeout + opts.tcp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms) == NULL); // opening -> closing + sess = session_manager_get_expired_session(mgr, 1 + cfg.tcp_init_timeout_ms + cfg.tcp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/infra/session_manager/test/gtest_timeout_udp_data.cpp b/infra/session_manager/test/gtest_timeout_udp_data.cpp index 60bb133..94c8e13 100644 --- a/infra/session_manager/test/gtest_timeout_udp_data.cpp +++ b/infra/session_manager/test/gtest_timeout_udp_data.cpp @@ -7,7 +7,7 @@ #include "session_manager.h" #include "test_packets.h" -struct session_manager_options opts = { +struct session_manager_config cfg = { // max session number .max_tcp_session_num = 256, .max_udp_session_num = 256, @@ -17,33 +17,37 @@ struct session_manager_options opts = { .udp_overload_evict_old_sess = 1, // 1: evict old session, 0: bypass new session // tcp timeout - .tcp_init_timeout = 1, - .tcp_handshake_timeout = 2, - .tcp_data_timeout = 3, - .tcp_half_closed_timeout = 4, - .tcp_time_wait_timeout = 5, - .tcp_discard_timeout = 6, - .tcp_unverified_rst_timeout = 7, + .tcp_init_timeout_ms = 1, + .tcp_handshake_timeout_ms = 2, + .tcp_data_timeout_ms = 3, + .tcp_half_closed_timeout_ms = 4, + .tcp_time_wait_timeout_ms = 5, + .tcp_discard_timeout_ms = 6, + .tcp_unverified_rst_timeout_ms = 7, // udp timeout - .udp_data_timeout = 8, - .udp_discard_timeout = 0, + .udp_data_timeout_ms = 8, + .udp_discard_timeout_ms = 0, + + // limit + .session_expire_polling_interval_ms = 0, + .session_expire_polling_limit = 1024, // duplicate packet filter .duplicated_packet_filter_enable = 1, .duplicated_packet_filter_capacity = 1000, - .duplicated_packet_filter_timeout = 10, + .duplicated_packet_filter_timeout_ms = 10, .duplicated_packet_filter_error_rate = 0.0001, // evicted session filter .evicted_session_filter_enable = 1, .evicted_session_filter_capacity = 1000, - .evicted_session_filter_timeout = 10, + .evicted_session_filter_timeout_ms = 10, .evicted_session_filter_error_rate = 0.0001, // TCP Reassembly .tcp_reassembly_enable = 1, - .tcp_reassembly_max_timeout = 60000, + .tcp_reassembly_max_timeout_ms = 60000, .tcp_reassembly_max_segments = 16, }; @@ -60,7 +64,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA1) struct session *sess = NULL; struct session_manager *mgr = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -77,8 +81,8 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA1) EXPECT_TRUE(sess); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + opts.udp_data_timeout) == NULL); // opening -> closing - sess = session_manager_get_expired_session(mgr, 1 + opts.udp_data_timeout + opts.udp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 1 + cfg.udp_data_timeout_ms) == NULL); // opening -> closing + sess = session_manager_get_expired_session(mgr, 1 + cfg.udp_data_timeout_ms + cfg.udp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); @@ -97,7 +101,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA2) struct session *sess = NULL; struct session_manager *mgr = NULL; - mgr = session_manager_new(&opts, 1); + mgr = session_manager_new(&cfg, 1); EXPECT_TRUE(mgr != NULL); session_manager_set_session_id_generator(mgr, session_id_generator); @@ -126,8 +130,8 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA2) EXPECT_TRUE(session_manager_update_session(mgr, sess, &pkt, 2) == 0); // expire session - EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + opts.udp_data_timeout) == NULL); // active -> closing - sess = session_manager_get_expired_session(mgr, 2 + opts.udp_data_timeout + opts.udp_data_timeout); // closing -> closed + EXPECT_TRUE(session_manager_get_expired_session(mgr, 2 + cfg.udp_data_timeout_ms) == NULL); // active -> closing + sess = session_manager_get_expired_session(mgr, 2 + cfg.udp_data_timeout_ms + cfg.udp_data_timeout_ms); // closing -> closed EXPECT_TRUE(sess); EXPECT_TRUE(session_get_current_state(sess) == SESSION_STATE_CLOSED); EXPECT_TRUE(session_get_closing_reason(sess) == CLOSING_BY_TIMEOUT); diff --git a/test/decoders/http/test_based_on_stellar/env/stellar.toml b/test/decoders/http/test_based_on_stellar/env/stellar.toml index ebafdee..da13527 100644 --- a/test/decoders/http/test_based_on_stellar/env/stellar.toml +++ b/test/decoders/http/test_based_on_stellar/env/stellar.toml @@ -10,7 +10,7 @@ dev_symbol = "nf_0_fw" dumpfile_path = "./pcap/test.pcap" #dumpfile_path = "/tmp/dumpfile/dumpfilelist" -nr_threads = 1 # [1, 256] +nr_worker_thread = 1 # [1, 256] cpu_mask = [5, 6, 7, 8, 9, 10, 11, 12] [ip_reassembly] @@ -29,40 +29,39 @@ tcp_overload_evict_old_sess = 1 # 1: evict old session, 0: bypass new session udp_overload_evict_old_sess = 1 # 1: evict old session, 0: bypass new session # TCP timeout -tcp_init_timeout = 50 # range: [1, 60000] (ms) -tcp_handshake_timeout = 50 # range: [1, 60000] (ms) -tcp_data_timeout = 50 # range: [1, 15999999000] (ms) -tcp_half_closed_timeout = 50 # range: [1, 604800000] (ms) -tcp_time_wait_timeout = 50 # range: [1, 600000] (ms) -tcp_discard_timeout = 10 # range: [1, 15999999000] (ms) -tcp_unverified_rst_timeout = 50 # range: [1, 600000] (ms) +tcp_init_timeout_ms = 50 # range: [1, 60000] (ms) +tcp_handshake_timeout_ms = 50 # range: [1, 60000] (ms) +tcp_data_timeout_ms = 50 # range: [1, 15999999000] (ms) +tcp_half_closed_timeout_ms = 50 # range: [1, 604800000] (ms) +tcp_time_wait_timeout_ms = 50 # range: [1, 600000] (ms) +tcp_discard_timeout_ms = 10 # range: [1, 15999999000] (ms) +tcp_unverified_rst_timeout_ms = 50 # range: [1, 600000] (ms) # UDP timeout -udp_data_timeout = 50 # range: [1, 15999999000] (ms) -udp_discard_timeout = 50 # range: [1, 15999999000] (ms) +udp_data_timeout_ms = 50 # range: [1, 15999999000] (ms) +udp_discard_timeout_ms = 50 # range: [1, 15999999000] (ms) + +# limit +session_expire_polling_interval_ms = 0 # range: [0, 60000] (ms) +session_expire_polling_limit = 1024 # range: [1, 1024] # duplicate packet filter duplicated_packet_filter_enable = 1 duplicated_packet_filter_capacity = 1000000 # range: [1, 4294967295] -duplicated_packet_filter_timeout = 10000 # range: [1, 60000] (ms) +duplicated_packet_filter_timeout_ms = 10000 # range: [1, 60000] (ms) duplicated_packet_filter_error_rate = 0.00001 # range: [0.0, 1.0] # evicted session filter evicted_session_filter_enable = 1 evicted_session_filter_capacity = 1000000 # range: [1, 4294967295] -evicted_session_filter_timeout = 10000 # range: [1, 60000] (ms) +evicted_session_filter_timeout_ms = 10000 # range: [1, 60000] (ms) evicted_session_filter_error_rate = 0.00001 # range: [0.0, 1.0] # TCP reassembly (Per direction) tcp_reassembly_enable = 1 -tcp_reassembly_max_timeout = 10000 # range: [1, 60000] (ms) -tcp_reassembly_max_segments = 256 # range: [2, 4096] +tcp_reassembly_max_timeout_ms = 10000 # range: [1, 60000] (ms) +tcp_reassembly_max_segments = 256 # range: [2, 4096] [schedule] -# Note: free_expired_session_interval determines the precision of session_manager timeout -free_expired_session_interval = 50 # range: [1, 60000] (ms) -free_expired_session_batch = 100 # range: [1, 60000] -force_session_expire_before_exit = 0 # 1: force session to expire before exit, 0: wait for session to naturally expire before exit. - # Note: free_expired_ip_frag_interval determines the precision of ip_reassembly timeout free_expired_ip_frag_interval = 50 # range: [1, 60000] (ms) free_expired_ip_frag_batch = 100 # range: [1, 60000] diff --git a/test/packet_inject/conf/stellar.toml b/test/packet_inject/conf/stellar.toml index 109e6cb..3860757 100644 --- a/test/packet_inject/conf/stellar.toml +++ b/test/packet_inject/conf/stellar.toml @@ -10,7 +10,7 @@ dev_symbol = "nf_0_fw" dumpfile_path = "/tmp/dumpfile/dumpfile.pcap" #dumpfile_path = "/tmp/dumpfile/dumpfilelist" -nr_threads = 1 # [1, 256] +nr_worker_thread = 1 # [1, 256] cpu_mask = [5] [ip_reassembly] @@ -29,40 +29,39 @@ tcp_overload_evict_old_sess = 1 # 1: evict old session, 0: bypass new session udp_overload_evict_old_sess = 1 # 1: evict old session, 0: bypass new session # TCP timeout -tcp_init_timeout = 50 # range: [1, 60000] (ms) -tcp_handshake_timeout = 50 # range: [1, 60000] (ms) -tcp_data_timeout = 50 # range: [1, 15999999000] (ms) -tcp_half_closed_timeout = 50 # range: [1, 604800000] (ms) -tcp_time_wait_timeout = 50 # range: [1, 600000] (ms) -tcp_discard_timeout = 50 # range: [1, 15999999000] (ms) -tcp_unverified_rst_timeout = 50 # range: [1, 600000] (ms) +tcp_init_timeout_ms = 50 # range: [1, 60000] (ms) +tcp_handshake_timeout_ms = 50 # range: [1, 60000] (ms) +tcp_data_timeout_ms = 50 # range: [1, 15999999000] (ms) +tcp_half_closed_timeout_ms = 50 # range: [1, 604800000] (ms) +tcp_time_wait_timeout_ms = 50 # range: [1, 600000] (ms) +tcp_discard_timeout_ms = 50 # range: [1, 15999999000] (ms) +tcp_unverified_rst_timeout_ms = 50 # range: [1, 600000] (ms) # UDP timeout -udp_data_timeout = 50 # range: [1, 15999999000] (ms) -udp_discard_timeout = 50 # range: [1, 15999999000] (ms) +udp_data_timeout_ms = 50 # range: [1, 15999999000] (ms) +udp_discard_timeout_ms = 50 # range: [1, 15999999000] (ms) + +# limit +session_expire_polling_interval_ms = 0 # range: [0, 60000] (ms) +session_expire_polling_limit = 1024 # range: [1, 1024] # duplicate packet filter duplicated_packet_filter_enable = 1 duplicated_packet_filter_capacity = 1000000 # range: [1, 4294967295] -duplicated_packet_filter_timeout = 100 # range: [1, 60000] (ms) +duplicated_packet_filter_timeout_ms = 100 # range: [1, 60000] (ms) duplicated_packet_filter_error_rate = 0.00001 # range: [0.0, 1.0] # evicted session filter evicted_session_filter_enable = 1 evicted_session_filter_capacity = 1000000 # range: [1, 4294967295] -evicted_session_filter_timeout = 100 # range: [1, 60000] (ms) +evicted_session_filter_timeout_ms = 100 # range: [1, 60000] (ms) evicted_session_filter_error_rate = 0.00001 # range: [0.0, 1.0] # TCP reassembly (Per direction) tcp_reassembly_enable = 1 -tcp_reassembly_max_timeout = 10000 # range: [1, 60000] (ms) -tcp_reassembly_max_segments = 128 # range: [2, 4096] +tcp_reassembly_max_timeout_ms = 10000 # range: [1, 60000] (ms) +tcp_reassembly_max_segments = 128 # range: [2, 4096] [schedule] -# Note: free_expired_session_interval determines the precision of session_manager timeout -free_expired_session_interval = 50 # range: [1, 60000] (ms) -free_expired_session_batch = 1000 # range: [1, 60000] -force_session_expire_before_exit = 0 # 1: force session to expire before exit, 0: wait for session to naturally expire before exit. - # Note: free_expired_ip_frag_interval determines the precision of ip_reassembly timeout free_expired_ip_frag_interval = 50 # range: [1, 60000] (ms) free_expired_ip_frag_batch = 1000 # range: [1, 60000]