Add packet IO module
* support marsio
* support dumpfile ( 1 thread read dumpfile & N thread handle packet)
This commit is contained in:
@@ -7,16 +7,16 @@
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <netinet/ether.h>
|
||||
|
||||
#include "logo.h"
|
||||
#include "stellar.h"
|
||||
#include "marsio.h"
|
||||
#include "config.h"
|
||||
#include "packet.h"
|
||||
#include "packet_io.h"
|
||||
#include "timestamp.h"
|
||||
#include "session_manager.h"
|
||||
#include "id_generator.h"
|
||||
#include "ip_reassemble.h"
|
||||
#include "session_manager.h"
|
||||
|
||||
#define STELLAR_LOG_STATE(format, ...) LOG_STATE("stellar", format, ##__VA_ARGS__)
|
||||
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
|
||||
@@ -25,13 +25,6 @@
|
||||
#define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED)
|
||||
#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED)
|
||||
|
||||
struct packet_io
|
||||
{
|
||||
struct mr_instance *mr_ins;
|
||||
struct mr_vdev *mr_dev;
|
||||
struct mr_sendpath *mr_path;
|
||||
};
|
||||
|
||||
struct thread_context
|
||||
{
|
||||
pthread_t tid;
|
||||
@@ -39,6 +32,7 @@ struct thread_context
|
||||
uint64_t need_exit;
|
||||
uint64_t is_runing;
|
||||
struct session_manager *sess_mgr;
|
||||
struct ip_reassemble_manager *ip_mgr;
|
||||
};
|
||||
|
||||
struct stellar_context
|
||||
@@ -46,12 +40,18 @@ struct stellar_context
|
||||
uint64_t need_exit;
|
||||
struct config config;
|
||||
|
||||
struct packet_io *pkt_io;
|
||||
struct packet_io *packet_io;
|
||||
struct thread_context threads_ctx[MAX_THREAD_NUM];
|
||||
};
|
||||
|
||||
struct stellar_context stellar_context;
|
||||
struct stellar_context *stellar_ctx_ptr = &stellar_context;
|
||||
struct stellar_context *stellar_ctx = &stellar_context;
|
||||
|
||||
// config
|
||||
struct device_config *dev_cfg = &stellar_context.config.dev_cfg;
|
||||
struct packet_io_config *pkt_io_cfg = &stellar_context.config.pkt_io_cfg;
|
||||
struct ip_reassemble_config *ip_reass_cfg = &stellar_context.config.ip_reass_cfg;
|
||||
struct session_manager_config *sess_mgr_cfg = &stellar_context.config.sess_mgr_cfg;
|
||||
|
||||
static const char *log_config_file = "./conf/log.toml";
|
||||
static const char *stellar_config_file = "./conf/stellar.toml";
|
||||
@@ -60,7 +60,7 @@ static const char *stellar_config_file = "./conf/stellar.toml";
|
||||
* example
|
||||
******************************************************************************/
|
||||
|
||||
static void __packet_plugin_dispatch_example(const struct packet *pkt)
|
||||
static void __packet_plugin(const struct packet *pkt)
|
||||
{
|
||||
if (pkt == NULL)
|
||||
{
|
||||
@@ -71,7 +71,7 @@ static void __packet_plugin_dispatch_example(const struct packet *pkt)
|
||||
printf("<= packet dispatch\n");
|
||||
}
|
||||
|
||||
static void __session_plugin_dispatch_example(struct session *sess)
|
||||
static void __session_plugin(struct session *sess)
|
||||
{
|
||||
if (sess == NULL)
|
||||
{
|
||||
@@ -96,19 +96,19 @@ static void signal_handler(int signo)
|
||||
if (signo == SIGINT)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGINT received, exit !!!");
|
||||
ATOMIC_SET(&stellar_ctx_ptr->need_exit, 1);
|
||||
ATOMIC_SET(&stellar_ctx->need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGQUIT)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGQUIT received, exit !!!");
|
||||
ATOMIC_SET(&stellar_ctx_ptr->need_exit, 1);
|
||||
ATOMIC_SET(&stellar_ctx->need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGTERM)
|
||||
{
|
||||
STELLAR_LOG_STATE("SIGTERM received, exit !!!");
|
||||
ATOMIC_SET(&stellar_ctx_ptr->need_exit, 1);
|
||||
ATOMIC_SET(&stellar_ctx->need_exit, 1);
|
||||
}
|
||||
|
||||
if (signo == SIGHUP)
|
||||
@@ -118,26 +118,6 @@ static void signal_handler(int signo)
|
||||
}
|
||||
}
|
||||
|
||||
// return 0 : not keepalive packet
|
||||
// return 1 : is keepalive packet
|
||||
static inline int is_keepalive_packet(const char *data, int len)
|
||||
{
|
||||
if (data == NULL || len < (int)(sizeof(struct ethhdr)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ethhdr *eth_hdr = (struct ethhdr *)data;
|
||||
if (eth_hdr->h_proto == 0xAAAA)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* thread
|
||||
******************************************************************************/
|
||||
@@ -151,27 +131,15 @@ static inline void thread_set_name(const char *thd_symbol, uint16_t thd_idx)
|
||||
|
||||
static void *main_loop(void *arg)
|
||||
{
|
||||
int n_pkt_recved;
|
||||
uint16_t len = 0;
|
||||
const char *data;
|
||||
struct packet pkt;
|
||||
struct packet *pkt = NULL;
|
||||
struct session *sess;
|
||||
marsio_buff_t *rx_buff;
|
||||
marsio_buff_t *rx_buffs[RX_BURST_MAX];
|
||||
struct thread_context *threads_ctx = (struct thread_context *)arg;
|
||||
struct session_manager *sess_mgr = threads_ctx->sess_mgr;
|
||||
struct packet_io *pkt_io = stellar_ctx_ptr->pkt_io;
|
||||
uint16_t thd_idx = threads_ctx->index;
|
||||
struct packet_io *packet_io = stellar_ctx->packet_io;
|
||||
struct session_manager *sess_mgr = threads_ctx->sess_mgr;
|
||||
struct ip_reassemble_manager *ip_mgr = threads_ctx->ip_mgr;
|
||||
|
||||
struct mr_vdev *mr_dev = pkt_io->mr_dev;
|
||||
struct mr_sendpath *mr_path = pkt_io->mr_path;
|
||||
struct mr_instance *mr_ins = pkt_io->mr_ins;
|
||||
struct mr_vdev *vdevs[1] = {
|
||||
mr_dev,
|
||||
};
|
||||
int min_timeout_ms = 10;
|
||||
|
||||
if (marsio_thread_init(mr_ins) != 0)
|
||||
if (packet_io_init(packet_io, thd_idx) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init marsio thread");
|
||||
return NULL;
|
||||
@@ -183,50 +151,45 @@ static void *main_loop(void *arg)
|
||||
|
||||
while (ATOMIC_READ(&threads_ctx->need_exit) == 0)
|
||||
{
|
||||
n_pkt_recved = marsio_recv_burst(mr_dev, thd_idx, rx_buffs, RX_BURST_MAX);
|
||||
if (n_pkt_recved <= 0)
|
||||
// recv packet
|
||||
if (packet_io_recv(packet_io, thd_idx, &pkt) != 0)
|
||||
{
|
||||
goto poll_wait;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_pkt_recved; i++)
|
||||
{
|
||||
rx_buff = rx_buffs[i];
|
||||
data = marsio_buff_mtod(rx_buff);
|
||||
len = marsio_buff_datalen(rx_buff);
|
||||
// call packet plugin
|
||||
__packet_plugin(pkt);
|
||||
|
||||
if (is_keepalive_packet(data, len))
|
||||
// ip fragment reassemble
|
||||
if (pkt->frag_layer)
|
||||
{
|
||||
struct packet *temp = ip_reassemble_packet(ip_mgr, pkt);
|
||||
// forward the original fragment packet
|
||||
packet_io_send(packet_io, thd_idx, pkt);
|
||||
if (temp == NULL)
|
||||
{
|
||||
marsio_send_burst(mr_path, thd_idx, &rx_buff, 1);
|
||||
continue;
|
||||
goto poll_wait;
|
||||
}
|
||||
else
|
||||
{
|
||||
packet_parse(&pkt, data, len);
|
||||
__packet_plugin_dispatch_example(&pkt);
|
||||
|
||||
sess = session_manager_update_session(sess_mgr, &pkt);
|
||||
__session_plugin_dispatch_example(sess);
|
||||
|
||||
sess = session_manager_get_evicted_session(sess_mgr);
|
||||
__session_plugin_dispatch_example(sess);
|
||||
|
||||
// TODO
|
||||
if (1) // action == forward
|
||||
{
|
||||
marsio_send_burst(mr_path, thd_idx, &rx_buff, 1);
|
||||
}
|
||||
else // action == drop
|
||||
{
|
||||
marsio_buff_free(mr_ins, &rx_buff, 1, 0, thd_idx);
|
||||
}
|
||||
pkt = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// update session
|
||||
sess = session_manager_update_session(sess_mgr, pkt);
|
||||
__session_plugin(sess);
|
||||
|
||||
// get evicted session
|
||||
sess = session_manager_get_evicted_session(sess_mgr);
|
||||
__session_plugin(sess);
|
||||
|
||||
packet_io_send(packet_io, thd_idx, pkt);
|
||||
|
||||
poll_wait:
|
||||
// get expired session
|
||||
sess = session_manager_get_expired_session(sess_mgr);
|
||||
__session_plugin_dispatch_example(sess);
|
||||
marsio_poll_wait(mr_ins, vdevs, 1, thd_idx, min_timeout_ms);
|
||||
__session_plugin(sess);
|
||||
}
|
||||
|
||||
ATOMIC_SET(&threads_ctx->is_runing, 0);
|
||||
@@ -235,12 +198,9 @@ static void *main_loop(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int thread_context_init(struct stellar_context *ctx)
|
||||
static int thread_context_init(struct stellar_context *ctx, uint8_t nr_threads)
|
||||
{
|
||||
struct system_config *sys_cfg = &ctx->config.sys_cfg;
|
||||
struct session_manager_config *sess_mgr_cfg = &ctx->config.sess_mgr_cfg;
|
||||
|
||||
for (uint8_t i = 0; i < sys_cfg->nr_threads; i++)
|
||||
for (uint8_t i = 0; i < nr_threads; i++)
|
||||
{
|
||||
struct thread_context *threads_ctx = &ctx->threads_ctx[i];
|
||||
threads_ctx->index = i;
|
||||
@@ -253,22 +213,28 @@ static int thread_context_init(struct stellar_context *ctx)
|
||||
STELLAR_LOG_ERROR("unable to create session manager");
|
||||
return -1;
|
||||
}
|
||||
|
||||
threads_ctx->ip_mgr = ip_reassemble_manager_create(ip_reass_cfg);
|
||||
if (threads_ctx->ip_mgr == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create ip reassemble manager");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void thread_context_free(struct stellar_context *ctx)
|
||||
static void thread_context_free(struct stellar_context *ctx, uint8_t nr_threads)
|
||||
{
|
||||
struct system_config *sys_cfg = &ctx->config.sys_cfg;
|
||||
|
||||
for (uint8_t i = 0; i < sys_cfg->nr_threads; i++)
|
||||
for (uint8_t i = 0; i < nr_threads; i++)
|
||||
{
|
||||
struct thread_context *threads_ctx = &ctx->threads_ctx[i];
|
||||
if (ATOMIC_READ(&threads_ctx->is_runing) == 0)
|
||||
{
|
||||
STELLAR_LOG_STATE("wait worker thread %d free context", i);
|
||||
session_manager_destroy(threads_ctx->sess_mgr);
|
||||
ip_reassemble_manager_destory(threads_ctx->ip_mgr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -302,93 +268,13 @@ static void thread_destroy(struct thread_context threads_ctx[], uint8_t nr_threa
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* packet io
|
||||
******************************************************************************/
|
||||
|
||||
void packet_io_destroy(struct packet_io *pkt_io)
|
||||
{
|
||||
if (pkt_io == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (pkt_io->mr_path != NULL)
|
||||
{
|
||||
marsio_sendpath_destory(pkt_io->mr_path);
|
||||
pkt_io->mr_path = NULL;
|
||||
}
|
||||
if (pkt_io->mr_dev != NULL)
|
||||
{
|
||||
marsio_close_device(pkt_io->mr_dev);
|
||||
pkt_io->mr_dev = NULL;
|
||||
}
|
||||
if (pkt_io->mr_ins != NULL)
|
||||
{
|
||||
marsio_destory(pkt_io->mr_ins);
|
||||
pkt_io->mr_ins = NULL;
|
||||
}
|
||||
free(pkt_io);
|
||||
pkt_io = NULL;
|
||||
}
|
||||
|
||||
struct packet_io *packet_io_create(struct system_config *sys_cfg)
|
||||
{
|
||||
struct packet_io *pkt_io = (struct packet_io *)calloc(1, sizeof(struct packet_io));
|
||||
if (pkt_io == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to alloc packet io");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
cpu_set_t coremask;
|
||||
CPU_ZERO(&coremask);
|
||||
for (uint8_t i = 0; i < sys_cfg->nr_threads; i++)
|
||||
{
|
||||
CPU_SET(sys_cfg->cpu_mask[i], &coremask);
|
||||
}
|
||||
|
||||
pkt_io->mr_ins = marsio_create();
|
||||
if (pkt_io->mr_ins == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create marsio instance");
|
||||
goto error_out;
|
||||
}
|
||||
marsio_option_set(pkt_io->mr_ins, MARSIO_OPT_THREAD_MASK_IN_CPUSET, &coremask, sizeof(coremask));
|
||||
marsio_option_set(pkt_io->mr_ins, MARSIO_OPT_EXIT_WHEN_ERR, &opt, sizeof(opt));
|
||||
if (marsio_init(pkt_io->mr_ins, sys_cfg->app_symbol) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init marsio instance");
|
||||
goto error_out;
|
||||
}
|
||||
pkt_io->mr_dev = marsio_open_device(pkt_io->mr_ins, sys_cfg->dev_symbol, sys_cfg->nr_threads, sys_cfg->nr_threads);
|
||||
if (pkt_io->mr_dev == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to open marsio device");
|
||||
goto error_out;
|
||||
}
|
||||
pkt_io->mr_path = marsio_sendpath_create_by_vdev(pkt_io->mr_dev);
|
||||
if (pkt_io->mr_path == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create marsio sendpath");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
return pkt_io;
|
||||
|
||||
error_out:
|
||||
packet_io_destroy(pkt_io);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* main
|
||||
******************************************************************************/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
memset(stellar_ctx_ptr, 0, sizeof(struct stellar_context));
|
||||
memset(stellar_ctx, 0, sizeof(struct stellar_context));
|
||||
timestamp_update();
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
@@ -404,17 +290,15 @@ int main(int argc, char **argv)
|
||||
|
||||
STELLAR_LOG_STATE("Start Stellar (version: %s)\n %s", __stellar_version, logo_str);
|
||||
|
||||
if (config_load(&stellar_ctx_ptr->config, stellar_config_file) != 0)
|
||||
if (config_load(&stellar_ctx->config, stellar_config_file) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to load config file");
|
||||
return -1;
|
||||
}
|
||||
|
||||
config_dump(&stellar_ctx_ptr->config);
|
||||
struct system_config *sys_cfg = &stellar_ctx_ptr->config.sys_cfg;
|
||||
uint8_t nr_threads = sys_cfg->nr_threads;
|
||||
config_dump(&stellar_ctx->config);
|
||||
|
||||
if (id_generator_init(sys_cfg->device_base, sys_cfg->device_offset) != 0)
|
||||
if (id_generator_init(dev_cfg->device_base, dev_cfg->device_offset) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init id generator");
|
||||
return -1;
|
||||
@@ -422,42 +306,40 @@ int main(int argc, char **argv)
|
||||
|
||||
// TODO load plugin
|
||||
|
||||
stellar_ctx_ptr->pkt_io = packet_io_create(sys_cfg);
|
||||
if (stellar_ctx_ptr->pkt_io == NULL)
|
||||
uint8_t nr_threads = pkt_io_cfg->nr_threads;
|
||||
stellar_ctx->packet_io = packet_io_create(pkt_io_cfg);
|
||||
if (stellar_ctx->packet_io == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create packet io");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (thread_context_init(stellar_ctx_ptr) != 0)
|
||||
if (thread_context_init(stellar_ctx, nr_threads) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to init thread context");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
if (thread_create(stellar_ctx_ptr->threads_ctx, nr_threads) != 0)
|
||||
if (thread_create(stellar_ctx->threads_ctx, nr_threads) != 0)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create worker thread");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
while (!ATOMIC_READ(&stellar_ctx_ptr->need_exit))
|
||||
while (!ATOMIC_READ(&stellar_ctx->need_exit))
|
||||
{
|
||||
timestamp_update();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
error_out:
|
||||
packet_io_destroy(stellar_ctx_ptr->pkt_io);
|
||||
|
||||
thread_destroy(stellar_ctx_ptr->threads_ctx, nr_threads);
|
||||
|
||||
thread_context_free(stellar_ctx_ptr);
|
||||
thread_destroy(stellar_ctx->threads_ctx, nr_threads);
|
||||
thread_context_free(stellar_ctx, nr_threads);
|
||||
packet_io_destroy(stellar_ctx->packet_io);
|
||||
|
||||
// TODO free plugin
|
||||
|
||||
id_generator_free();
|
||||
|
||||
log_free();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user