reactor packet I/O & duplicated packet filter & evicted session filter

This commit is contained in:
luwenpeng
2024-03-09 19:28:14 +08:00
parent ee35a26a9d
commit 512dfddd03
79 changed files with 1974 additions and 2093 deletions

View File

@@ -7,53 +7,53 @@
// return 0: success
// retuun -1: failed
static int parse_device_options(struct device_options *dev_opts, toml_table_t *conf_file_handle)
static int parse_device_options(toml_table_t *table, struct device_options *opts)
{
const char *ptr;
toml_table_t *device_table;
toml_table_t *device;
device_table = toml_table_in(conf_file_handle, "device");
if (device_table == NULL)
device = toml_table_in(table, "device");
if (device == NULL)
{
CONFIG_LOG_ERROR("config file missing device section");
return -1;
}
ptr = toml_raw_in(device_table, "device_base");
ptr = toml_raw_in(device, "device_base");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing device.device_base");
return -1;
}
dev_opts->device_base = atoi(ptr);
opts->device_base = atoi(ptr);
ptr = toml_raw_in(device_table, "device_offset");
ptr = toml_raw_in(device, "device_offset");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing device.device_offset");
return -1;
}
dev_opts->device_offset = atoi(ptr);
opts->device_offset = atoi(ptr);
return 0;
}
// return 0: success
// retuun -1: failed
static int parse_packet_io_options(struct packet_io_options *pkt_io_opts, toml_table_t *conf_file_handle)
static int parse_packet_io_options(toml_table_t *table, struct packet_io_options *opts)
{
const char *ptr;
toml_table_t *packet_io_table;
toml_table_t *packet_io;
toml_array_t *mask_array;
packet_io_table = toml_table_in(conf_file_handle, "packet_io");
if (packet_io_table == NULL)
packet_io = toml_table_in(table, "packet_io");
if (packet_io == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io section");
return -1;
}
ptr = toml_raw_in(packet_io_table, "mode");
ptr = toml_raw_in(packet_io, "mode");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io.mode");
@@ -61,11 +61,11 @@ static int parse_packet_io_options(struct packet_io_options *pkt_io_opts, toml_t
}
if (strcmp(ptr, "dumpfile") == 0)
{
pkt_io_opts->mode = PACKET_IO_DUMPFILE;
opts->mode = PACKET_IO_DUMPFILE;
}
else if (strcmp(ptr, "marsio") == 0)
{
pkt_io_opts->mode = PACKET_IO_MARSIO;
opts->mode = PACKET_IO_MARSIO;
}
else
{
@@ -73,37 +73,37 @@ static int parse_packet_io_options(struct packet_io_options *pkt_io_opts, toml_t
return -1;
}
if (pkt_io_opts->mode == PACKET_IO_DUMPFILE)
if (opts->mode == PACKET_IO_DUMPFILE)
{
ptr = toml_raw_in(packet_io_table, "dumpfile_dir");
ptr = toml_raw_in(packet_io, "dumpfile_dir");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io.dumpfile_dir");
return -1;
}
// skip ""
strncpy(pkt_io_opts->dumpfile_dir, ptr + 1, strlen(ptr) - 2);
strncpy(opts->dumpfile_dir, ptr + 1, strlen(ptr) - 2);
}
else
{
ptr = toml_raw_in(packet_io_table, "app_symbol");
ptr = toml_raw_in(packet_io, "app_symbol");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io.app_symbol");
return -1;
}
strncpy(pkt_io_opts->app_symbol, ptr, sizeof(pkt_io_opts->app_symbol) - 1);
strncpy(opts->app_symbol, ptr, sizeof(opts->app_symbol) - 1);
ptr = toml_raw_in(packet_io_table, "dev_symbol");
ptr = toml_raw_in(packet_io, "dev_symbol");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io.dev_symbol");
return -1;
}
strncpy(pkt_io_opts->dev_symbol, ptr, sizeof(pkt_io_opts->dev_symbol) - 1);
strncpy(opts->dev_symbol, ptr, sizeof(opts->dev_symbol) - 1);
}
ptr = toml_raw_in(packet_io_table, "nr_threads");
ptr = toml_raw_in(packet_io, "nr_threads");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io.nr_threads");
@@ -114,15 +114,15 @@ static int parse_packet_io_options(struct packet_io_options *pkt_io_opts, toml_t
CONFIG_LOG_ERROR("config file invalid packet_io.nr_threads %d, range [1, %d]", atoi(ptr), MAX_THREAD_NUM);
return -1;
}
pkt_io_opts->nr_threads = atoi(ptr);
opts->nr_threads = atoi(ptr);
mask_array = toml_array_in(packet_io_table, "cpu_mask");
mask_array = toml_array_in(packet_io, "cpu_mask");
if (mask_array == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io.cpu_mask");
return -1;
}
for (uint8_t i = 0; i < pkt_io_opts->nr_threads; i++)
for (uint8_t i = 0; i < opts->nr_threads; i++)
{
ptr = toml_raw_at(mask_array, i);
if (ptr == NULL)
@@ -130,7 +130,7 @@ static int parse_packet_io_options(struct packet_io_options *pkt_io_opts, toml_t
CONFIG_LOG_ERROR("config file missing packet_io.cpu_mask[%d]", i);
return -1;
}
pkt_io_opts->cpu_mask[i] = atoi(ptr);
opts->cpu_mask[i] = atoi(ptr);
}
return 0;
@@ -138,269 +138,269 @@ static int parse_packet_io_options(struct packet_io_options *pkt_io_opts, toml_t
// return 0: success
// retuun -1: failed
static int parse_ip_reassembly_options(struct ip_reassembly_options *ip_reass_opts, toml_table_t *conf_file_handle)
static int parse_ip_reassembly_options(toml_table_t *table, struct ip_reassembly_options *opts)
{
const char *ptr;
toml_table_t *ip_reass_table;
toml_table_t *ip_reassembly;
ip_reass_table = toml_table_in(conf_file_handle, "ip_reassembly");
if (ip_reass_table == NULL)
ip_reassembly = toml_table_in(table, "ip_reassembly");
if (ip_reassembly == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly section");
return -1;
}
ptr = toml_raw_in(ip_reass_table, "enable");
ptr = toml_raw_in(ip_reassembly, "enable");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly.enable");
return -1;
}
ip_reass_opts->enable = atoi(ptr);
opts->enable = atoi(ptr);
ptr = toml_raw_in(ip_reass_table, "timeout");
ptr = toml_raw_in(ip_reassembly, "timeout");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly.timeout");
return -1;
}
ip_reass_opts->timeout = atoi(ptr);
opts->timeout = atoi(ptr);
ptr = toml_raw_in(ip_reass_table, "bucket_entries");
ptr = toml_raw_in(ip_reassembly, "bucket_entries");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly.bucket_entries");
return -1;
}
ip_reass_opts->bucket_entries = atoi(ptr);
opts->bucket_entries = atoi(ptr);
ptr = toml_raw_in(ip_reass_table, "bucket_num");
ptr = toml_raw_in(ip_reassembly, "bucket_num");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing ip_reassembly.bucket_num");
return -1;
}
ip_reass_opts->bucket_num = atoi(ptr);
opts->bucket_num = atoi(ptr);
return 0;
}
// return 0: success
// retuun -1: failed
static int parse_session_manager_options(struct session_manager_options *sess_mgr_opts, toml_table_t *conf_file_handle)
static int parse_session_manager_options(toml_table_t *table, struct session_manager_options *opts)
{
const char *ptr;
toml_table_t *sess_mgr_table;
toml_table_t *session_manager;
sess_mgr_table = toml_table_in(conf_file_handle, "session_manager");
if (sess_mgr_table == NULL)
session_manager = toml_table_in(table, "session_manager");
if (session_manager == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager section");
return -1;
}
// max session number
ptr = toml_raw_in(sess_mgr_table, "max_tcp_session_num");
ptr = toml_raw_in(session_manager, "max_tcp_session_num");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.max_tcp_session_num");
return -1;
}
sess_mgr_opts->max_tcp_session_num = atoll(ptr);
opts->max_tcp_session_num = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "max_udp_session_num");
ptr = toml_raw_in(session_manager, "max_udp_session_num");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.max_udp_session_num");
return -1;
}
sess_mgr_opts->max_udp_session_num = atoll(ptr);
opts->max_udp_session_num = atoll(ptr);
// session overload (1: evict old session, 0: bypass new session)
ptr = toml_raw_in(sess_mgr_table, "tcp_overload_evict_old_sess");
ptr = toml_raw_in(session_manager, "tcp_overload_evict_old_sess");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_overload_evict_old_sess");
return -1;
}
sess_mgr_opts->tcp_overload_evict_old_sess = atoi(ptr);
opts->tcp_overload_evict_old_sess = atoi(ptr);
ptr = toml_raw_in(sess_mgr_table, "udp_overload_evict_old_sess");
ptr = toml_raw_in(session_manager, "udp_overload_evict_old_sess");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.udp_overload_evict_old_sess");
return -1;
}
sess_mgr_opts->udp_overload_evict_old_sess = atoi(ptr);
opts->udp_overload_evict_old_sess = atoi(ptr);
// TCP timeout
ptr = toml_raw_in(sess_mgr_table, "tcp_timeout_init");
ptr = toml_raw_in(session_manager, "tcp_timeout_init");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_timeout_init");
return -1;
}
sess_mgr_opts->tcp_timeout_init = atoll(ptr);
opts->tcp_timeout_init = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_timeout_handshake");
ptr = toml_raw_in(session_manager, "tcp_timeout_handshake");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_timeout_handshake");
return -1;
}
sess_mgr_opts->tcp_timeout_handshake = atoll(ptr);
opts->tcp_timeout_handshake = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_timeout_data");
ptr = toml_raw_in(session_manager, "tcp_timeout_data");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_timeout_data");
return -1;
}
sess_mgr_opts->tcp_timeout_data = atoll(ptr);
opts->tcp_timeout_data = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_timeout_half_closed");
ptr = toml_raw_in(session_manager, "tcp_timeout_half_closed");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_timeout_half_closed");
return -1;
}
sess_mgr_opts->tcp_timeout_half_closed = atoll(ptr);
opts->tcp_timeout_half_closed = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_timeout_time_wait");
ptr = toml_raw_in(session_manager, "tcp_timeout_time_wait");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_timeout_time_wait");
return -1;
}
sess_mgr_opts->tcp_timeout_time_wait = atoll(ptr);
opts->tcp_timeout_time_wait = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_timeout_discard");
ptr = toml_raw_in(session_manager, "tcp_timeout_discard");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_timeout_discard");
return -1;
}
sess_mgr_opts->tcp_timeout_discard = atoll(ptr);
opts->tcp_timeout_discard = atoll(ptr);
// UDP timeout
ptr = toml_raw_in(sess_mgr_table, "udp_timeout_data");
ptr = toml_raw_in(session_manager, "udp_timeout_data");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.udp_timeout_data");
return -1;
}
sess_mgr_opts->udp_timeout_data = atoll(ptr);
opts->udp_timeout_data = atoll(ptr);
// TCP duplicate packet filter
ptr = toml_raw_in(sess_mgr_table, "tcp_dupkt_filter_enable");
// duplicate packet filter
ptr = toml_raw_in(session_manager, "duplicated_packet_filter_enable");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_dupkt_filter_enable");
CONFIG_LOG_ERROR("config file missing session_manager.duplicated_packet_filter_enable");
return -1;
}
sess_mgr_opts->tcp_dupkt_filter_enable = atoi(ptr);
opts->duplicated_packet_filter_enable = atoi(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_dupkt_filter_capacity");
ptr = toml_raw_in(session_manager, "duplicated_packet_filter_capacity");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_dupkt_filter_capacity");
CONFIG_LOG_ERROR("config file missing session_manager.duplicated_packet_filter_capacity");
return -1;
}
sess_mgr_opts->tcp_dupkt_filter_capacity = atoll(ptr);
opts->duplicated_packet_filter_capacity = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_dupkt_filter_timeout");
ptr = toml_raw_in(session_manager, "duplicated_packet_filter_timeout");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_dupkt_filter_timeout");
CONFIG_LOG_ERROR("config file missing session_manager.duplicated_packet_filter_timeout");
return -1;
}
sess_mgr_opts->tcp_dupkt_filter_timeout = atoll(ptr);
opts->duplicated_packet_filter_timeout = atoll(ptr);
ptr = toml_raw_in(sess_mgr_table, "tcp_dupkt_filter_error_rate");
ptr = toml_raw_in(session_manager, "duplicated_packet_filter_error_rate");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.tcp_dupkt_filter_error_rate");
CONFIG_LOG_ERROR("config file missing session_manager.duplicated_packet_filter_error_rate");
return -1;
}
sess_mgr_opts->tcp_dupkt_filter_error_rate = atof(ptr);
opts->duplicated_packet_filter_error_rate = atof(ptr);
// UDP eviction filter
ptr = toml_raw_in(sess_mgr_table, "udp_eviction_filter_enable");
// eviction filter
ptr = toml_raw_in(session_manager, "evicted_session_filter_enable");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.udp_eviction_filter_enable");
CONFIG_LOG_ERROR("config file missing session_manager.evicted_session_filter_enable");
return -1;
}
sess_mgr_opts->udp_eviction_filter_enable = atoi(ptr);
opts->evicted_session_filter_enable = atoi(ptr);
ptr = toml_raw_in(sess_mgr_table, "udp_eviction_filter_capacity");
ptr = toml_raw_in(session_manager, "evicted_session_filter_capacity");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.udp_eviction_filter_capacity");
CONFIG_LOG_ERROR("config file missing session_manager.evicted_session_filter_capacity");
return -1;
}
sess_mgr_opts->udp_eviction_filter_capacity = atoll(ptr);
opts->evicted_session_filter_capacity = atoi(ptr);
ptr = toml_raw_in(sess_mgr_table, "udp_eviction_filter_timeout");
ptr = toml_raw_in(session_manager, "evicted_session_filter_timeout");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.udp_eviction_filter_timeout");
CONFIG_LOG_ERROR("config file missing session_manager.evicted_session_filter_timeout");
return -1;
}
sess_mgr_opts->udp_eviction_filter_timeout = atoll(ptr);
opts->evicted_session_filter_timeout = atoi(ptr);
ptr = toml_raw_in(sess_mgr_table, "udp_eviction_filter_error_rate");
ptr = toml_raw_in(session_manager, "evicted_session_filter_error_rate");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing session_manager.udp_eviction_filter_error_rate");
CONFIG_LOG_ERROR("config file missing session_manager.evicted_session_filter_error_rate");
return -1;
}
sess_mgr_opts->udp_eviction_filter_error_rate = atof(ptr);
opts->evicted_session_filter_error_rate = atof(ptr);
return 0;
}
// return 0: success
// retuun -1: failed
int config_load(struct config *cfg, const char *cfg_file)
int parse_config_file(const char *file, struct config *config)
{
int ret = -1;
char errbuf[200];
FILE *fp = NULL;
toml_table_t *conf_file_handle = NULL;
toml_table_t *table = NULL;
memset(cfg, 0, sizeof(*cfg));
memset(config, 0, sizeof(*config));
fp = fopen(cfg_file, "r");
fp = fopen(file, "r");
if (fp == NULL)
{
CONFIG_LOG_ERROR("open config file %s failed, %s", cfg_file, strerror(errno));
CONFIG_LOG_ERROR("open config file %s failed, %s", file, strerror(errno));
goto error_out;
}
conf_file_handle = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (conf_file_handle == NULL)
table = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (table == NULL)
{
CONFIG_LOG_ERROR("parse config file %s failed, %s", cfg_file, errbuf);
CONFIG_LOG_ERROR("parse config file %s failed, %s", file, errbuf);
goto error_out;
}
if (parse_device_options(&cfg->dev_opts, conf_file_handle) != 0)
if (parse_device_options(table, &config->dev_opts) != 0)
{
goto error_out;
}
if (parse_packet_io_options(&cfg->pkt_io_opts, conf_file_handle) != 0)
if (parse_packet_io_options(table, &config->pkt_io_opts) != 0)
{
goto error_out;
}
if (parse_ip_reassembly_options(&cfg->ip_reass_opts, conf_file_handle) != 0)
if (parse_ip_reassembly_options(table, &config->ip_reass_opts) != 0)
{
goto error_out;
}
if (parse_session_manager_options(&cfg->sess_mgr_opts, conf_file_handle) != 0)
if (parse_session_manager_options(table, &config->sess_mgr_opts) != 0)
{
goto error_out;
}
@@ -408,9 +408,9 @@ int config_load(struct config *cfg, const char *cfg_file)
ret = 0;
error_out:
if (conf_file_handle)
if (table)
{
toml_free(conf_file_handle);
toml_free(table);
}
if (fp)
@@ -421,17 +421,17 @@ error_out:
return ret;
}
void config_dump(struct config *cfg)
void print_config_options(struct config *config)
{
if (cfg == NULL)
if (config == NULL)
{
return;
}
struct device_options *dev_opts = &cfg->dev_opts;
struct packet_io_options *pkt_io_opts = &cfg->pkt_io_opts;
struct ip_reassembly_options *ip_reass_opts = &cfg->ip_reass_opts;
struct session_manager_options *sess_mgr_opts = &cfg->sess_mgr_opts;
struct device_options *dev_opts = &config->dev_opts;
struct packet_io_options *pkt_io_opts = &config->pkt_io_opts;
struct ip_reassembly_options *ip_reass_opts = &config->ip_reass_opts;
struct session_manager_options *sess_mgr_opts = &config->sess_mgr_opts;
// device config
CONFIG_LOG_DEBUG("device->device_base : %d", dev_opts->device_base);
@@ -476,13 +476,13 @@ void config_dump(struct config *cfg)
CONFIG_LOG_DEBUG("session_manager->udp_timeout_data : %ld", sess_mgr_opts->udp_timeout_data);
CONFIG_LOG_DEBUG("session_manager->tcp_dupkt_filter_enable : %d", sess_mgr_opts->tcp_dupkt_filter_enable);
CONFIG_LOG_DEBUG("session_manager->tcp_dupkt_filter_capacity : %ld", sess_mgr_opts->tcp_dupkt_filter_capacity);
CONFIG_LOG_DEBUG("session_manager->tcp_dupkt_filter_timeout : %ld", sess_mgr_opts->tcp_dupkt_filter_timeout);
CONFIG_LOG_DEBUG("session_manager->tcp_dupkt_filter_error_rate : %f", sess_mgr_opts->tcp_dupkt_filter_error_rate);
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);
CONFIG_LOG_DEBUG("session_manager->udp_eviction_filter_enable : %d", sess_mgr_opts->udp_eviction_filter_enable);
CONFIG_LOG_DEBUG("session_manager->udp_eviction_filter_capacity : %ld", sess_mgr_opts->udp_eviction_filter_capacity);
CONFIG_LOG_DEBUG("session_manager->udp_eviction_filter_timeout : %ld", sess_mgr_opts->udp_eviction_filter_timeout);
CONFIG_LOG_DEBUG("session_manager->udp_eviction_filter_error_rate : %f", sess_mgr_opts->udp_eviction_filter_error_rate);
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);
}