feature: packet IO support IP reassembly
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "utils_internal.h"
|
||||
#include "packet_internal.h"
|
||||
#include "packet_manager_runtime.h"
|
||||
#include "packet_manager_internal.h"
|
||||
|
||||
#define PACKET_MANAGER_MODULE_NAME "packet_manager_module"
|
||||
|
||||
struct packet_manager_config
|
||||
#define PACKET_MANAGER_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet manager", format, ##__VA_ARGS__)
|
||||
#define PACKET_MANAGER_LOG_FATAL(format, ...) STELLAR_LOG_FATAL(__thread_local_logger, "packet manager", format, ##__VA_ARGS__)
|
||||
#define PACKET_MANAGER_LOG_INFO(format, ...) STELLAR_LOG_INFO(__thread_local_logger, "packet manager", format, ##__VA_ARGS__)
|
||||
|
||||
struct packet_manager_runtime
|
||||
{
|
||||
uint16_t nr_worker_thread;
|
||||
enum packet_stage curr_stage;
|
||||
struct packet_queue queue[PACKET_QUEUE_MAX];
|
||||
|
||||
void *claim_arg;
|
||||
on_packet_claimed_callback *claim_cb;
|
||||
|
||||
struct mq_runtime *mq;
|
||||
struct packet_manager_stat stat;
|
||||
};
|
||||
|
||||
struct packet_manager_schema
|
||||
@@ -21,43 +31,63 @@ struct packet_manager_schema
|
||||
|
||||
struct packet_manager
|
||||
{
|
||||
struct packet_manager_config *cfg;
|
||||
uint64_t thread_num;
|
||||
struct packet_manager_schema *schema;
|
||||
struct packet_manager_runtime *runtime[MAX_THREAD_NUM];
|
||||
};
|
||||
|
||||
/******************************************************************************
|
||||
* packet manager config
|
||||
******************************************************************************/
|
||||
|
||||
static void packet_manager_config_free(struct packet_manager_config *cfg)
|
||||
const char *packet_stage_to_str(enum packet_stage stage)
|
||||
{
|
||||
if (cfg)
|
||||
switch (stage)
|
||||
{
|
||||
free(cfg);
|
||||
cfg = NULL;
|
||||
case PACKET_STAGE_PREROUTING:
|
||||
return "PACKET_STAGE_PREROUTING";
|
||||
case PACKET_STAGE_INPUT:
|
||||
return "PACKET_STAGE_INPUT";
|
||||
case PACKET_STAGE_FORWARD:
|
||||
return "PACKET_STAGE_FORWARD";
|
||||
case PACKET_STAGE_OUTPUT:
|
||||
return "PACKET_STAGE_OUTPUT";
|
||||
case PACKET_STAGE_POSTROUTING:
|
||||
return "PACKET_STAGE_POSTROUTING";
|
||||
default:
|
||||
return "PACKET_STAGE_UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
static struct packet_manager_config *packet_manager_config_new(const char *toml_file)
|
||||
void packet_manager_runtime_free(struct packet_manager_runtime *pkt_mgr_rt)
|
||||
{
|
||||
struct packet_manager_config *cfg = calloc(1, sizeof(struct packet_manager_config));
|
||||
if (cfg == NULL)
|
||||
if (pkt_mgr_rt)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("failed to allocate memory for packet_manager_config");
|
||||
for (int i = 0; i < PACKET_QUEUE_MAX; i++)
|
||||
{
|
||||
struct packet *pkt = NULL;
|
||||
while ((pkt = TAILQ_FIRST(&pkt_mgr_rt->queue[i])))
|
||||
{
|
||||
TAILQ_REMOVE(&pkt_mgr_rt->queue[i], pkt, stage_tqe);
|
||||
packet_free(pkt);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(pkt_mgr_rt);
|
||||
pkt_mgr_rt = NULL;
|
||||
}
|
||||
|
||||
struct packet_manager_runtime *packet_manager_runtime_new()
|
||||
{
|
||||
struct packet_manager_runtime *pkt_mgr_rt = calloc(1, sizeof(struct packet_manager_runtime));
|
||||
if (pkt_mgr_rt == NULL)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("failed to allocate memory for packet_manager_runtime");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint64_t val = 0;
|
||||
if (load_and_validate_toml_integer_config(toml_file, "packet_io.nr_worker_thread", &val, 1, MAX_THREAD_NUM) != 0)
|
||||
for (int i = 0; i < PACKET_QUEUE_MAX; i++)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("failed to load packet_io.nr_worker_thread from %s", toml_file);
|
||||
free(cfg);
|
||||
return NULL;
|
||||
TAILQ_INIT(&pkt_mgr_rt->queue[i]);
|
||||
}
|
||||
cfg->nr_worker_thread = val;
|
||||
|
||||
return cfg;
|
||||
return pkt_mgr_rt;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
@@ -85,7 +115,7 @@ static void on_packet_stage_dispatch(int topic_id, void *msg, on_msg_cb_func *cb
|
||||
((on_packet_stage_callback *)(void *)cb)(stage, pkt, cb_arg);
|
||||
}
|
||||
|
||||
static void packet_manager_schema_free(struct packet_manager_schema *pkt_mgr_schema)
|
||||
static void packet_schema_free(struct packet_manager_schema *pkt_mgr_schema)
|
||||
{
|
||||
if (pkt_mgr_schema)
|
||||
{
|
||||
@@ -110,12 +140,12 @@ static void packet_manager_schema_free(struct packet_manager_schema *pkt_mgr_sch
|
||||
}
|
||||
}
|
||||
|
||||
static struct packet_manager_schema *packet_manager_schema_new(struct mq_schema *mq)
|
||||
static struct packet_manager_schema *packet_schema_new(struct mq_schema *mq)
|
||||
{
|
||||
struct packet_manager_schema *pkt_mgr_schema = calloc(1, sizeof(struct packet_manager_schema));
|
||||
if (pkt_mgr_schema == NULL)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("failed to allocate memory for packet_manager_schema");
|
||||
PACKET_MANAGER_LOG_ERROR("failed to allocate memory for packet_schema");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -140,7 +170,7 @@ static struct packet_manager_schema *packet_manager_schema_new(struct mq_schema
|
||||
return pkt_mgr_schema;
|
||||
|
||||
error_out:
|
||||
packet_manager_schema_free(pkt_mgr_schema);
|
||||
packet_schema_free(pkt_mgr_schema);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -148,7 +178,7 @@ error_out:
|
||||
* packet manager
|
||||
******************************************************************************/
|
||||
|
||||
struct packet_manager *packet_manager_new(struct mq_schema *mq, const char *toml_file)
|
||||
struct packet_manager *packet_manager_new(struct mq_schema *mq_schema, uint64_t thread_num)
|
||||
{
|
||||
struct packet_manager *pkt_mgr = calloc(1, sizeof(struct packet_manager));
|
||||
if (pkt_mgr == NULL)
|
||||
@@ -157,21 +187,15 @@ struct packet_manager *packet_manager_new(struct mq_schema *mq, const char *toml
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pkt_mgr->cfg = packet_manager_config_new(toml_file);
|
||||
if (pkt_mgr->cfg == NULL)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("failed to create packet_manager_config");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
pkt_mgr->schema = packet_manager_schema_new(mq);
|
||||
pkt_mgr->thread_num = thread_num;
|
||||
pkt_mgr->schema = packet_schema_new(mq_schema);
|
||||
if (pkt_mgr->schema == NULL)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("failed to create packet_manager_schema");
|
||||
PACKET_MANAGER_LOG_ERROR("failed to create packet_schema");
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < pkt_mgr->cfg->nr_worker_thread; i++)
|
||||
for (uint16_t i = 0; i < pkt_mgr->thread_num; i++)
|
||||
{
|
||||
pkt_mgr->runtime[i] = packet_manager_runtime_new();
|
||||
if (pkt_mgr->runtime[i] == NULL)
|
||||
@@ -194,22 +218,18 @@ void packet_manager_free(struct packet_manager *pkt_mgr)
|
||||
|
||||
if (pkt_mgr)
|
||||
{
|
||||
if (pkt_mgr->cfg)
|
||||
|
||||
for (uint16_t i = 0; i < pkt_mgr->thread_num; i++)
|
||||
{
|
||||
for (uint16_t i = 0; i < pkt_mgr->cfg->nr_worker_thread; i++)
|
||||
pkt_mgr_rt = pkt_mgr->runtime[i];
|
||||
if (pkt_mgr_rt)
|
||||
{
|
||||
pkt_mgr_rt = pkt_mgr->runtime[i];
|
||||
if (pkt_mgr_rt)
|
||||
{
|
||||
PACKET_MANAGER_LOG_INFO("runtime: %p, idx: %d, will be cleaned", pkt_mgr_rt, i);
|
||||
packet_manager_runtime_print_stat(pkt_mgr_rt);
|
||||
packet_manager_runtime_free(pkt_mgr_rt);
|
||||
}
|
||||
packet_manager_print_stat(pkt_mgr, i);
|
||||
packet_manager_runtime_free(pkt_mgr_rt);
|
||||
}
|
||||
}
|
||||
|
||||
packet_manager_schema_free(pkt_mgr->schema);
|
||||
packet_manager_config_free(pkt_mgr->cfg);
|
||||
packet_schema_free(pkt_mgr->schema);
|
||||
|
||||
free(pkt_mgr);
|
||||
pkt_mgr = NULL;
|
||||
@@ -218,73 +238,158 @@ void packet_manager_free(struct packet_manager *pkt_mgr)
|
||||
|
||||
int packet_manager_new_packet_exdata_index(struct packet_manager *pkt_mgr, const char *name, exdata_free *func, void *arg)
|
||||
{
|
||||
assert(pkt_mgr);
|
||||
return exdata_schema_new_index(pkt_mgr->schema->exdata, name, func, arg);
|
||||
}
|
||||
|
||||
int packet_manager_subscribe(struct packet_manager *pkt_mgr, enum packet_stage stage, on_packet_stage_callback *cb, void *args)
|
||||
{
|
||||
return mq_schema_subscribe(pkt_mgr->schema->mq, pkt_mgr->schema->topic_id[stage], (on_msg_cb_func *)cb, args);
|
||||
}
|
||||
|
||||
void packet_manager_init(struct packet_manager *pkt_mgr, uint16_t thread_id, struct mq_runtime *mq_rt)
|
||||
int packet_manager_subscribe(struct packet_manager *pkt_mgr, enum packet_stage stage, on_packet_stage_callback *cb, void *arg)
|
||||
{
|
||||
assert(pkt_mgr);
|
||||
assert(thread_id < pkt_mgr->cfg->nr_worker_thread);
|
||||
return mq_schema_subscribe(pkt_mgr->schema->mq, pkt_mgr->schema->topic_id[stage], (on_msg_cb_func *)cb, arg);
|
||||
}
|
||||
|
||||
int packet_manager_init(struct packet_manager *pkt_mgr, uint16_t thread_id, struct mq_runtime *mq_rt)
|
||||
{
|
||||
assert(pkt_mgr);
|
||||
assert(thread_id < pkt_mgr->thread_num);
|
||||
assert(mq_rt);
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
|
||||
packet_manager_runtime_init(runtime, mq_rt);
|
||||
runtime->mq = mq_rt;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void packet_manager_ingress(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt)
|
||||
{
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
|
||||
struct exdata_runtime *exdata_rt = exdata_runtime_new(pkt_mgr->schema->exdata);
|
||||
packet_set_user_data(pkt, exdata_rt);
|
||||
|
||||
packet_manager_runtime_ingress(runtime, pkt);
|
||||
runtime->stat.total.pkts_ingress++;
|
||||
runtime->stat.queue[PACKET_STAGE_PREROUTING].pkts_in++;
|
||||
TAILQ_INSERT_TAIL(&runtime->queue[PACKET_STAGE_PREROUTING], pkt, stage_tqe);
|
||||
}
|
||||
|
||||
struct packet *packet_manager_egress(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
||||
{
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
struct packet *pkt = packet_manager_runtime_egress(runtime);
|
||||
|
||||
struct packet *pkt = TAILQ_FIRST(&runtime->queue[PACKET_STAGE_MAX]);
|
||||
if (pkt)
|
||||
{
|
||||
runtime->stat.total.pkts_egress++;
|
||||
runtime->stat.queue[PACKET_STAGE_MAX].pkts_out++;
|
||||
TAILQ_REMOVE(&runtime->queue[PACKET_STAGE_MAX], pkt, stage_tqe);
|
||||
|
||||
struct exdata_runtime *exdata_rt = packet_get_user_data(pkt);
|
||||
exdata_runtime_free(exdata_rt);
|
||||
return pkt;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return pkt;
|
||||
}
|
||||
|
||||
void packet_manager_dispatch(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
||||
{
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
packet_manager_runtime_dispatch(runtime);
|
||||
|
||||
for (int i = 0; i < PACKET_STAGE_MAX; i++)
|
||||
{
|
||||
runtime->curr_stage = i;
|
||||
|
||||
struct packet *pkt = NULL;
|
||||
while ((pkt = TAILQ_FIRST(&runtime->queue[runtime->curr_stage])))
|
||||
{
|
||||
packet_set_claim(pkt, false);
|
||||
runtime->claim_cb = NULL;
|
||||
runtime->claim_arg = NULL;
|
||||
|
||||
TAILQ_REMOVE(&runtime->queue[runtime->curr_stage], pkt, stage_tqe);
|
||||
runtime->stat.queue[runtime->curr_stage].pkts_out++;
|
||||
|
||||
mq_runtime_publish_message(runtime->mq, runtime->curr_stage, pkt);
|
||||
mq_runtime_dispatch(runtime->mq);
|
||||
|
||||
if (packet_is_claim(pkt))
|
||||
{
|
||||
if (runtime->claim_cb)
|
||||
{
|
||||
runtime->claim_cb(pkt, runtime->claim_arg);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
TAILQ_INSERT_TAIL(&runtime->queue[runtime->curr_stage + 1], pkt, stage_tqe);
|
||||
runtime->stat.queue[runtime->curr_stage + 1].pkts_in++;
|
||||
}
|
||||
}
|
||||
runtime->curr_stage = -1;
|
||||
}
|
||||
|
||||
int packet_manager_claim_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, on_packet_claimed_callback cb, void *args)
|
||||
int packet_manager_claim_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, on_packet_claimed_callback cb, void *arg)
|
||||
{
|
||||
assert(pkt_mgr);
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
return packet_manager_runtime_claim_packet(runtime, pkt, cb, args);
|
||||
|
||||
if (packet_is_claim(pkt))
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("packet is already claimed, cannot claim again");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
runtime->claim_cb = cb;
|
||||
runtime->claim_arg = arg;
|
||||
packet_set_claim(pkt, true);
|
||||
runtime->stat.queue[runtime->curr_stage].pkts_claim++;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void packet_manager_schedule_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, enum packet_stage stage)
|
||||
{
|
||||
assert(pkt_mgr);
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
packet_manager_runtime_schedule_packet(runtime, pkt, stage);
|
||||
|
||||
if (stage >= PACKET_STAGE_MAX)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("invalid stage %d", stage);
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
runtime->stat.queue[stage].pkts_schedule++;
|
||||
runtime->stat.queue[stage].pkts_in++;
|
||||
TAILQ_INSERT_TAIL(&runtime->queue[stage], pkt, stage_tqe);
|
||||
}
|
||||
|
||||
struct packet_manager_stat *packet_manager_get_stat(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
||||
{
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
return packet_manager_runtime_get_stat(runtime);
|
||||
return &runtime->stat;
|
||||
}
|
||||
|
||||
void packet_manager_print_stat(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
||||
{
|
||||
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
||||
packet_manager_runtime_print_stat(runtime);
|
||||
|
||||
PACKET_MANAGER_LOG_INFO("runtime: %p, pkts_ingress: %lu, pkts_egress: %lu",
|
||||
runtime, runtime->stat.total.pkts_ingress,
|
||||
runtime->stat.total.pkts_egress);
|
||||
for (int i = 0; i < PACKET_QUEUE_MAX; i++)
|
||||
{
|
||||
PACKET_MANAGER_LOG_INFO("runtime: %p, %-24s stat => pkts_in: %lu, pkts_out: %lu, pkts_claim: %lu, pkts_schedule: %lu",
|
||||
runtime,
|
||||
packet_stage_to_str(i),
|
||||
runtime->stat.queue[i].pkts_in,
|
||||
runtime->stat.queue[i].pkts_out,
|
||||
runtime->stat.queue[i].pkts_claim,
|
||||
runtime->stat.queue[i].pkts_schedule);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
@@ -299,7 +404,7 @@ struct packet_manager *stellar_module_get_packet_manager(struct stellar_module_m
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return (struct packet_manager*)stellar_module_get_ctx(pkt_mgr_mod);
|
||||
return (struct packet_manager *)stellar_module_get_ctx(pkt_mgr_mod);
|
||||
}
|
||||
|
||||
struct stellar_module *packet_manager_on_init(struct stellar_module_manager *mod_mgr)
|
||||
@@ -307,10 +412,9 @@ struct stellar_module *packet_manager_on_init(struct stellar_module_manager *mod
|
||||
assert(mod_mgr);
|
||||
struct mq_schema *mq_schema = stellar_module_manager_get_mq_schema(mod_mgr);
|
||||
assert(mq_schema);
|
||||
const char *toml_file = stellar_module_manager_get_toml_path(mod_mgr);
|
||||
assert(toml_file);
|
||||
uint64_t thread_num = stellar_module_manager_get_max_thread_num(mod_mgr);
|
||||
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, toml_file);
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, thread_num);
|
||||
if (pkt_mgr == NULL)
|
||||
{
|
||||
return NULL;
|
||||
|
||||
Reference in New Issue
Block a user