feature: packet IO support IP reassembly
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
add_library(packet_manager
|
||||
packet_manager_runtime.c
|
||||
packet_manager.c
|
||||
packet_pool.c
|
||||
packet_parser.c
|
||||
packet_builder.c
|
||||
packet_filter.c
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
#include "packet_parser.h"
|
||||
#include "packet_internal.h"
|
||||
|
||||
#define PACKET_CRAFT_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "packet craft", format, ##__VA_ARGS__)
|
||||
#define PACKET_CRAFT_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet craft", format, ##__VA_ARGS__)
|
||||
#define PACKET_BUILD_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet build", format, ##__VA_ARGS__)
|
||||
|
||||
struct fingerprint
|
||||
{
|
||||
@@ -79,7 +78,7 @@ static void update_gtp1_hdr(struct gtp1_hdr *gtp, int trim_len)
|
||||
gtp1_hdr_set_msg_len(gtp, msg_len - trim_len);
|
||||
if (gtp1_hdr_get_seq_flag(gtp) && gtp1_hdr_get_seq(gtp))
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv1 layer requires a sequence number");
|
||||
PACKET_BUILD_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv1 layer requires a sequence number");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +88,7 @@ static void update_gtp2_hdr(struct gtp2_hdr *gtp, int trim_len)
|
||||
gtp2_hdr_set_msg_len(gtp, msg_len - trim_len);
|
||||
if (gtp2_hdr_get_seq(gtp))
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv2 layer requires a sequence number");
|
||||
PACKET_BUILD_LOG_ERROR("build packets may be dropped by intermediate devices, the GTPv2 layer requires a sequence number");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,7 +255,7 @@ struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_se
|
||||
(tcp_payload == NULL && tcp_payload_len != 0) || (tcp_payload != NULL && tcp_payload_len == 0) ||
|
||||
(tcp_options_len && tcp_options_len % 4 != 0))
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, invalid arguments");
|
||||
PACKET_BUILD_LOG_ERROR("build TCP packet failed, invalid arguments");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -265,7 +264,7 @@ struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_se
|
||||
const struct layer_private *tcp_layer = packet_get_layer(origin_pkt, layer_count - 1);
|
||||
if (tcp_layer == NULL || tcp_layer->proto != LAYER_PROTO_TCP)
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, the innermost layer of the original packet is not TCP");
|
||||
PACKET_BUILD_LOG_ERROR("build TCP packet failed, the innermost layer of the original packet is not TCP");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -275,7 +274,7 @@ struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_se
|
||||
struct packet *new_pkt = packet_new(new_pkt_len);
|
||||
if (new_pkt == NULL)
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft TCP packet failed, no space to allocate new packet");
|
||||
PACKET_BUILD_LOG_ERROR("build TCP packet failed, no space to allocate new packet");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -297,7 +296,14 @@ struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_se
|
||||
|
||||
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
|
||||
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
|
||||
new_pkt->meta.origin_ctx = NULL;
|
||||
struct packet_origin origin = {
|
||||
.type = ORIGIN_TYPE_USER,
|
||||
.ctx = NULL,
|
||||
.cb = NULL,
|
||||
.args = NULL,
|
||||
.thr_idx = -1,
|
||||
};
|
||||
packet_set_origin(new_pkt, &origin);
|
||||
|
||||
return new_pkt;
|
||||
}
|
||||
@@ -307,7 +313,7 @@ struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp
|
||||
// check arguments
|
||||
if (origin_pkt == NULL || (udp_payload == NULL && udp_payload_len != 0) || (udp_payload != NULL && udp_payload_len == 0))
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, invalid arguments");
|
||||
PACKET_BUILD_LOG_ERROR("build UDP packet failed, invalid arguments");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -316,7 +322,7 @@ struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp
|
||||
const struct layer_private *udp_layer = packet_get_layer(origin_pkt, layer_count - 1);
|
||||
if (udp_layer == NULL || udp_layer->proto != LAYER_PROTO_UDP)
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, the innermost layer of the original packet is not UDP");
|
||||
PACKET_BUILD_LOG_ERROR("build UDP packet failed, the innermost layer of the original packet is not UDP");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -326,7 +332,7 @@ struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp
|
||||
struct packet *new_pkt = packet_new(new_pkt_len);
|
||||
if (new_pkt == NULL)
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft UDP packet failed, no space to allocate new packet");
|
||||
PACKET_BUILD_LOG_ERROR("build UDP packet failed, no space to allocate new packet");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -339,7 +345,14 @@ struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp
|
||||
|
||||
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
|
||||
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
|
||||
new_pkt->meta.origin_ctx = NULL;
|
||||
struct packet_origin origin = {
|
||||
.type = ORIGIN_TYPE_USER,
|
||||
.ctx = NULL,
|
||||
.cb = NULL,
|
||||
.args = NULL,
|
||||
.thr_idx = -1,
|
||||
};
|
||||
packet_set_origin(new_pkt, &origin);
|
||||
|
||||
return new_pkt;
|
||||
}
|
||||
@@ -348,7 +361,7 @@ struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto
|
||||
{
|
||||
if (origin_pkt == NULL || (l3_payload == NULL && l3_payload_len != 0) || (l3_payload != NULL && l3_payload_len == 0))
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, invalid arguments");
|
||||
PACKET_BUILD_LOG_ERROR("build L3 packet failed, invalid arguments");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -369,7 +382,7 @@ struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto
|
||||
}
|
||||
if (l3_layer == NULL)
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, the original packet does not contain an IP layer");
|
||||
PACKET_BUILD_LOG_ERROR("build L3 packet failed, the original packet does not contain an IP layer");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -382,7 +395,7 @@ struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto
|
||||
struct packet *new_pkt = packet_new(new_pkt_len);
|
||||
if (new_pkt == NULL)
|
||||
{
|
||||
PACKET_CRAFT_LOG_ERROR("craft L3 packet failed, no space to allocate new packet");
|
||||
PACKET_BUILD_LOG_ERROR("build L3 packet failed, no space to allocate new packet");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -408,7 +421,14 @@ struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto
|
||||
|
||||
packet_parse(new_pkt, new_pkt_data, new_pkt_len);
|
||||
memcpy(&new_pkt->meta, &origin_pkt->meta, sizeof(struct metadata));
|
||||
new_pkt->meta.origin_ctx = NULL;
|
||||
struct packet_origin origin = {
|
||||
.type = ORIGIN_TYPE_USER,
|
||||
.ctx = NULL,
|
||||
.cb = NULL,
|
||||
.args = NULL,
|
||||
.thr_idx = -1,
|
||||
};
|
||||
packet_set_origin(new_pkt, &origin);
|
||||
|
||||
return new_pkt;
|
||||
}
|
||||
@@ -1,18 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "log_internal.h"
|
||||
#include "packet_dump.h"
|
||||
#include "packet_parser.h"
|
||||
#include "packet_helper.h"
|
||||
#include "packet_internal.h"
|
||||
|
||||
#define PACKET_DUMP_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet dump", format, ##__VA_ARGS__)
|
||||
|
||||
struct pcap_pkt_hdr
|
||||
{
|
||||
unsigned int tv_sec; // time stamp
|
||||
@@ -32,8 +23,6 @@ struct pcap_file_hdr
|
||||
unsigned int linktype; // data link type (LINKTYPE_*)
|
||||
};
|
||||
|
||||
// return 0: success
|
||||
// return -1: failed
|
||||
int packet_dump_pcap(const struct packet *pkt, const char *file)
|
||||
{
|
||||
const char *data = packet_get_raw_data(pkt);
|
||||
@@ -49,16 +38,9 @@ int packet_dump_pcap(const struct packet *pkt, const char *file)
|
||||
.snaplen = 0xFFFF,
|
||||
.linktype = 1};
|
||||
|
||||
if (file == NULL || data == NULL || len == 0)
|
||||
{
|
||||
PACKET_DUMP_LOG_ERROR("invalid parameter, file: %p, data: %p, len: %d", file, data, len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(file, "w+");
|
||||
if (fp == NULL)
|
||||
{
|
||||
PACKET_DUMP_LOG_ERROR("fopen %s failed, %s", file, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,10 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct packet;
|
||||
#include "stellar/packet.h"
|
||||
|
||||
// return 0: success
|
||||
// return -1: failed
|
||||
int packet_dump_pcap(const struct packet *pkt, const char *file);
|
||||
void packet_dump_hex(const struct packet *pkt, int fd);
|
||||
// return number of bytes written
|
||||
int packet_dump_str(const struct packet *pkt, char *buff, int size);
|
||||
void packet_print(const struct packet *pkt);
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
#include "utils_internal.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -34,7 +34,25 @@ struct metadata
|
||||
enum packet_direction direction;
|
||||
enum packet_action action;
|
||||
struct timeval tv;
|
||||
const void *origin_ctx;
|
||||
};
|
||||
|
||||
enum origin_type
|
||||
{
|
||||
ORIGIN_TYPE_MR,
|
||||
ORIGIN_TYPE_PCAP,
|
||||
ORIGIN_TYPE_USER,
|
||||
};
|
||||
|
||||
typedef void origin_free(struct packet *pkt, void *args);
|
||||
struct packet_origin
|
||||
{
|
||||
enum origin_type type;
|
||||
void *ctx;
|
||||
|
||||
origin_free *cb;
|
||||
void *args;
|
||||
|
||||
int thr_idx;
|
||||
};
|
||||
|
||||
struct layer_private
|
||||
@@ -50,6 +68,8 @@ struct layer_private
|
||||
uint16_t hdr_offset; // header offset from data_ptr
|
||||
};
|
||||
|
||||
TAILQ_HEAD(packet_queue, packet);
|
||||
|
||||
struct packet
|
||||
{
|
||||
void *user_data;
|
||||
@@ -58,14 +78,19 @@ struct packet
|
||||
int8_t layers_used;
|
||||
int8_t layers_size;
|
||||
int8_t need_free;
|
||||
int8_t is_defraged;
|
||||
|
||||
const char *data_ptr;
|
||||
uint16_t data_len;
|
||||
uint16_t trim_len; // trim eth padding
|
||||
|
||||
TAILQ_ENTRY(packet) stage_tqe;
|
||||
TAILQ_ENTRY(packet) pool_tqe; // for packet pool
|
||||
TAILQ_ENTRY(packet) frag_tqe; // for IP reassembly
|
||||
TAILQ_ENTRY(packet) stage_tqe; // for packet manager
|
||||
struct packet_queue frag_list; // for defraged packet
|
||||
|
||||
struct metadata meta;
|
||||
struct packet_origin origin;
|
||||
};
|
||||
|
||||
enum packet_load_balance_method
|
||||
@@ -84,8 +109,8 @@ enum packet_load_balance_method
|
||||
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx);
|
||||
const struct route_ctx *packet_get_route_ctx(const struct packet *pkt);
|
||||
|
||||
void packet_set_origin_ctx(struct packet *pkt, void *ctx);
|
||||
const void *packet_get_origin_ctx(const struct packet *pkt);
|
||||
void packet_set_origin(struct packet *pkt, struct packet_origin *origin);
|
||||
struct packet_origin *packet_get_origin(struct packet *pkt);
|
||||
|
||||
void packet_set_sids(struct packet *pkt, const struct sids *sids);
|
||||
const struct sids *packet_get_sids(const struct packet *pkt);
|
||||
@@ -152,6 +177,11 @@ struct packet *packet_dup(const struct packet *pkt);
|
||||
void packet_free(struct packet *pkt);
|
||||
|
||||
int packet_is_fragment(const struct packet *pkt);
|
||||
int packet_is_defraged(const struct packet *pkt);
|
||||
void packet_set_defraged(struct packet *pkt);
|
||||
|
||||
void packet_push_frag(struct packet *pkt, struct packet *frag);
|
||||
struct packet *packet_pop_frag(struct packet *pkt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -8,17 +8,36 @@ extern "C"
|
||||
#include "stellar/mq.h"
|
||||
#include "stellar/packet_manager.h"
|
||||
|
||||
struct packet_manager *packet_manager_new(struct mq_schema *mq_schema, const char *toml_file);
|
||||
#define PACKET_QUEUE_MAX (PACKET_STAGE_MAX + 1)
|
||||
|
||||
struct packet_manager_stat
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint64_t pkts_ingress;
|
||||
uint64_t pkts_egress;
|
||||
} total;
|
||||
struct
|
||||
{
|
||||
uint64_t pkts_in; // include the packets that are scheduled
|
||||
uint64_t pkts_out; // include the packets that are claimed
|
||||
uint64_t pkts_claim;
|
||||
uint64_t pkts_schedule;
|
||||
} queue[PACKET_QUEUE_MAX]; // the last queue is for sending packets
|
||||
};
|
||||
|
||||
struct packet_manager *packet_manager_new(struct mq_schema *mq_schema, uint64_t thread_num);
|
||||
void packet_manager_free(struct packet_manager *pkt_mgr);
|
||||
|
||||
void packet_manager_init(struct packet_manager *pkt_mgr, uint16_t thread_id, struct mq_runtime *mq_rt);
|
||||
int packet_manager_init(struct packet_manager *pkt_mgr, uint16_t thread_id, struct mq_runtime *mq_rt);
|
||||
void packet_manager_ingress(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt);
|
||||
struct packet *packet_manager_egress(struct packet_manager *pkt_mgr, uint16_t thread_id);
|
||||
void packet_manager_dispatch(struct packet_manager *pkt_mgr, uint16_t thread_id);
|
||||
|
||||
struct packet_manager_stat *packet_manager_get_stat(struct packet_manager *pkt_mgr, uint16_t thread_id);
|
||||
void packet_manager_print_stat(struct packet_manager *pkt_mgr, uint16_t thread_id);
|
||||
|
||||
const char *packet_stage_to_str(enum packet_stage stage);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,173 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "packet_internal.h"
|
||||
#include "packet_manager_runtime.h"
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
for (int i = 0; i < PACKET_QUEUE_MAX; i++)
|
||||
{
|
||||
TAILQ_INIT(&pkt_mgr_rt->queue[i]);
|
||||
}
|
||||
|
||||
return pkt_mgr_rt;
|
||||
}
|
||||
|
||||
void packet_manager_runtime_free(struct packet_manager_runtime *pkt_mgr_rt)
|
||||
{
|
||||
if (pkt_mgr_rt)
|
||||
{
|
||||
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);
|
||||
|
||||
// TODO: free packet and free mbuff
|
||||
packet_free(pkt);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(pkt_mgr_rt);
|
||||
pkt_mgr_rt = NULL;
|
||||
}
|
||||
|
||||
void packet_manager_runtime_init(struct packet_manager_runtime *pkt_mgr_rt, struct mq_runtime *mq_rt)
|
||||
{
|
||||
pkt_mgr_rt->mq = mq_rt;
|
||||
}
|
||||
|
||||
void packet_manager_runtime_ingress(struct packet_manager_runtime *pkt_mgr_rt, struct packet *pkt)
|
||||
{
|
||||
pkt_mgr_rt->stat.total.pkts_ingress++;
|
||||
pkt_mgr_rt->stat.queue[PACKET_STAGE_PREROUTING].pkts_in++;
|
||||
TAILQ_INSERT_TAIL(&pkt_mgr_rt->queue[PACKET_STAGE_PREROUTING], pkt, stage_tqe);
|
||||
}
|
||||
|
||||
struct packet *packet_manager_runtime_egress(struct packet_manager_runtime *pkt_mgr_rt)
|
||||
{
|
||||
struct packet *pkt = TAILQ_FIRST(&pkt_mgr_rt->queue[PACKET_STAGE_MAX]);
|
||||
if (pkt)
|
||||
{
|
||||
pkt_mgr_rt->stat.total.pkts_egress++;
|
||||
pkt_mgr_rt->stat.queue[PACKET_STAGE_MAX].pkts_out++;
|
||||
TAILQ_REMOVE(&pkt_mgr_rt->queue[PACKET_STAGE_MAX], pkt, stage_tqe);
|
||||
}
|
||||
return pkt;
|
||||
}
|
||||
|
||||
void packet_manager_runtime_dispatch(struct packet_manager_runtime *pkt_mgr_rt)
|
||||
{
|
||||
for (int i = 0; i < PACKET_STAGE_MAX; i++)
|
||||
{
|
||||
pkt_mgr_rt->stage = i;
|
||||
|
||||
struct packet *pkt = NULL;
|
||||
while ((pkt = TAILQ_FIRST(&pkt_mgr_rt->queue[pkt_mgr_rt->stage])))
|
||||
{
|
||||
packet_set_claim(pkt, false);
|
||||
pkt_mgr_rt->claimed_cb = NULL;
|
||||
pkt_mgr_rt->cb_args = NULL;
|
||||
|
||||
TAILQ_REMOVE(&pkt_mgr_rt->queue[pkt_mgr_rt->stage], pkt, stage_tqe);
|
||||
pkt_mgr_rt->stat.queue[pkt_mgr_rt->stage].pkts_out++;
|
||||
|
||||
mq_runtime_publish_message(pkt_mgr_rt->mq, pkt_mgr_rt->stage, pkt);
|
||||
mq_runtime_dispatch(pkt_mgr_rt->mq);
|
||||
|
||||
if (packet_is_claim(pkt))
|
||||
{
|
||||
if (pkt_mgr_rt->claimed_cb)
|
||||
{
|
||||
pkt_mgr_rt->claimed_cb(pkt, pkt_mgr_rt->cb_args);
|
||||
}
|
||||
packet_set_claim(pkt, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
TAILQ_INSERT_TAIL(&pkt_mgr_rt->queue[pkt_mgr_rt->stage + 1], pkt, stage_tqe);
|
||||
pkt_mgr_rt->stat.queue[pkt_mgr_rt->stage + 1].pkts_in++;
|
||||
}
|
||||
}
|
||||
pkt_mgr_rt->stage = -1;
|
||||
}
|
||||
|
||||
int packet_manager_runtime_claim_packet(struct packet_manager_runtime *pkt_mgr_rt, struct packet *pkt, on_packet_claimed_callback cb, void *args)
|
||||
{
|
||||
if (packet_is_claim(pkt))
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("packet is already claimed, cannot claim again");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
pkt_mgr_rt->claimed_cb = cb;
|
||||
pkt_mgr_rt->cb_args = args;
|
||||
packet_set_claim(pkt, true);
|
||||
pkt_mgr_rt->stat.queue[pkt_mgr_rt->stage].pkts_claim++;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void packet_manager_runtime_schedule_packet(struct packet_manager_runtime *pkt_mgr_rt, struct packet *pkt, enum packet_stage stage)
|
||||
{
|
||||
if (stage >= PACKET_STAGE_MAX)
|
||||
{
|
||||
PACKET_MANAGER_LOG_ERROR("invalid stage %d", stage);
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
pkt_mgr_rt->stat.queue[stage].pkts_schedule++;
|
||||
pkt_mgr_rt->stat.queue[stage].pkts_in++;
|
||||
TAILQ_INSERT_TAIL(&pkt_mgr_rt->queue[stage], pkt, stage_tqe);
|
||||
}
|
||||
|
||||
struct packet_manager_stat *packet_manager_runtime_get_stat(struct packet_manager_runtime *pkt_mgr_rt)
|
||||
{
|
||||
return &pkt_mgr_rt->stat;
|
||||
}
|
||||
|
||||
void packet_manager_runtime_print_stat(struct packet_manager_runtime *pkt_mgr_rt)
|
||||
{
|
||||
PACKET_MANAGER_LOG_INFO("runtime: %p, pkts_ingress: %lu, pkts_egress: %lu",
|
||||
pkt_mgr_rt, pkt_mgr_rt->stat.total.pkts_ingress, pkt_mgr_rt->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",
|
||||
pkt_mgr_rt,
|
||||
packet_stage_to_str(i),
|
||||
pkt_mgr_rt->stat.queue[i].pkts_in,
|
||||
pkt_mgr_rt->stat.queue[i].pkts_out,
|
||||
pkt_mgr_rt->stat.queue[i].pkts_claim,
|
||||
pkt_mgr_rt->stat.queue[i].pkts_schedule);
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include "log_internal.h"
|
||||
#include "stellar/mq.h"
|
||||
#include "stellar/packet_manager.h"
|
||||
|
||||
#define PACKET_MANAGER_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet manager", format, ##__VA_ARGS__)
|
||||
#define PACKET_MANAGER_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__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__)
|
||||
|
||||
#define PACKET_QUEUE_MAX (PACKET_STAGE_MAX + 1)
|
||||
|
||||
TAILQ_HEAD(packet_queue, packet);
|
||||
|
||||
struct packet_manager_stat
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint64_t pkts_ingress;
|
||||
uint64_t pkts_egress;
|
||||
} total;
|
||||
struct
|
||||
{
|
||||
uint64_t pkts_in; // include the packets that are scheduled
|
||||
uint64_t pkts_out; // include the packets that are claimed
|
||||
uint64_t pkts_claim;
|
||||
uint64_t pkts_schedule;
|
||||
} queue[PACKET_QUEUE_MAX]; // the last queue is for sending packets
|
||||
};
|
||||
|
||||
struct packet_manager_runtime
|
||||
{
|
||||
enum packet_stage stage;
|
||||
struct packet_queue queue[PACKET_QUEUE_MAX];
|
||||
|
||||
void *cb_args;
|
||||
on_packet_claimed_callback *claimed_cb;
|
||||
|
||||
struct mq_runtime *mq;
|
||||
struct packet_manager_stat stat;
|
||||
};
|
||||
|
||||
struct packet_manager_runtime *packet_manager_runtime_new();
|
||||
void packet_manager_runtime_free(struct packet_manager_runtime *pkt_mgr_rt);
|
||||
|
||||
void packet_manager_runtime_init(struct packet_manager_runtime *pkt_mgr_rt, struct mq_runtime *mq_rt);
|
||||
void packet_manager_runtime_ingress(struct packet_manager_runtime *pkt_mgr_rt, struct packet *pkt);
|
||||
struct packet *packet_manager_runtime_egress(struct packet_manager_runtime *pkt_mgr_rt);
|
||||
void packet_manager_runtime_dispatch(struct packet_manager_runtime *pkt_mgr_rt);
|
||||
|
||||
int packet_manager_runtime_claim_packet(struct packet_manager_runtime *pkt_mgr_rt, struct packet *pkt, on_packet_claimed_callback cb, void *args);
|
||||
void packet_manager_runtime_schedule_packet(struct packet_manager_runtime *pkt_mgr_rt, struct packet *pkt, enum packet_stage stage);
|
||||
|
||||
struct packet_manager_stat *packet_manager_runtime_get_stat(struct packet_manager_runtime *pkt_mgr_rt);
|
||||
void packet_manager_runtime_print_stat(struct packet_manager_runtime *pkt_mgr_rt);
|
||||
|
||||
const char *packet_stage_to_str(enum packet_stage stage);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
115
infra/packet_manager/packet_pool.c
Normal file
115
infra/packet_manager/packet_pool.c
Normal file
@@ -0,0 +1,115 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "packet_pool.h"
|
||||
#include "packet_internal.h"
|
||||
|
||||
struct packet_pool
|
||||
{
|
||||
uint64_t capacity;
|
||||
uint64_t used;
|
||||
uint64_t free;
|
||||
struct packet_queue free_list;
|
||||
};
|
||||
|
||||
struct packet_pool *packet_pool_new(uint64_t capacity)
|
||||
{
|
||||
struct packet_pool *pool = (struct packet_pool *)calloc(1, sizeof(struct packet_pool));
|
||||
if (pool == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
pool->capacity = capacity;
|
||||
pool->used = 0;
|
||||
pool->free = 0;
|
||||
TAILQ_INIT(&pool->free_list);
|
||||
|
||||
for (uint64_t i = 0; i < capacity; i++)
|
||||
{
|
||||
struct packet *pkt = (struct packet *)calloc(1, sizeof(struct packet));
|
||||
if (pkt == NULL)
|
||||
{
|
||||
goto error_out;
|
||||
}
|
||||
TAILQ_INSERT_TAIL(&pool->free_list, pkt, pool_tqe);
|
||||
pool->free++;
|
||||
}
|
||||
|
||||
return pool;
|
||||
|
||||
error_out:
|
||||
packet_pool_free(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void packet_pool_free(struct packet_pool *pool)
|
||||
{
|
||||
struct packet *pkt;
|
||||
if (pool)
|
||||
{
|
||||
while ((pkt = TAILQ_FIRST(&pool->free_list)))
|
||||
{
|
||||
TAILQ_REMOVE(&pool->free_list, pkt, pool_tqe);
|
||||
free(pkt);
|
||||
pool->free--;
|
||||
}
|
||||
|
||||
assert(pool->used == 0);
|
||||
assert(pool->free == 0);
|
||||
|
||||
free(pool);
|
||||
pool = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct packet *packet_pool_pop(struct packet_pool *pool)
|
||||
{
|
||||
struct packet *pkt = TAILQ_FIRST(&pool->free_list);
|
||||
if (pkt == NULL)
|
||||
{
|
||||
pkt = (struct packet *)calloc(1, sizeof(struct packet));
|
||||
if (pkt == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TAILQ_REMOVE(&pool->free_list, pkt, pool_tqe);
|
||||
pool->free--;
|
||||
}
|
||||
|
||||
pool->used++;
|
||||
|
||||
return pkt;
|
||||
}
|
||||
|
||||
void packet_pool_push(struct packet_pool *pool, struct packet *pkt)
|
||||
{
|
||||
if (pool == NULL || pkt == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pool->used--;
|
||||
|
||||
if (pool->free < pool->capacity)
|
||||
{
|
||||
TAILQ_INSERT_TAIL(&pool->free_list, pkt, pool_tqe);
|
||||
pool->free++;
|
||||
}
|
||||
else
|
||||
{
|
||||
free(pkt);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t packet_pool_get_used_num(const struct packet_pool *pool)
|
||||
{
|
||||
return pool->used;
|
||||
}
|
||||
|
||||
uint64_t packet_pool_get_free_num(const struct packet_pool *pool)
|
||||
{
|
||||
return pool->free;
|
||||
}
|
||||
22
infra/packet_manager/packet_pool.h
Normal file
22
infra/packet_manager/packet_pool.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct packet_pool;
|
||||
struct packet_pool *packet_pool_new(uint64_t capacity);
|
||||
void packet_pool_free(struct packet_pool *pool);
|
||||
|
||||
struct packet *packet_pool_pop(struct packet_pool *pool);
|
||||
void packet_pool_push(struct packet_pool *pool, struct packet *pkt);
|
||||
|
||||
uint64_t packet_pool_get_used_num(const struct packet_pool *pool);
|
||||
uint64_t packet_pool_get_free_num(const struct packet_pool *pool);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <assert.h>
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include "tuple.h"
|
||||
#include "uthash.h"
|
||||
@@ -23,14 +24,14 @@ const struct route_ctx *packet_get_route_ctx(const struct packet *pkt)
|
||||
return &pkt->meta.route_ctx;
|
||||
}
|
||||
|
||||
void packet_set_origin_ctx(struct packet *pkt, void *ctx)
|
||||
void packet_set_origin(struct packet *pkt, struct packet_origin *origin)
|
||||
{
|
||||
pkt->meta.origin_ctx = ctx;
|
||||
pkt->origin = *origin;
|
||||
}
|
||||
|
||||
const void *packet_get_origin_ctx(const struct packet *pkt)
|
||||
struct packet_origin *packet_get_origin(struct packet *pkt)
|
||||
{
|
||||
return pkt->meta.origin_ctx;
|
||||
return &pkt->origin;
|
||||
}
|
||||
|
||||
void packet_set_sids(struct packet *pkt, const struct sids *sids)
|
||||
@@ -925,7 +926,14 @@ struct packet *packet_dup(const struct packet *pkt)
|
||||
dup_pkt->frag_layer = &dup_pkt->layers[pkt->frag_layer - pkt->layers];
|
||||
}
|
||||
memcpy(&dup_pkt->meta, &pkt->meta, sizeof(struct metadata));
|
||||
packet_set_origin_ctx(dup_pkt, (void *)NULL);
|
||||
struct packet_origin origin = {
|
||||
.type = ORIGIN_TYPE_USER,
|
||||
.ctx = NULL,
|
||||
.cb = NULL,
|
||||
.args = NULL,
|
||||
.thr_idx = -1,
|
||||
};
|
||||
packet_set_origin(dup_pkt, &origin);
|
||||
|
||||
return dup_pkt;
|
||||
}
|
||||
@@ -941,6 +949,11 @@ void packet_free(struct packet *pkt)
|
||||
return;
|
||||
}
|
||||
|
||||
if (pkt->origin.cb)
|
||||
{
|
||||
pkt->origin.cb(pkt, pkt->origin.args);
|
||||
}
|
||||
|
||||
if (pkt->need_free)
|
||||
{
|
||||
free((void *)pkt);
|
||||
@@ -953,14 +966,54 @@ int packet_is_fragment(const struct packet *pkt)
|
||||
return (pkt->frag_layer) ? 1 : 0;
|
||||
}
|
||||
|
||||
int packet_is_defraged(const struct packet *pkt)
|
||||
{
|
||||
return pkt->is_defraged;
|
||||
}
|
||||
|
||||
void packet_set_defraged(struct packet *pkt)
|
||||
{
|
||||
pkt->is_defraged = 1;
|
||||
TAILQ_INIT(&pkt->frag_list);
|
||||
}
|
||||
|
||||
void packet_push_frag(struct packet *pkt, struct packet *frag)
|
||||
{
|
||||
if (!packet_is_defraged(pkt))
|
||||
{
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
TAILQ_INSERT_TAIL(&pkt->frag_list, frag, frag_tqe);
|
||||
}
|
||||
|
||||
struct packet *packet_pop_frag(struct packet *pkt)
|
||||
{
|
||||
if (!packet_is_defraged(pkt))
|
||||
{
|
||||
assert(0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct packet *frag = TAILQ_FIRST(&pkt->frag_list);
|
||||
if (frag)
|
||||
{
|
||||
TAILQ_REMOVE(&pkt->frag_list, frag, frag_tqe);
|
||||
}
|
||||
return frag;
|
||||
}
|
||||
|
||||
void packet_set_exdata(struct packet *pkt, int idx, void *ex_ptr)
|
||||
{
|
||||
struct exdata_runtime *exdata_rt = (struct exdata_runtime *)packet_get_user_data(pkt);
|
||||
assert(exdata_rt);
|
||||
exdata_set(exdata_rt, idx, ex_ptr);
|
||||
}
|
||||
|
||||
void *packet_get_exdata(struct packet *pkt, int idx)
|
||||
{
|
||||
struct exdata_runtime *exdata_rt = (struct exdata_runtime *)packet_get_user_data(pkt);
|
||||
assert(exdata_rt);
|
||||
return exdata_get(exdata_rt, idx);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,9 @@ target_link_libraries(gtest_packet_filter packet_manager gtest)
|
||||
add_executable(gtest_packet_ldbc gtest_packet_ldbc.cpp)
|
||||
target_link_libraries(gtest_packet_ldbc packet_manager gtest)
|
||||
|
||||
add_executable(gtest_packet_pool gtest_packet_pool.cpp)
|
||||
target_link_libraries(gtest_packet_pool packet_manager gtest)
|
||||
|
||||
add_executable(gtest_packet_manager gtest_packet_manager.cpp)
|
||||
target_link_libraries(gtest_packet_manager packet_manager gtest)
|
||||
|
||||
@@ -78,6 +81,7 @@ gtest_discover_tests(gtest_packet_parser)
|
||||
gtest_discover_tests(gtest_packet_builder)
|
||||
gtest_discover_tests(gtest_packet_filter)
|
||||
gtest_discover_tests(gtest_packet_ldbc)
|
||||
gtest_discover_tests(gtest_packet_pool)
|
||||
gtest_discover_tests(gtest_packet_manager)
|
||||
|
||||
file(COPY ../../../conf/ DESTINATION ./conf/)
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "packet_parser.h"
|
||||
#include "packet_internal.h"
|
||||
#include "packet_manager_runtime.h"
|
||||
#include "packet_manager_internal.h"
|
||||
|
||||
/******************************************************************************
|
||||
@@ -90,7 +89,7 @@ TEST(PACKET_MANAGER, NEW_FREE)
|
||||
struct mq_schema *mq_schema = mq_schema_new();
|
||||
EXPECT_TRUE(mq_schema);
|
||||
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, "./conf/stellar.toml");
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, 1);
|
||||
EXPECT_TRUE(pkt_mgr);
|
||||
|
||||
packet_manager_free(pkt_mgr);
|
||||
@@ -120,7 +119,7 @@ TEST(PACKET_MANAGER, SUBSCRIBER_PACKET_STAGE)
|
||||
EXPECT_TRUE(mq_rt);
|
||||
|
||||
// module init
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, "./conf/stellar.toml");
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, 1);
|
||||
EXPECT_TRUE(pkt_mgr);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_PREROUTING, on_packet_stage, NULL) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_INPUT, on_packet_stage, NULL) == 0);
|
||||
@@ -178,11 +177,11 @@ static void packet_claimed(struct packet *pkt, void *args)
|
||||
free(str);
|
||||
}
|
||||
|
||||
static void on_packet_stage_claim_packet_success(enum packet_stage stage, struct packet *pkt, void *args)
|
||||
static void claim_packet_success(enum packet_stage stage, struct packet *pkt, void *args)
|
||||
{
|
||||
struct packet_manager *pkt_mgr = (struct packet_manager *)args;
|
||||
|
||||
printf("on_packet_stage_claim_packet_success: %s\n", packet_stage_to_str(stage));
|
||||
printf("claim_packet_success: %s\n", packet_stage_to_str(stage));
|
||||
|
||||
static int count = 0;
|
||||
EXPECT_TRUE(count == 0);
|
||||
@@ -193,11 +192,11 @@ static void on_packet_stage_claim_packet_success(enum packet_stage stage, struct
|
||||
count++;
|
||||
}
|
||||
|
||||
static void on_packet_stage_claim_packet_failed(enum packet_stage stage, struct packet *pkt, void *args)
|
||||
static void claim_packet_failed(enum packet_stage stage, struct packet *pkt, void *args)
|
||||
{
|
||||
struct packet_manager *pkt_mgr = (struct packet_manager *)args;
|
||||
|
||||
printf("on_packet_stage_claim_packet_failed: %s\n", packet_stage_to_str(stage));
|
||||
printf("claim_packet_failed: %s\n", packet_stage_to_str(stage));
|
||||
|
||||
static int count = 0;
|
||||
EXPECT_TRUE(count == 0);
|
||||
@@ -217,19 +216,19 @@ TEST(PACKET_MANAGER, CLAIM_PACKET)
|
||||
EXPECT_TRUE(mq_rt);
|
||||
|
||||
// module init
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, "./conf/stellar.toml");
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, 1);
|
||||
EXPECT_TRUE(pkt_mgr);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_PREROUTING, on_packet_stage_claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_INPUT, on_packet_stage_claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_FORWARD, on_packet_stage_claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_OUTPUT, on_packet_stage_claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_POSTROUTING, on_packet_stage_claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_PREROUTING, claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_INPUT, claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_FORWARD, claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_OUTPUT, claim_packet_success, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_POSTROUTING, claim_packet_success, pkt_mgr) == 0);
|
||||
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_PREROUTING, on_packet_stage_claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_INPUT, on_packet_stage_claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_FORWARD, on_packet_stage_claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_OUTPUT, on_packet_stage_claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_POSTROUTING, on_packet_stage_claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_PREROUTING, claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_INPUT, claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_FORWARD, claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_OUTPUT, claim_packet_failed, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_POSTROUTING, claim_packet_failed, pkt_mgr) == 0);
|
||||
|
||||
// per-thread init
|
||||
packet_manager_init(pkt_mgr, thread_id, mq_rt);
|
||||
@@ -296,7 +295,7 @@ TEST(PACKET_MANAGER, SCHEDULE_PACKET)
|
||||
EXPECT_TRUE(mq_rt);
|
||||
|
||||
// module init
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, "./conf/stellar.toml");
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, 1);
|
||||
EXPECT_TRUE(pkt_mgr);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_PREROUTING, on_packet_stage_schedule_packet, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_INPUT, on_packet_stage_schedule_packet, pkt_mgr) == 0);
|
||||
@@ -398,7 +397,7 @@ TEST(PACKET_MANAGER, SCHEDULE_CLAIMED_PACKET)
|
||||
EXPECT_TRUE(mq_rt);
|
||||
|
||||
// module init
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, "./conf/stellar.toml");
|
||||
struct packet_manager *pkt_mgr = packet_manager_new(mq_schema, 1);
|
||||
EXPECT_TRUE(pkt_mgr);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_PREROUTING, on_packet_stage_claim_packet_to_schedule, pkt_mgr) == 0);
|
||||
EXPECT_TRUE(packet_manager_subscribe(pkt_mgr, PACKET_STAGE_INPUT, on_packet_stage_claim_packet_to_schedule, pkt_mgr) == 0);
|
||||
|
||||
64
infra/packet_manager/test/gtest_packet_pool.cpp
Normal file
64
infra/packet_manager/test/gtest_packet_pool.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "packet_pool.h"
|
||||
|
||||
TEST(PACKET_POOL, TEST)
|
||||
{
|
||||
struct packet *pkt1 = NULL;
|
||||
struct packet *pkt2 = NULL;
|
||||
struct packet *pkt3 = NULL;
|
||||
struct packet *pkt4 = NULL;
|
||||
|
||||
// new
|
||||
struct packet_pool *pool = packet_pool_new(3);
|
||||
EXPECT_TRUE(pool != NULL);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 0);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 3);
|
||||
|
||||
// pop
|
||||
pkt1 = packet_pool_pop(pool);
|
||||
EXPECT_TRUE(pkt1 != NULL);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 1);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 2);
|
||||
|
||||
pkt2 = packet_pool_pop(pool);
|
||||
EXPECT_TRUE(pkt2 != NULL);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 2);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 1);
|
||||
|
||||
pkt3 = packet_pool_pop(pool);
|
||||
EXPECT_TRUE(pkt3 != NULL);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 3);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 0);
|
||||
|
||||
pkt4 = packet_pool_pop(pool);
|
||||
EXPECT_TRUE(pkt4 != NULL);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 4);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 0);
|
||||
|
||||
// push
|
||||
packet_pool_push(pool, pkt1);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 3);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 1);
|
||||
|
||||
packet_pool_push(pool, pkt2);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 2);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 2);
|
||||
|
||||
packet_pool_push(pool, pkt3);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 1);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 3);
|
||||
|
||||
packet_pool_push(pool, pkt4);
|
||||
EXPECT_TRUE(packet_pool_get_used_num(pool) == 0);
|
||||
EXPECT_TRUE(packet_pool_get_free_num(pool) == 3);
|
||||
|
||||
// free
|
||||
packet_pool_free(pool);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user