#include #include #include #include "log.h" #include "toml.h" #include "stellar_config.h" #define CONFIG_LOG_ERROR(format, ...) LOG_ERROR("config", format, ##__VA_ARGS__) #define CONFIG_LOG_DEBUG(format, ...) LOG_DEBUG("config", format, ##__VA_ARGS__) // return 0: success // retuun -1: failed static int parse_snowflake_section(toml_table_t *root, struct snowflake_options *opts) { const char *ptr; toml_table_t *table; table = toml_table_in(root, "snowflake"); if (table == NULL) { CONFIG_LOG_ERROR("config file missing snowflake section"); return -1; } ptr = toml_raw_in(table, "snowflake_base"); if (ptr == NULL) { CONFIG_LOG_ERROR("config file missing snowflake->snowflake_base"); return -1; } opts->snowflake_base = atoi(ptr); ptr = toml_raw_in(table, "snowflake_offset"); if (ptr == NULL) { CONFIG_LOG_ERROR("config file missing snowflake->snowflake_offset"); return -1; } opts->snowflake_offset = atoi(ptr); return 0; } // return 0: success // retuun -1: failed static int parse_packet_io_section(toml_table_t *root, struct packet_io_options *opts) { int ret = -1; char *ptr_mode = NULL; char *ptr_dumpfile_path = NULL; char *ptr_app_symbol = NULL; char *ptr_dev_symbol = NULL; const char *ptr; toml_table_t *table; toml_array_t *mask_array; table = toml_table_in(root, "packet_io"); if (table == NULL) { CONFIG_LOG_ERROR("config file missing packet_io section"); goto error_out; } ptr = toml_raw_in(table, "mode"); if (ptr == NULL || toml_rtos(ptr, &ptr_mode) != 0) { CONFIG_LOG_ERROR("config file missing packet_io->mode"); goto error_out; } if (strcmp(ptr_mode, "dumpfile") == 0) { opts->mode = PACKET_IO_DUMPFILE; } else if (strcmp(ptr_mode, "dumpfilelist") == 0) { opts->mode = PACKET_IO_DUMPFILELIST; } else if (strcmp(ptr_mode, "marsio") == 0) { opts->mode = PACKET_IO_MARSIO; } else { CONFIG_LOG_ERROR("config file invalid packet_io->mode %s, only support dumpfile and marsio", ptr); goto error_out; } if (opts->mode == PACKET_IO_DUMPFILE || opts->mode == PACKET_IO_DUMPFILELIST) { ptr = toml_raw_in(table, "dumpfile_path"); if (ptr == NULL || toml_rtos(ptr, &ptr_dumpfile_path) != 0) { CONFIG_LOG_ERROR("config file missing packet_io->dumpfile_path"); goto error_out; } strcpy(opts->dumpfile_path, ptr_dumpfile_path); } else { ptr = toml_raw_in(table, "app_symbol"); if (ptr == NULL || toml_rtos(ptr, &ptr_app_symbol) != 0) { CONFIG_LOG_ERROR("config file missing packet_io->app_symbol"); goto error_out; } strcpy(opts->app_symbol, ptr_app_symbol); ptr = toml_raw_in(table, "dev_symbol"); if (ptr == NULL || toml_rtos(ptr, &ptr_dev_symbol) != 0) { CONFIG_LOG_ERROR("config file missing packet_io->dev_symbol"); goto error_out; } strcpy(opts->dev_symbol, ptr_dev_symbol); } ptr = toml_raw_in(table, "nr_threads"); if (ptr == NULL) { CONFIG_LOG_ERROR("config file missing packet_io->nr_threads"); 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); goto error_out; } opts->nr_threads = atoi(ptr); mask_array = toml_array_in(table, "cpu_mask"); if (mask_array == NULL) { CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask"); goto error_out; } for (uint16_t i = 0; i < opts->nr_threads; i++) { ptr = toml_raw_at(mask_array, i); if (ptr == NULL) { CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask[%d]", i); goto error_out; } opts->cpu_mask[i] = atoi(ptr); } ret = 0; error_out: if (ptr_mode) { free(ptr_mode); } if (ptr_dumpfile_path) { free(ptr_dumpfile_path); } if (ptr_app_symbol) { free(ptr_app_symbol); } if (ptr_dev_symbol) { free(ptr_dev_symbol); } 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_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) { const char *ptr; toml_table_t *table; table = toml_table_in(root, "schedule"); if (table == NULL) { CONFIG_LOG_ERROR("config file missing schedule section"); 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, "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) { CONFIG_LOG_ERROR("config file missing schedule->merge_stat_interval"); return -1; } opts->merge_stat_interval = atoll(ptr); if (opts->merge_stat_interval < 1 || opts->merge_stat_interval > 60000) { CONFIG_LOG_ERROR("config file invalid schedule->merge_stat_interval %ld, range [1, 60000]", opts->merge_stat_interval); return -1; } ptr = toml_raw_in(table, "output_stat_interval"); if (ptr == NULL) { CONFIG_LOG_ERROR("config file missing schedule->output_stat_interval"); return -1; } opts->output_stat_interval = atoll(ptr); if (opts->output_stat_interval < 1 || opts->output_stat_interval > 60000) { CONFIG_LOG_ERROR("config file invalid schedule->output_stat_interval %ld, range [1, 60000]", opts->output_stat_interval); return -1; } ptr = toml_raw_in(table, "packet_io_yield_interval"); if (ptr == NULL) { CONFIG_LOG_ERROR("config file missing schedule->packet_io_yield_interval"); return -1; } opts->packet_io_yield_interval = atoll(ptr); if (opts->packet_io_yield_interval < 1 || opts->packet_io_yield_interval > 60000) { CONFIG_LOG_ERROR("config file invalid schedule->packet_io_yield_interval %ld, range [1, 60000]", opts->packet_io_yield_interval); return -1; } return 0; } // return 0: success // retuun -1: failed int stellar_config_load(struct stellar_config *config, const char *file) { int ret = -1; char errbuf[200]; FILE *fp = NULL; toml_table_t *table = NULL; fp = fopen(file, "r"); if (fp == NULL) { CONFIG_LOG_ERROR("open config file %s failed, %s", file, strerror(errno)); goto error_out; } table = toml_parse_file(fp, errbuf, sizeof(errbuf)); if (table == NULL) { CONFIG_LOG_ERROR("parse config file %s failed, %s", file, errbuf); goto error_out; } if (parse_snowflake_section(table, &config->snowflake_opts) != 0) { goto error_out; } if (parse_packet_io_section(table, &config->pkt_io_opts) != 0) { goto error_out; } if (parse_ip_reassembly_section(table, &config->ip_reass_opts) != 0) { 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; } ret = 0; error_out: if (table) { toml_free(table); } if (fp) { fclose(fp); } return ret; } void stellar_config_print(const struct stellar_config *config) { if (config == NULL) { return; } 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); CONFIG_LOG_DEBUG("snowflake->snowflake_offset : %d", snowflake_opts->snowflake_offset); // packet io config CONFIG_LOG_DEBUG("packet_io->mode : %s", pkt_io_opts->mode == PACKET_IO_DUMPFILE ? "dumpfile" : (pkt_io_opts->mode == PACKET_IO_DUMPFILELIST ? "dumpfilelist" : "marsio")); if (pkt_io_opts->mode == PACKET_IO_DUMPFILE || pkt_io_opts->mode == PACKET_IO_DUMPFILELIST) { CONFIG_LOG_DEBUG("packet_io->dumpfile_path : %s", pkt_io_opts->dumpfile_path); } else { 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->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); // 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); 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); }