Optimize packet I/O and timeouts

- Introduce per-thread I/O statistics for packet I/O to reduce performance overhead.
- Implement packet_io_yield() for better thread management during I/O operations.
- Refactor time wheel management:
  - Replace timeouts-based cron tasks with (now_ts - last_ts > timeout) for scheduled tasks.
  - Update the time wheel every 5 ms for improved time management.
This commit is contained in:
luwenpeng
2024-04-18 14:20:28 +08:00
parent 892842c61b
commit 5508454a1b
32 changed files with 377 additions and 540 deletions

View File

@@ -1,4 +1,5 @@
add_executable(stellar stellar.cpp stat.cpp cron.cpp)
target_link_libraries(stellar timestamp session_manager plugin_manager pthread config packet_io fieldstat4)
add_executable(stellar config.cpp stat.cpp stellar.cpp)
target_link_libraries(stellar timestamp plugin_manager session_manager ip_reassembly packet_io)
target_link_libraries(stellar pthread fieldstat4 toml)
install(TARGETS stellar RUNTIME DESTINATION bin COMPONENT Program)

538
src/stellar/config.cpp Normal file
View File

@@ -0,0 +1,538 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "toml.h"
// return 0: success
// retuun -1: failed
static int parse_device_section(toml_table_t *root, struct device_options *opts)
{
const char *ptr;
toml_table_t *table;
table = toml_table_in(root, "device");
if (table == NULL)
{
CONFIG_LOG_ERROR("config file missing device section");
return -1;
}
ptr = toml_raw_in(table, "base");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing device->base");
return -1;
}
opts->base = atoi(ptr);
ptr = toml_raw_in(table, "offset");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing device->offset");
return -1;
}
opts->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)
{
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");
return -1;
}
ptr = toml_raw_in(table, "mode");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->mode");
return -1;
}
if (strcmp(ptr, "dumpfile") == 0)
{
opts->mode = PACKET_IO_DUMPFILE;
}
else if (strcmp(ptr, "marsio") == 0)
{
opts->mode = PACKET_IO_MARSIO;
}
else
{
CONFIG_LOG_ERROR("config file invalid packet_io->mode %s, only support dumpfile and marsio", ptr);
return -1;
}
if (opts->mode == PACKET_IO_DUMPFILE)
{
ptr = toml_raw_in(table, "dumpfile_dir");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->dumpfile_dir");
return -1;
}
// skip ""
strncpy(opts->dumpfile_dir, ptr + 1, strlen(ptr) - 2);
}
else
{
ptr = toml_raw_in(table, "app_symbol");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->app_symbol");
return -1;
}
strncpy(opts->app_symbol, ptr, sizeof(opts->app_symbol) - 1);
ptr = toml_raw_in(table, "dev_symbol");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->dev_symbol");
return -1;
}
strncpy(opts->dev_symbol, ptr, sizeof(opts->dev_symbol) - 1);
}
ptr = toml_raw_in(table, "nr_threads");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->nr_threads");
return -1;
}
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);
return -1;
}
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");
return -1;
}
for (uint8_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);
return -1;
}
opts->cpu_mask[i] = atoi(ptr);
}
return 0;
}
// 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
int stellar_load_config(const char *file, struct stellar_config *config)
{
int ret = -1;
char errbuf[200];
FILE *fp = NULL;
toml_table_t *table = NULL;
memset(config, 0, sizeof(*config));
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_device_section(table, &config->dev_opts) != 0)
{
goto error_out;
}
if (parse_packet_io_section(table, &config->io_opts) != 0)
{
goto error_out;
}
if (parse_ip_reassembly_section(table, &config->ip_opts) != 0)
{
goto error_out;
}
if (parse_session_manager_section(table, &config->sess_mgr_opts) != 0)
{
goto error_out;
}
ret = 0;
error_out:
if (table)
{
toml_free(table);
}
if (fp)
{
fclose(fp);
}
return ret;
}
void stellar_print_config(struct stellar_config *config)
{
if (config == NULL)
{
return;
}
struct device_options *dev_opts = &config->dev_opts;
struct packet_io_options *io_opts = &config->io_opts;
struct ip_reassembly_options *ip_opts = &config->ip_opts;
struct session_manager_options *sess_mgr_opts = &config->sess_mgr_opts;
// device config
CONFIG_LOG_DEBUG("device->base : %d", dev_opts->base);
CONFIG_LOG_DEBUG("device->offset : %d", dev_opts->offset);
// packet io config
CONFIG_LOG_DEBUG("packet_io->mode : %s", io_opts->mode == PACKET_IO_DUMPFILE ? "dumpfile" : "marsio");
if (io_opts->mode == PACKET_IO_DUMPFILE)
{
CONFIG_LOG_DEBUG("packet_io->dumpfile_dir : %s", io_opts->dumpfile_dir);
}
else
{
CONFIG_LOG_DEBUG("packet_io->app_symbol : %s", io_opts->app_symbol);
CONFIG_LOG_DEBUG("packet_io->dev_symbol : %s", io_opts->dev_symbol);
}
CONFIG_LOG_DEBUG("packet_io->nr_threads : %d", io_opts->nr_threads);
for (uint8_t i = 0; i < io_opts->nr_threads; i++)
{
CONFIG_LOG_DEBUG("packet_io->cpu_mask[%03d] : %d", i, io_opts->cpu_mask[i]);
}
// ip reassemble config
CONFIG_LOG_DEBUG("ip_reassembly->enable : %d", ip_opts->enable);
CONFIG_LOG_DEBUG("ip_reassembly->timeout : %d", ip_opts->timeout);
CONFIG_LOG_DEBUG("ip_reassembly->bucket_entries : %d", ip_opts->bucket_entries);
CONFIG_LOG_DEBUG("ip_reassembly->bucket_num : %d", ip_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);
}

37
src/stellar/config.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef _CONFIG_H
#define _CONFIG_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "packet_io.h"
#include "ip_reassembly.h"
#include "session_manager.h"
#define CONFIG_LOG_ERROR(format, ...) LOG_ERROR("config", format, ##__VA_ARGS__)
#define CONFIG_LOG_DEBUG(format, ...) LOG_DEBUG("config", format, ##__VA_ARGS__)
struct device_options
{
uint8_t base;
uint8_t offset;
};
struct stellar_config
{
struct device_options dev_opts;
struct packet_io_options io_opts;
struct ip_reassembly_options ip_opts;
struct session_manager_options sess_mgr_opts;
};
int stellar_load_config(const char *file, struct stellar_config *config);
void stellar_print_config(struct stellar_config *config);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,69 +0,0 @@
#include "cron.h"
#ifndef container_of
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *)&((type *)0)->member)
#endif
struct thread_cron
{
struct timeouts *timeouts;
};
struct thread_cron *thread_cron_new(uint64_t now)
{
timeout_error_t err;
struct thread_cron *cron = (struct thread_cron *)calloc(1, sizeof(struct thread_cron));
if (cron == NULL)
{
return NULL;
}
cron->timeouts = timeouts_open(0, &err);
if (cron->timeouts == NULL)
{
goto error_out;
}
timeouts_update(cron->timeouts, now);
return cron;
error_out:
thread_cron_free(cron);
return NULL;
}
void thread_cron_free(struct thread_cron *cron)
{
if (cron)
{
if (cron->timeouts)
{
timeouts_close(cron->timeouts);
}
free(cron);
cron = NULL;
}
}
void thread_cron_run(struct thread_cron *cron, uint64_t now)
{
struct timeout *timeout;
timeouts_update(cron->timeouts, now);
while ((timeout = timeouts_get(cron->timeouts)))
{
struct cron_task *task = container_of(timeout, struct cron_task, timeout);
task->callback(task->data);
}
}
void thread_cron_add_task(struct thread_cron *cron, struct cron_task *task)
{
timeout_init(&task->timeout, TIMEOUT_INT);
timeouts_add(cron->timeouts, &task->timeout, task->cycle);
}
void thread_cron_del_task(struct thread_cron *cron, struct cron_task *task)
{
timeouts_del(cron->timeouts, &task->timeout);
}

View File

@@ -1,32 +0,0 @@
#ifndef _CRON_H
#define _CRON_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdint.h>
#include "timeout.h"
struct cron_task
{
struct timeout timeout;
void (*callback)(void *);
void *data;
uint64_t cycle;
};
struct thread_cron;
struct thread_cron *thread_cron_new(uint64_t now);
void thread_cron_free(struct thread_cron *cron);
void thread_cron_run(struct thread_cron *cron, uint64_t now);
void thread_cron_add_task(struct thread_cron *cron, struct cron_task *task);
void thread_cron_del_task(struct thread_cron *cron, struct cron_task *task);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -80,6 +80,7 @@ struct stellar_stat
// thread stat
uint16_t nr_thread;
int flag[MAX_THREAD_NUM]; // IS_FREE or IS_BUSY
struct io_stat thr_io_stat[MAX_THREAD_NUM];
struct ip_reassembly_stat thr_ip_stat[MAX_THREAD_NUM];
struct session_manager_stat thr_sess_stat[MAX_THREAD_NUM];
@@ -196,6 +197,28 @@ void stellar_stat_output(struct stellar_stat *stat)
{
if (ATOMIC_READ(&(stat->flag[i])) == IS_BUSY)
{
// packet io stat
stat->io_stat.dev_rx_pkts += stat->thr_io_stat[i].dev_rx_pkts;
stat->io_stat.dev_rx_bytes += stat->thr_io_stat[i].dev_rx_bytes;
stat->io_stat.dev_tx_pkts += stat->thr_io_stat[i].dev_tx_pkts;
stat->io_stat.dev_tx_bytes += stat->thr_io_stat[i].dev_tx_bytes;
stat->io_stat.keep_alive_pkts += stat->thr_io_stat[i].keep_alive_pkts;
stat->io_stat.keep_alive_bytes += stat->thr_io_stat[i].keep_alive_bytes;
stat->io_stat.raw_rx_pkts += stat->thr_io_stat[i].raw_rx_pkts;
stat->io_stat.raw_rx_bytes += stat->thr_io_stat[i].raw_rx_bytes;
stat->io_stat.raw_tx_pkts += stat->thr_io_stat[i].raw_tx_pkts;
stat->io_stat.raw_tx_bytes += stat->thr_io_stat[i].raw_tx_bytes;
stat->io_stat.ctrl_rx_pkts += stat->thr_io_stat[i].ctrl_rx_pkts;
stat->io_stat.ctrl_rx_bytes += stat->thr_io_stat[i].ctrl_rx_bytes;
stat->io_stat.ctrl_tx_pkts += stat->thr_io_stat[i].ctrl_tx_pkts;
stat->io_stat.ctrl_tx_bytes += stat->thr_io_stat[i].ctrl_tx_bytes;
// ip reassembly stat
stat->ip_stat.ip4_flow_find += stat->thr_ip_stat[i].ip4_flow_find;
stat->ip_stat.ip4_flow_add += stat->thr_ip_stat[i].ip4_flow_add;
@@ -339,34 +362,11 @@ void stellar_stat_output(struct stellar_stat *stat)
memset(&stat->sess_stat, 0, sizeof(struct session_manager_stat));
}
void stellar_peek_io_stat(struct stellar_stat *stat, const struct io_stat *pkt_io)
{
stat->io_stat.dev_rx_pkts += pkt_io->dev_rx_pkts;
stat->io_stat.dev_rx_bytes += pkt_io->dev_rx_bytes;
stat->io_stat.dev_tx_pkts += pkt_io->dev_tx_pkts;
stat->io_stat.dev_tx_bytes += pkt_io->dev_tx_bytes;
stat->io_stat.keep_alive_pkts += pkt_io->keep_alive_pkts;
stat->io_stat.keep_alive_bytes += pkt_io->keep_alive_bytes;
stat->io_stat.raw_rx_pkts += pkt_io->raw_rx_pkts;
stat->io_stat.raw_rx_bytes += pkt_io->raw_rx_bytes;
stat->io_stat.raw_tx_pkts += pkt_io->raw_tx_pkts;
stat->io_stat.raw_tx_bytes += pkt_io->raw_tx_bytes;
stat->io_stat.ctrl_rx_pkts += pkt_io->ctrl_rx_pkts;
stat->io_stat.ctrl_rx_bytes += pkt_io->ctrl_rx_bytes;
stat->io_stat.ctrl_tx_pkts += pkt_io->ctrl_tx_pkts;
stat->io_stat.ctrl_tx_bytes += pkt_io->ctrl_tx_bytes;
}
void stellar_peek_thr_stat(struct stellar_stat *stat, const struct thread_stat *thr_stat, uint16_t thr_idx)
void stellar_stat_merge(struct stellar_stat *stat, const struct thread_stat *thr_stat, uint16_t thr_idx)
{
if (ATOMIC_READ(&(stat->flag[thr_idx])) == IS_FREE)
{
memcpy(&stat->thr_io_stat[thr_idx], thr_stat->io, sizeof(struct io_stat));
memcpy(&stat->thr_ip_stat[thr_idx], thr_stat->ip_rea, sizeof(struct ip_reassembly_stat));
memcpy(&stat->thr_sess_stat[thr_idx], thr_stat->sess_mgr, sizeof(struct session_manager_stat));

View File

@@ -14,6 +14,7 @@ extern "C"
struct thread_stat
{
struct io_stat *io;
struct ip_reassembly_stat *ip_rea;
struct session_manager_stat *sess_mgr;
};
@@ -22,9 +23,7 @@ struct stellar_stat;
struct stellar_stat *stellar_stat_new(uint16_t nr_thread);
void stellar_stat_free(struct stellar_stat *stat);
void stellar_stat_output(struct stellar_stat *stat);
void stellar_peek_io_stat(struct stellar_stat *stat, const struct io_stat *io_stat);
void stellar_peek_thr_stat(struct stellar_stat *stat, const struct thread_stat *thr_stat, uint16_t thr_idx);
void stellar_stat_merge(struct stellar_stat *stat, const struct thread_stat *thr_stat, uint16_t thr_idx);
#ifdef __cpluscplus
}

View File

@@ -10,7 +10,6 @@
#include "logo.h"
#include "stat.h"
#include "cron.h"
#include "stellar.h"
#include "config.h"
#include "packet_private.h"
@@ -25,12 +24,13 @@
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_DEBUG(format, ...) LOG_DEBUG("stellar", format, ##__VA_ARGS__)
struct thread_ctx
struct stellar_thread
{
pthread_t tid;
uint16_t idx;
uint64_t is_runing;
struct thread_cron *cron;
uint64_t stat_last_merge_ts;
uint64_t timing_wheel_last_update_ts;
struct ip_reassembly *ip_mgr;
struct session_manager *sess_mgr;
};
@@ -38,25 +38,21 @@ struct thread_ctx
struct stellar_runtime
{
uint64_t need_exit;
struct thread_cron *cron;
uint64_t stat_last_output_ts;
struct stellar_stat *stat;
struct packet_io *packet_io;
struct plugin_manager *plug_mgr;
struct thread_ctx threads[MAX_THREAD_NUM];
struct stellar_thread threads[MAX_THREAD_NUM];
};
struct stellar_runtime __runtime;
struct stellar_runtime __runtime = {0};
struct stellar_runtime *runtime = &__runtime;
struct stellar_config __config;
struct stellar_config __config = {0};
struct stellar_config *config = &__config;
static const char *log_config_file = "./conf/log.toml";
static const char *stellar_config_file = "./conf/stellar.toml";
/******************************************************************************
* util
******************************************************************************/
static void signal_handler(int signo)
{
if (signo == SIGINT)
@@ -84,16 +80,15 @@ static void signal_handler(int signo)
}
}
static void execute_packet_action(struct packet_io *packet_io, struct session *sess, struct packet *pkt, uint16_t thr_idx)
static void update_session_stat(struct session *sess, struct packet *pkt)
{
int is_ctrl = packet_is_ctrl(pkt);
int need_drop = packet_need_drop(pkt);
if (sess != NULL)
if (sess)
{
enum session_stat stat_pkt;
enum session_stat stat_byte;
if (need_drop)
enum session_direction dir = session_get_current_direction(sess);
int is_ctrl = packet_is_ctrl(pkt);
if (packet_need_drop(pkt))
{
stat_pkt = is_ctrl ? STAT_CTRL_PKTS_DROP : STAT_RAW_PKTS_DROP;
stat_byte = is_ctrl ? STAT_CTRL_BYTES_DROP : STAT_RAW_BYTES_DROP;
@@ -104,41 +99,61 @@ static void execute_packet_action(struct packet_io *packet_io, struct session *s
stat_byte = is_ctrl ? STAT_CTRL_BYTES_TX : STAT_RAW_BYTES_TX;
}
session_inc_stat(sess, session_get_current_direction(sess), stat_pkt, 1);
session_inc_stat(sess, session_get_current_direction(sess), stat_byte, packet_get_len(pkt));
session_inc_stat(sess, dir, stat_pkt, 1);
session_inc_stat(sess, dir, stat_byte, packet_get_len(pkt));
session_set_current_packet(sess, NULL);
session_set_current_direction(sess, SESSION_DIRECTION_NONE);
}
}
if (need_drop)
static inline void free_evicted_sessions(struct session_manager *sess_mgr, uint64_t max_free)
{
void *plugin_ctx = NULL;
struct session *sess = NULL;
for (uint64_t i = 0; i < max_free; i++)
{
packet_io_drop(packet_io, thr_idx, pkt, 1);
}
else
{
packet_io_egress(packet_io, thr_idx, pkt, 1);
sess = session_manager_get_evicted_session(sess_mgr);
if (sess)
{
plugin_ctx = session_get_user_data(sess);
plugin_manager_free_ctx(plugin_ctx);
session_manager_free_session(sess_mgr, sess);
}
else
{
break;
}
}
}
/******************************************************************************
* thread
******************************************************************************/
static inline void thread_set_name(const char *thd_symbol, uint16_t thd_idx)
static inline void free_expired_sessions(struct session_manager *sess_mgr, uint64_t max_free, uint64_t now)
{
char thd_name[16];
snprintf(thd_name, sizeof(thd_name), "%s:%d", thd_symbol, thd_idx);
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);
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);
if (sess)
{
plugin_ctx = session_get_user_data(sess);
plugin_manager_free_ctx(plugin_ctx);
session_manager_free_session(sess_mgr, sess);
}
else
{
break;
}
}
}
static inline void thread_stat_merge_cron(void *ctx)
static inline void merge_thread_stat(struct stellar_thread *thread, uint64_t now)
{
struct thread_ctx *thr_ctx = (struct thread_ctx *)ctx;
struct thread_stat thr_stat = {
ip_reassembly_get_stat(thr_ctx->ip_mgr),
session_manager_get_stat(thr_ctx->sess_mgr),
packet_io_stat(runtime->packet_io, thread->idx),
ip_reassembly_stat(thread->ip_mgr),
session_manager_stat(thread->sess_mgr),
};
stellar_peek_thr_stat(runtime->stat, &thr_stat, thr_ctx->idx);
stellar_stat_merge(runtime->stat, &thr_stat, thread->idx);
}
static void *work_thread(void *arg)
@@ -146,26 +161,20 @@ static void *work_thread(void *arg)
int nr_recv;
uint64_t now = 0;
uint16_t thr_idx = 0;
void *plugin_ctx;
struct packet *pkt;
char thd_name[16] = {0};
void *plugin_ctx = NULL;
struct packet *pkt = NULL;
struct packet packets[RX_BURST_MAX];
struct session *sess;
struct session *evicted_sess;
struct session *expired_sess;
struct session *sess = NULL;
struct packet_io *packet_io = runtime->packet_io;
struct plugin_manager *plug_mgr = runtime->plug_mgr;
struct thread_ctx *thr_ctx = (struct thread_ctx *)arg;
struct thread_cron *cron = thr_ctx->cron;
struct ip_reassembly *ip_reass = thr_ctx->ip_mgr;
struct session_manager *sess_mgr = thr_ctx->sess_mgr;
thr_idx = thr_ctx->idx;
struct stellar_thread *thread = (struct stellar_thread *)arg;
struct ip_reassembly *ip_reass = thread->ip_mgr;
struct session_manager *sess_mgr = thread->sess_mgr;
thr_idx = thread->idx;
struct cron_task stat_task = {
.callback = thread_stat_merge_cron,
.data = thr_ctx,
.cycle = 2000, // ms
};
thread_cron_add_task(cron, &stat_task);
snprintf(thd_name, sizeof(thd_name), "stellar:%d", thr_idx);
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);
if (packet_io_init(packet_io, thr_idx) != 0)
{
@@ -173,8 +182,7 @@ static void *work_thread(void *arg)
return NULL;
}
ATOMIC_SET(&thr_ctx->is_runing, 1);
thread_set_name("stellar", thr_idx);
ATOMIC_SET(&thread->is_runing, 1);
STELLAR_LOG_STATE("worker thread %d runing", thr_idx);
while (ATOMIC_READ(&runtime->need_exit) == 0)
@@ -227,39 +235,47 @@ static void *work_thread(void *arg)
plugin_manager_dispatch_session(plug_mgr, sess, pkt);
fast_path:
execute_packet_action(packet_io, sess, pkt, thr_idx);
}
idle_tasks:
// nr_recv packet atmost trigger nr_recv session evict
for (int i = 0; i < nr_recv; i++)
{
evicted_sess = session_manager_get_evicted_session(sess_mgr);
if (evicted_sess)
update_session_stat(sess, pkt);
if (packet_need_drop(pkt))
{
plugin_ctx = session_get_user_data(evicted_sess);
plugin_manager_free_ctx(plugin_ctx);
session_manager_free_session(sess_mgr, evicted_sess);
packet_io_drop(packet_io, thr_idx, pkt, 1);
}
else
{
packet_io_egress(packet_io, thr_idx, pkt, 1);
}
}
while ((expired_sess = session_manager_get_expired_session(sess_mgr, now)))
{
plugin_ctx = session_get_user_data(expired_sess);
plugin_manager_free_ctx(plugin_ctx);
session_manager_free_session(sess_mgr, expired_sess);
}
ip_reassembly_expire(ip_reass, now);
idle_tasks:
// nr_recv packet atmost trigger nr_recv session evicted
free_evicted_sessions(sess_mgr, nr_recv);
thread_cron_run(cron, now);
// per 5 ms, atmost free 8 expired session
if (now - thread->timing_wheel_last_update_ts > 5)
{
free_expired_sessions(sess_mgr, 8, now);
thread->timing_wheel_last_update_ts = now;
}
if (now - thread->stat_last_merge_ts > 1000)
{
merge_thread_stat(thread, now);
thread->stat_last_merge_ts = now;
}
ip_reassembly_expire(ip_reass, now);
// TODO
// plugin_manager_cron();
// poll_non_packet_events();
// packet_io_yield();
if (nr_recv == 0)
{
packet_io_yield(packet_io, thr_idx, 10);
}
}
ATOMIC_SET(&thr_ctx->is_runing, 0);
ATOMIC_SET(&thread->is_runing, 0);
STELLAR_LOG_STATE("worker thread %d exit !!!", thr_idx);
return NULL;
@@ -270,23 +286,19 @@ static int stellar_thread_init(struct stellar_runtime *ctx, uint8_t nr_threads)
uint64_t now = timestamp_get_msec();
for (uint8_t i = 0; i < nr_threads; i++)
{
struct thread_ctx *thr_ctx = &ctx->threads[i];
thr_ctx->idx = i;
thr_ctx->is_runing = 0;
thr_ctx->cron = thread_cron_new(now);
if (thr_ctx->cron == NULL)
{
STELLAR_LOG_ERROR("unable to create thread cron");
return -1;
}
thr_ctx->sess_mgr = session_manager_new(&config->sess_mgr_opts, now);
if (thr_ctx->sess_mgr == NULL)
struct stellar_thread *thread = &ctx->threads[i];
thread->idx = i;
thread->is_runing = 0;
thread->stat_last_merge_ts = now;
thread->timing_wheel_last_update_ts = now;
thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now);
if (thread->sess_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create session manager");
return -1;
}
thr_ctx->ip_mgr = ip_reassembly_new(&config->ip_opts);
if (thr_ctx->ip_mgr == NULL)
thread->ip_mgr = ip_reassembly_new(&config->ip_opts);
if (thread->ip_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create ip reassemble manager");
return -1;
@@ -300,13 +312,12 @@ static void stellar_thread_clean(struct stellar_runtime *ctx, uint8_t nr_threads
{
for (uint8_t i = 0; i < nr_threads; i++)
{
struct thread_ctx *thr_ctx = &ctx->threads[i];
if (ATOMIC_READ(&thr_ctx->is_runing) == 0)
struct stellar_thread *thread = &ctx->threads[i];
if (ATOMIC_READ(&thread->is_runing) == 0)
{
STELLAR_LOG_STATE("wait worker thread %d free context", i);
session_manager_free(thr_ctx->sess_mgr);
ip_reassembly_free(thr_ctx->ip_mgr);
thread_cron_free(thr_ctx->cron);
session_manager_free(thread->sess_mgr);
ip_reassembly_free(thread->ip_mgr);
}
}
}
@@ -315,8 +326,8 @@ static int stellar_thread_run(struct stellar_runtime *ctx, uint8_t nr_threads)
{
for (uint8_t i = 0; i < nr_threads; i++)
{
struct thread_ctx *thr_ctx = &ctx->threads[i];
if (pthread_create(&thr_ctx->tid, NULL, work_thread, (void *)thr_ctx) < 0)
struct stellar_thread *thread = &ctx->threads[i];
if (pthread_create(&thread->tid, NULL, work_thread, (void *)thread) < 0)
{
STELLAR_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno));
return -1;
@@ -330,8 +341,8 @@ static void stellar_thread_join(struct stellar_runtime *ctx, uint8_t nr_threads)
{
for (uint8_t i = 0; i < nr_threads; i++)
{
struct thread_ctx *thr_ctx = &ctx->threads[i];
while (ATOMIC_READ(&thr_ctx->is_runing) == 1)
struct stellar_thread *thread = &ctx->threads[i];
while (ATOMIC_READ(&thread->is_runing) == 1)
{
STELLAR_LOG_STATE("wait worker thread %d stop", i);
sleep(1);
@@ -339,28 +350,9 @@ static void stellar_thread_join(struct stellar_runtime *ctx, uint8_t nr_threads)
}
}
/******************************************************************************
* main
******************************************************************************/
static inline void stellar_stat_output_cron(void *ctx)
{
struct stellar_runtime *runtime = (struct stellar_runtime *)ctx;
stellar_peek_io_stat(runtime->stat, packet_io_get_stat(runtime->packet_io));
stellar_stat_output(runtime->stat);
}
int main(int argc, char **argv)
{
uint8_t nr_threads = 0;
struct cron_task stat_task =
{
.callback = stellar_stat_output_cron,
.data = runtime,
.cycle = 2000, // ms
};
memset(runtime, 0, sizeof(struct stellar_runtime));
memset(config, 0, sizeof(struct stellar_config));
timestamp_update();
signal(SIGINT, signal_handler);
@@ -375,12 +367,12 @@ int main(int argc, char **argv)
}
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
if (stellar_config_load(stellar_config_file, config) != 0)
if (stellar_load_config(stellar_config_file, config) != 0)
{
STELLAR_LOG_ERROR("unable to load config file");
goto error_out;
}
stellar_config_print(config);
stellar_print_config(config);
STELLAR_LOG_DEBUG("sizeof(struct session) = %lu bytes", sizeof(struct session));
nr_threads = config->io_opts.nr_threads;
@@ -390,13 +382,6 @@ int main(int argc, char **argv)
goto error_out;
}
runtime->cron = thread_cron_new(timestamp_get_msec());
if (runtime->cron == NULL)
{
STELLAR_LOG_ERROR("unable to create runtime cron");
goto error_out;
}
runtime->stat = stellar_stat_new(nr_threads);
if (runtime->stat == NULL)
{
@@ -430,12 +415,16 @@ int main(int argc, char **argv)
goto error_out;
}
thread_cron_add_task(runtime->cron, &stat_task);
runtime->stat_last_output_ts = timestamp_get_msec();
while (!ATOMIC_READ(&runtime->need_exit))
{
timestamp_update();
thread_cron_run(runtime->cron, timestamp_get_msec());
usleep(5 * 1000);
if (timestamp_get_msec() - runtime->stat_last_output_ts > 1000)
{
runtime->stat_last_output_ts = timestamp_get_msec();
stellar_stat_output(runtime->stat);
}
usleep(1000); // 1ms
}
error_out:
@@ -444,7 +433,6 @@ error_out:
packet_io_free(runtime->packet_io);
plugin_manager_free(runtime->plug_mgr);
stellar_stat_free(runtime->stat);
thread_cron_free(runtime->cron);
STELLAR_LOG_STATE("stellar exit !!!\n");
log_free();