2024-09-14 15:20:59 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
#include "utils_internal.h"
|
2024-09-19 16:10:59 +08:00
|
|
|
#include "packet_internal.h"
|
2024-09-19 16:25:49 +08:00
|
|
|
#include "packet_manager_internal.h"
|
2024-10-11 06:08:50 +00:00
|
|
|
|
|
|
|
|
#define PACKET_MANAGER_MODULE_NAME "packet_manager_module"
|
2024-09-13 18:03:05 +08:00
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
#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
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
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;
|
2024-09-13 18:03:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct packet_manager_schema
|
|
|
|
|
{
|
2024-09-20 16:56:05 +08:00
|
|
|
struct exdata_schema *exdata;
|
2024-09-13 18:03:05 +08:00
|
|
|
struct mq_schema *mq;
|
|
|
|
|
int topic_id[PACKET_STAGE_MAX];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct packet_manager
|
|
|
|
|
{
|
2024-10-23 10:10:20 +08:00
|
|
|
uint16_t thread_num;
|
2024-09-13 18:03:05 +08:00
|
|
|
struct packet_manager_schema *schema;
|
|
|
|
|
struct packet_manager_runtime *runtime[MAX_THREAD_NUM];
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
const char *packet_stage_to_str(enum packet_stage stage)
|
|
|
|
|
{
|
|
|
|
|
switch (stage)
|
|
|
|
|
{
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-13 18:03:05 +08:00
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
void packet_manager_runtime_free(struct packet_manager_runtime *pkt_mgr_rt)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
if (pkt_mgr_rt)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
2024-10-23 10:01:20 +08:00
|
|
|
free(pkt_mgr_rt);
|
|
|
|
|
pkt_mgr_rt = NULL;
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:10:20 +08:00
|
|
|
struct packet_manager_runtime *packet_manager_runtime_new(struct mq_runtime *mq_rt)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
struct packet_manager_runtime *pkt_mgr_rt = calloc(1, sizeof(struct packet_manager_runtime));
|
|
|
|
|
if (pkt_mgr_rt == NULL)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to allocate memory for packet_manager_runtime");
|
2024-09-13 18:03:05 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
for (int i = 0; i < PACKET_QUEUE_MAX; i++)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
TAILQ_INIT(&pkt_mgr_rt->queue[i]);
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
2024-10-23 10:10:20 +08:00
|
|
|
pkt_mgr_rt->mq = mq_rt;
|
2024-09-13 18:03:05 +08:00
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
return pkt_mgr_rt;
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* packet manager schema
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
static void on_packet_stage_dispatch(int topic_id, void *msg, on_msg_cb_func *cb, void *cb_arg, void *dispatch_arg)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-09-14 15:20:59 +08:00
|
|
|
assert(msg);
|
|
|
|
|
assert(dispatch_arg);
|
|
|
|
|
|
2024-09-13 18:03:05 +08:00
|
|
|
struct packet_manager_schema *schema = (struct packet_manager_schema *)dispatch_arg;
|
|
|
|
|
struct packet *pkt = (struct packet *)msg;
|
|
|
|
|
|
|
|
|
|
enum packet_stage stage = PACKET_STAGE_MAX;
|
|
|
|
|
for (int i = 0; i < PACKET_STAGE_MAX; i++)
|
|
|
|
|
{
|
|
|
|
|
if (schema->topic_id[i] == topic_id)
|
|
|
|
|
{
|
|
|
|
|
stage = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
((on_packet_stage_callback *)(void *)cb)(stage, pkt, cb_arg);
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
static void packet_schema_free(struct packet_manager_schema *pkt_mgr_schema)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-09-14 15:20:59 +08:00
|
|
|
if (pkt_mgr_schema)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-09-14 15:20:59 +08:00
|
|
|
if (pkt_mgr_schema->mq)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
|
|
|
|
for (int i = 0; i < PACKET_STAGE_MAX; i++)
|
|
|
|
|
{
|
2024-09-14 15:20:59 +08:00
|
|
|
if (pkt_mgr_schema->topic_id[i] >= 0)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-09-14 15:20:59 +08:00
|
|
|
mq_schema_destroy_topic(pkt_mgr_schema->mq, pkt_mgr_schema->topic_id[i]);
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
if (pkt_mgr_schema->exdata)
|
|
|
|
|
{
|
|
|
|
|
exdata_schema_free(pkt_mgr_schema->exdata);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 15:20:59 +08:00
|
|
|
free(pkt_mgr_schema);
|
|
|
|
|
pkt_mgr_schema = NULL;
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
static struct packet_manager_schema *packet_schema_new(struct mq_schema *mq)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-09-14 15:20:59 +08:00
|
|
|
struct packet_manager_schema *pkt_mgr_schema = calloc(1, sizeof(struct packet_manager_schema));
|
|
|
|
|
if (pkt_mgr_schema == NULL)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to allocate memory for packet_schema");
|
2024-09-13 18:03:05 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
pkt_mgr_schema->exdata = exdata_schema_new();
|
|
|
|
|
if (pkt_mgr_schema->exdata == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to create exdata_schema");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-09-13 18:03:05 +08:00
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
pkt_mgr_schema->mq = mq;
|
2024-09-13 18:03:05 +08:00
|
|
|
for (int i = 0; i < PACKET_STAGE_MAX; i++)
|
|
|
|
|
{
|
2024-09-20 16:56:05 +08:00
|
|
|
pkt_mgr_schema->topic_id[i] = mq_schema_create_topic(pkt_mgr_schema->mq, packet_stage_to_str(i), &on_packet_stage_dispatch, pkt_mgr_schema, NULL, NULL);
|
2024-09-14 15:20:59 +08:00
|
|
|
if (pkt_mgr_schema->topic_id[i] < 0)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-09-14 15:20:59 +08:00
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to create topic %s", packet_stage_to_str(i));
|
2024-09-13 18:03:05 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 15:20:59 +08:00
|
|
|
return pkt_mgr_schema;
|
2024-09-13 18:03:05 +08:00
|
|
|
|
|
|
|
|
error_out:
|
2024-10-23 10:01:20 +08:00
|
|
|
packet_schema_free(pkt_mgr_schema);
|
2024-09-13 18:03:05 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2024-09-14 15:20:59 +08:00
|
|
|
* packet manager
|
2024-09-13 18:03:05 +08:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-10-23 10:10:20 +08:00
|
|
|
struct packet_manager *packet_manager_new(struct mq_schema *mq_schema, uint16_t thread_num)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
|
|
|
|
struct packet_manager *pkt_mgr = calloc(1, sizeof(struct packet_manager));
|
|
|
|
|
if (pkt_mgr == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to allocate memory for packet_manager");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
pkt_mgr->thread_num = thread_num;
|
|
|
|
|
pkt_mgr->schema = packet_schema_new(mq_schema);
|
2024-09-13 18:03:05 +08:00
|
|
|
if (pkt_mgr->schema == NULL)
|
|
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to create packet_schema");
|
2024-09-13 18:03:05 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pkt_mgr;
|
|
|
|
|
|
|
|
|
|
error_out:
|
|
|
|
|
packet_manager_free(pkt_mgr);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_manager_free(struct packet_manager *pkt_mgr)
|
|
|
|
|
{
|
|
|
|
|
if (pkt_mgr)
|
|
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
packet_schema_free(pkt_mgr->schema);
|
2024-09-14 15:20:59 +08:00
|
|
|
|
2024-09-13 18:03:05 +08:00
|
|
|
free(pkt_mgr);
|
|
|
|
|
pkt_mgr = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
int packet_manager_new_packet_exdata_index(struct packet_manager *pkt_mgr, const char *name, exdata_free *func, void *arg)
|
|
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
assert(pkt_mgr);
|
2024-09-20 16:56:05 +08:00
|
|
|
return exdata_schema_new_index(pkt_mgr->schema->exdata, name, func, arg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
int packet_manager_subscribe(struct packet_manager *pkt_mgr, enum packet_stage stage, on_packet_stage_callback *cb, void *arg)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
assert(pkt_mgr);
|
|
|
|
|
return mq_schema_subscribe(pkt_mgr->schema->mq, pkt_mgr->schema->topic_id[stage], (on_msg_cb_func *)cb, arg);
|
2024-09-19 17:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
int packet_manager_init(struct packet_manager *pkt_mgr, uint16_t thread_id, struct mq_runtime *mq_rt)
|
2024-09-19 17:26:34 +08:00
|
|
|
{
|
2024-10-09 10:01:20 +08:00
|
|
|
assert(pkt_mgr);
|
2024-10-23 10:01:20 +08:00
|
|
|
assert(thread_id < pkt_mgr->thread_num);
|
2024-10-09 10:01:20 +08:00
|
|
|
assert(mq_rt);
|
2024-09-19 17:26:34 +08:00
|
|
|
|
2024-10-23 10:10:20 +08:00
|
|
|
struct packet_manager_runtime *pkt_mgr_rt = packet_manager_runtime_new(mq_rt);
|
|
|
|
|
if (pkt_mgr_rt == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to create packet_manager_runtime");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pkt_mgr->runtime[thread_id] = pkt_mgr_rt;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-23 10:01:20 +08:00
|
|
|
|
2024-10-23 10:10:20 +08:00
|
|
|
void packet_manager_clean(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
|
|
|
|
{
|
|
|
|
|
assert(pkt_mgr);
|
|
|
|
|
assert(thread_id < pkt_mgr->thread_num);
|
|
|
|
|
|
|
|
|
|
struct packet_manager_runtime *pkt_mgr_rt = pkt_mgr->runtime[thread_id];
|
|
|
|
|
PACKET_MANAGER_LOG_INFO("runtime: %p, idx: %d, will be cleaned", pkt_mgr_rt, thread_id);
|
|
|
|
|
packet_manager_print_stat(pkt_mgr, thread_id);
|
|
|
|
|
packet_manager_runtime_free(pkt_mgr_rt);
|
|
|
|
|
pkt_mgr_rt = NULL;
|
2024-09-19 17:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
2024-10-23 10:01:20 +08:00
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
struct exdata_runtime *exdata_rt = exdata_runtime_new(pkt_mgr->schema->exdata);
|
|
|
|
|
packet_set_user_data(pkt, exdata_rt);
|
2024-09-19 17:26:34 +08:00
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
runtime->stat.total.pkts_ingress++;
|
|
|
|
|
runtime->stat.queue[PACKET_STAGE_PREROUTING].pkts_in++;
|
|
|
|
|
TAILQ_INSERT_TAIL(&runtime->queue[PACKET_STAGE_PREROUTING], pkt, stage_tqe);
|
2024-09-19 17:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct packet *packet_manager_egress(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
|
|
|
|
{
|
|
|
|
|
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
2024-10-23 10:01:20 +08:00
|
|
|
|
|
|
|
|
struct packet *pkt = TAILQ_FIRST(&runtime->queue[PACKET_STAGE_MAX]);
|
2024-09-19 17:26:34 +08:00
|
|
|
if (pkt)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
runtime->stat.total.pkts_egress++;
|
|
|
|
|
runtime->stat.queue[PACKET_STAGE_MAX].pkts_out++;
|
|
|
|
|
TAILQ_REMOVE(&runtime->queue[PACKET_STAGE_MAX], pkt, stage_tqe);
|
|
|
|
|
|
2024-09-20 16:56:05 +08:00
|
|
|
struct exdata_runtime *exdata_rt = packet_get_user_data(pkt);
|
|
|
|
|
exdata_runtime_free(exdata_rt);
|
2024-10-23 10:01:20 +08:00
|
|
|
return pkt;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
2024-09-19 17:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_manager_dispatch(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
|
|
|
|
{
|
|
|
|
|
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
2024-10-23 10:01:20 +08:00
|
|
|
|
|
|
|
|
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;
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
int packet_manager_claim_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, on_packet_claimed_callback cb, void *arg)
|
2024-09-13 18:03:05 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
assert(pkt_mgr);
|
2024-09-19 17:26:34 +08:00
|
|
|
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
2024-10-23 10:01:20 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-09-19 17:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_manager_schedule_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, enum packet_stage stage)
|
|
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
assert(pkt_mgr);
|
2024-09-19 17:26:34 +08:00
|
|
|
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
2024-10-23 10:01:20 +08:00
|
|
|
|
|
|
|
|
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);
|
2024-09-19 17:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
2024-10-23 10:01:20 +08:00
|
|
|
return &runtime->stat;
|
2024-09-19 17:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_manager_print_stat(struct packet_manager *pkt_mgr, uint16_t thread_id)
|
|
|
|
|
{
|
|
|
|
|
struct packet_manager_runtime *runtime = pkt_mgr->runtime[thread_id];
|
2024-10-23 10:01:20 +08:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-09-13 18:03:05 +08:00
|
|
|
}
|
2024-10-09 10:01:20 +08:00
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* packet manager module
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-10-11 06:08:50 +00:00
|
|
|
struct packet_manager *stellar_module_get_packet_manager(struct stellar_module_manager *mod_mgr)
|
|
|
|
|
{
|
|
|
|
|
assert(mod_mgr);
|
|
|
|
|
struct stellar_module *pkt_mgr_mod = stellar_module_manager_get_module(mod_mgr, PACKET_MANAGER_MODULE_NAME);
|
|
|
|
|
if (pkt_mgr_mod == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-10-23 10:01:20 +08:00
|
|
|
return (struct packet_manager *)stellar_module_get_ctx(pkt_mgr_mod);
|
2024-10-11 06:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-09 10:01:20 +08:00
|
|
|
struct stellar_module *packet_manager_on_init(struct stellar_module_manager *mod_mgr)
|
|
|
|
|
{
|
|
|
|
|
assert(mod_mgr);
|
|
|
|
|
struct mq_schema *mq_schema = stellar_module_manager_get_mq_schema(mod_mgr);
|
|
|
|
|
assert(mq_schema);
|
2024-10-23 10:10:20 +08:00
|
|
|
uint16_t thread_num = stellar_module_manager_get_max_thread_num(mod_mgr);
|
2024-10-09 10:01:20 +08:00
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, thread_num);
|
2024-10-09 10:01:20 +08:00
|
|
|
if (pkt_mgr == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct stellar_module *pkt_mgr_mod = stellar_module_new(PACKET_MANAGER_MODULE_NAME, NULL);
|
|
|
|
|
if (pkt_mgr_mod == NULL)
|
|
|
|
|
{
|
|
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to create packet_manager");
|
|
|
|
|
packet_manager_free(pkt_mgr);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
stellar_module_set_ctx(pkt_mgr_mod, pkt_mgr);
|
|
|
|
|
|
2024-10-24 16:22:18 +08:00
|
|
|
PACKET_MANAGER_LOG_FATAL("packet_manager init");
|
2024-10-09 10:01:20 +08:00
|
|
|
return pkt_mgr_mod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_manager_on_exit(struct stellar_module_manager *mod_mgr __attribute__((unused)), struct stellar_module *mod)
|
|
|
|
|
{
|
|
|
|
|
if (mod)
|
|
|
|
|
{
|
|
|
|
|
struct packet_manager *pkt_mgr = stellar_module_get_ctx(mod);
|
|
|
|
|
|
|
|
|
|
packet_manager_free(pkt_mgr);
|
|
|
|
|
stellar_module_free(mod);
|
2024-10-24 16:22:18 +08:00
|
|
|
PACKET_MANAGER_LOG_FATAL("packet_manager exit");
|
2024-10-09 10:01:20 +08:00
|
|
|
}
|
2024-10-23 10:10:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct stellar_module *packet_manager_on_thread_init(struct stellar_module_manager *mod_mgr, int thread_id, struct stellar_module *mod)
|
|
|
|
|
{
|
|
|
|
|
struct packet_manager *pkt_mgr = stellar_module_get_ctx(mod);
|
|
|
|
|
assert(pkt_mgr);
|
|
|
|
|
struct mq_runtime *mq_rt = stellar_module_manager_get_mq_runtime(mod_mgr);
|
|
|
|
|
assert(mq_rt);
|
|
|
|
|
assert(thread_id < pkt_mgr->thread_num);
|
|
|
|
|
|
|
|
|
|
if (packet_manager_init(pkt_mgr, thread_id, mq_rt) != 0)
|
|
|
|
|
{
|
|
|
|
|
PACKET_MANAGER_LOG_ERROR("failed to init packet_manager_init");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return mod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void packet_manager_on_thread_exit(struct stellar_module_manager *mod_mgr __attribute__((unused)), int thread_id, struct stellar_module *mod)
|
|
|
|
|
{
|
|
|
|
|
struct packet_manager *pkt_mgr = stellar_module_get_ctx(mod);
|
|
|
|
|
assert(pkt_mgr);
|
|
|
|
|
assert(thread_id < pkt_mgr->thread_num);
|
|
|
|
|
|
|
|
|
|
packet_manager_clean(pkt_mgr, thread_id);
|
2024-10-09 10:01:20 +08:00
|
|
|
}
|