Add packet utils to packet.h

This commit is contained in:
luwenpeng
2024-04-11 19:44:02 +08:00
parent 84541c40e2
commit 17f5d338de
32 changed files with 615 additions and 558 deletions

View File

@@ -59,19 +59,19 @@ static void signal_handler(int signo)
{
if (signo == SIGINT)
{
STELLAR_LOG_STATE("SIGINT received, exit !!!");
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGQUIT)
{
STELLAR_LOG_STATE("SIGQUIT received, exit !!!");
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGTERM)
{
STELLAR_LOG_STATE("SIGTERM received, exit !!!");
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
@@ -84,22 +84,22 @@ 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)
{
enum packet_action action = packet_get_action(pkt);
enum packet_type type = packet_get_type(pkt);
int is_ctrl = packet_is_ctrl(pkt);
int need_drop = packet_need_drop(pkt);
if (sess)
if (sess != NULL)
{
enum session_stat stat_pkt;
enum session_stat stat_byte;
if (action == PACKET_ACTION_DROP)
if (need_drop)
{
stat_pkt = (type == PACKET_TYPE_CTRL) ? STAT_CTRL_PKTS_DROP : STAT_RAW_PKTS_DROP;
stat_byte = (type == PACKET_TYPE_CTRL) ? STAT_CTRL_BYTES_DROP : STAT_RAW_BYTES_DROP;
stat_pkt = is_ctrl ? STAT_CTRL_PKTS_DROP : STAT_RAW_PKTS_DROP;
stat_byte = is_ctrl ? STAT_CTRL_BYTES_DROP : STAT_RAW_BYTES_DROP;
}
else
{
stat_pkt = (type == PACKET_TYPE_CTRL) ? STAT_CTRL_PKTS_TX : STAT_RAW_PKTS_TX;
stat_byte = (type == PACKET_TYPE_CTRL) ? STAT_CTRL_BYTES_TX : STAT_RAW_BYTES_TX;
stat_pkt = is_ctrl ? STAT_CTRL_PKTS_TX : STAT_RAW_PKTS_TX;
stat_byte = is_ctrl ? STAT_CTRL_BYTES_TX : STAT_RAW_BYTES_TX;
}
session_inc_stat(sess, session_get_current_direction(sess), stat_pkt, 1);
@@ -108,7 +108,7 @@ static void execute_packet_action(struct packet_io *packet_io, struct session *s
session_set_current_direction(sess, SESSION_DIRECTION_NONE);
}
if (action == PACKET_ACTION_DROP)
if (need_drop)
{
packet_io_drop(packet_io, thr_idx, pkt, 1);
}
@@ -168,6 +168,7 @@ static void *work_thread(void *arg)
for (int i = 0; i < nr_recv; i++)
{
sess = NULL;
pkt = &packets[i];
plugin_manager_dispatch_packet(plug_mgr, pkt);
@@ -237,7 +238,7 @@ static void *work_thread(void *arg)
}
ATOMIC_SET(&thr_ctx->is_runing, 0);
STELLAR_LOG_STATE("worker thread %d stop", thr_idx);
STELLAR_LOG_STATE("worker thread %d exit !!!", thr_idx);
return NULL;
}
@@ -315,7 +316,9 @@ static void stellar_thread_join(struct stellar_runtime *ctx, uint8_t nr_threads)
int main(int argc, char **argv)
{
uint8_t nr_threads;
memset(runtime, 0, sizeof(struct stellar_runtime));
memset(config, 0, sizeof(struct stellar_config));
timestamp_update();
signal(SIGINT, signal_handler);
@@ -326,31 +329,31 @@ int main(int argc, char **argv)
if (log_init(log_config_file) != 0)
{
STELLAR_LOG_ERROR("unable to init log");
return -1;
goto error_out;
}
STELLAR_LOG_STATE("Start Stellar (version: %s)\n %s", __stellar_version, logo_str);
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
if (stellar_config_load(stellar_config_file, config) != 0)
{
STELLAR_LOG_ERROR("unable to load config file");
return -1;
goto error_out;
}
stellar_config_print(config);
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
{
STELLAR_LOG_ERROR("unable to init id generator");
return -1;
goto error_out;
}
runtime->plug_mgr = plugin_manager_new();
if (runtime->plug_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create plugin manager");
return -1;
goto error_out;
}
uint8_t nr_threads = config->io_opts.nr_threads;
nr_threads = config->io_opts.nr_threads;
runtime->packet_io = packet_io_new(&config->io_opts);
if (runtime->packet_io == NULL)
{
@@ -373,7 +376,7 @@ int main(int argc, char **argv)
while (!ATOMIC_READ(&runtime->need_exit))
{
timestamp_update();
sleep(1);
usleep(5 * 1000);
}
error_out:
@@ -381,7 +384,7 @@ error_out:
stellar_thread_clean(runtime, nr_threads);
packet_io_free(runtime->packet_io);
plugin_manager_free(runtime->plug_mgr);
STELLAR_LOG_STATE("stellar exit !!!\n");
log_free();
return 0;