IP reassembly parses IP frag related configuration items

This commit is contained in:
luwenpeng
2024-08-29 18:03:14 +08:00
parent 338dcf93e5
commit 8935e5408b
10 changed files with 253 additions and 225 deletions

View File

@@ -168,55 +168,6 @@ error_out:
return ret;
}
// return 0: success
// retuun -1: failed
static int parse_ip_reassembly_section(toml_table_t *root, struct ip_reassembly_options *opts)
{
const char *ptr;
toml_table_t *table;
table = toml_table_in(root, "ip_reassembly");
if (table == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly section");
return -1;
}
ptr = toml_raw_in(table, "enable");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly->enable");
return -1;
}
opts->enable = atoi(ptr);
ptr = toml_raw_in(table, "timeout");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly->timeout");
return -1;
}
opts->timeout = atoi(ptr);
ptr = toml_raw_in(table, "bucket_entries");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly->bucket_entries");
return -1;
}
opts->bucket_entries = atoi(ptr);
ptr = toml_raw_in(table, "bucket_num");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly->bucket_num");
return -1;
}
opts->bucket_num = atoi(ptr);
return 0;
}
// return 0: success
// retuun -1: failed
static int parse_schedule_options(toml_table_t *root, struct schedule_options *opts)
@@ -231,32 +182,6 @@ static int parse_schedule_options(toml_table_t *root, struct schedule_options *o
return -1;
}
ptr = toml_raw_in(table, "free_expired_ip_frag_interval");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing schedule->free_expired_ip_frag_interval");
return -1;
}
opts->free_expired_ip_frag_interval = atoll(ptr);
if (opts->free_expired_ip_frag_interval < 1 || opts->free_expired_ip_frag_interval > 60000)
{
CONFIG_LOG_ERROR("config file invalid schedule->free_expired_ip_frag_interval %ld, range [1, 60000]", opts->free_expired_ip_frag_interval);
return -1;
}
ptr = toml_raw_in(table, "free_expired_ip_frag_batch");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing schedule->free_expired_ip_frag_batch");
return -1;
}
opts->free_expired_ip_frag_batch = atoll(ptr);
if (opts->free_expired_ip_frag_batch < 1 || opts->free_expired_ip_frag_batch > 60000)
{
CONFIG_LOG_ERROR("config file invalid schedule->free_expired_ip_frag_batch %ld, range [1, 60000]", opts->free_expired_ip_frag_batch);
return -1;
}
ptr = toml_raw_in(table, "merge_stat_interval");
if (ptr == NULL)
{
@@ -332,11 +257,6 @@ int stellar_config_load(struct stellar_config *config, const char *file)
goto error_out;
}
if (parse_ip_reassembly_section(table, &config->ip_reass_opts) != 0)
{
goto error_out;
}
if (parse_schedule_options(table, &config->sched_opts) != 0)
{
goto error_out;
@@ -367,7 +287,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;
// snowflake config
CONFIG_LOG_DEBUG("snowflake->snowflake_base : %d", snowflake_opts->snowflake_base);
@@ -390,15 +309,7 @@ void stellar_config_print(const struct stellar_config *config)
CONFIG_LOG_DEBUG("packet_io->cpu_mask[%3d] : %d", i, pkt_io_opts->cpu_mask[i]);
}
// ip reassemble config
CONFIG_LOG_DEBUG("ip_reassembly->enable : %d", ip_reass_opts->enable);
CONFIG_LOG_DEBUG("ip_reassembly->timeout : %d", ip_reass_opts->timeout);
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);
// schedule config
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);
CONFIG_LOG_DEBUG("schedule->output_stat_interval : %ld", config->sched_opts.output_stat_interval);
CONFIG_LOG_DEBUG("schedule->packet_io_yield_interval : %ld", config->sched_opts.packet_io_yield_interval);

View File

@@ -6,14 +6,9 @@ extern "C"
#endif
#include "packet_io.h"
#include "ip_reassembly.h"
struct schedule_options
{
// 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]
uint64_t merge_stat_interval; // range: [1, 60000] (ms)
uint64_t output_stat_interval; // range: [1, 60000] (ms)
@@ -30,7 +25,6 @@ struct stellar_config
{
struct packet_io_options pkt_io_opts;
struct snowflake_options snowflake_opts;
struct ip_reassembly_options ip_reass_opts;
struct schedule_options sched_opts;
};

View File

@@ -42,10 +42,9 @@ struct stellar_thread
pthread_t tid;
uint16_t idx;
uint64_t is_runing;
uint64_t last_free_expired_ip_frag_timestamp;
uint64_t last_merge_thread_stat_timestamp;
struct snowflake *snowflake;
struct ip_reassembly *ip_mgr;
struct ip_reassembly *ip_reass;
struct session_manager *sess_mgr;
struct stellar *st;
};
@@ -60,6 +59,7 @@ struct stellar_runtime
struct plugin_manager_schema *plug_mgr;
struct stellar_thread threads[MAX_THREAD_NUM];
struct session_manager_config *sess_mgr_cfg;
struct ip_reassembly_config *ip_reass_cfg;
};
struct stellar
@@ -130,7 +130,7 @@ static void *worker_thread(void *arg)
struct packet packets[RX_BURST_MAX];
struct session *sess = NULL;
struct stellar_thread *thread = (struct stellar_thread *)arg;
struct ip_reassembly *ip_reass = thread->ip_mgr;
struct ip_reassembly *ip_reass = thread->ip_reass;
struct session_manager *sess_mgr = thread->sess_mgr;
struct session_manager_stat *sess_stat = session_manager_stat(sess_mgr);
struct stellar *st = thread->st;
@@ -144,8 +144,6 @@ static void *worker_thread(void *arg)
.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;
uint64_t packet_io_yield_interval = config->sched_opts.packet_io_yield_interval;
uint16_t thr_idx = thread->idx;
@@ -290,6 +288,7 @@ static void *worker_thread(void *arg)
idle_tasks:
clean_session(sess_mgr, now_ms);
ip_reassembly_expire(ip_reass, now_ms);
plugin_manager_on_polling(plug_mgr);
// per merge_stat_interval merge thread stat
@@ -299,13 +298,6 @@ static void *worker_thread(void *arg)
thread->last_merge_thread_stat_timestamp = now_ms;
}
// per free_expired_ip_frag_interval MAX free_expired_ip_frag_batch ip fragments are released
if (now_ms - thread->last_free_expired_ip_frag_timestamp >= free_expired_ip_frag_interval)
{
ip_reassembly_expire(ip_reass, free_expired_ip_frag_batch, now_ms);
thread->last_free_expired_ip_frag_timestamp = now_ms;
}
if (nr_pkt_received == 0)
{
packet_io_yield(packet_io, thr_idx, packet_io_yield_interval);
@@ -359,7 +351,6 @@ static int stellar_thread_init(struct stellar *st)
thread->idx = i;
thread->is_runing = 0;
thread->last_free_expired_ip_frag_timestamp = now_ms;
thread->last_merge_thread_stat_timestamp = now_ms;
thread->snowflake = snowflake_new(i, config->snowflake_opts.snowflake_base, config->snowflake_opts.snowflake_offset);
@@ -368,6 +359,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(runtime->sess_mgr_cfg, now_ms);
if (thread->sess_mgr == NULL)
{
@@ -375,8 +367,9 @@ static int stellar_thread_init(struct stellar *st)
return -1;
}
session_manager_set_session_id_generator(thread->sess_mgr, stellar_generate_session_id);
thread->ip_mgr = ip_reassembly_new(&config->ip_reass_opts);
if (thread->ip_mgr == NULL)
thread->ip_reass = ip_reassembly_new(runtime->ip_reass_cfg, now_ms);
if (thread->ip_reass == NULL)
{
CORE_LOG_ERROR("unable to create ip reassemble manager");
return -1;
@@ -398,7 +391,7 @@ static void stellar_thread_clean(struct stellar *st)
struct stellar_thread *thread = &runtime->threads[i];
if (ATOMIC_READ(&thread->is_runing) == 0)
{
ip_reassembly_free(thread->ip_mgr);
ip_reassembly_free(thread->ip_reass);
session_manager_free(thread->sess_mgr);
snowflake_free(thread->snowflake);
}
@@ -490,8 +483,15 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg
CORE_LOG_ERROR("unable to create session manager config");
goto error_out;
}
session_manager_config_print(runtime->sess_mgr_cfg);
runtime->ip_reass_cfg = ip_reassembly_config_new(st->stellar_cfg_file);
if (runtime->ip_reass_cfg == NULL)
{
CORE_LOG_ERROR("unable to create ip reassembly config");
goto error_out;
}
session_manager_config_print(runtime->sess_mgr_cfg);
ip_reassembly_config_print(runtime->ip_reass_cfg);
if (stellar_config_load(config, st->stellar_cfg_file) != 0)
{
CORE_LOG_ERROR("unable to load config file");
@@ -587,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);
ip_reassembly_config_free(runtime->ip_reass_cfg);
session_manager_config_free(runtime->sess_mgr_cfg);
CORE_LOG_FATAL("stellar exit\n");
log_free(runtime->logger);