[PACKET_IO]format api style
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include "pio_pcap_file.h"
|
||||
#include "packet_io.h"
|
||||
#include "packet_io_util.h"
|
||||
#include "packet_io_internal.h"
|
||||
|
||||
/**
|
||||
* @brief validate path is a valid plain file or directory
|
||||
@@ -30,10 +31,10 @@
|
||||
* if success, dir == nullptr <---> means path is plain file
|
||||
* dir != nullptr <---> means path is directory
|
||||
*/
|
||||
static int validate_directory_or_file(const char *path, DIR **dir)
|
||||
static ssize_t validate_directory_or_file(const char *path, DIR **dir)
|
||||
{
|
||||
DIR *temp_dir = nullptr;
|
||||
int ret = -1;
|
||||
ssize_t ret = -1;
|
||||
|
||||
temp_dir = opendir(path);
|
||||
if (nullptr == temp_dir) {
|
||||
@@ -79,7 +80,7 @@ static bool peek_first_packet_timestamp(struct pcap_plain_file_info *pfile_info)
|
||||
return true;
|
||||
}
|
||||
|
||||
static int init_pcap_file(struct pcap_plain_file_info *pfile_info)
|
||||
static ssize_t init_pcap_file(struct pcap_plain_file_info *pfile_info)
|
||||
{
|
||||
char errbuf[PCAP_ERRBUF_SIZE] = "";
|
||||
|
||||
@@ -121,7 +122,7 @@ static int init_pcap_file(struct pcap_plain_file_info *pfile_info)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pcap_plain_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *file_name)
|
||||
static ssize_t pcap_plain_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *file_name)
|
||||
{
|
||||
if (nullptr == pfile_dev_ctx) {
|
||||
return -1;
|
||||
@@ -154,7 +155,7 @@ static int pcap_plain_file_init(struct pio_pcap_file_device_context *pfile_dev_c
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *dir_name, DIR *directory)
|
||||
static ssize_t pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *dir_name, DIR *directory)
|
||||
{
|
||||
if (nullptr == pfile_dev_ctx) {
|
||||
return -1;
|
||||
@@ -173,7 +174,8 @@ static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_d
|
||||
return -1;
|
||||
}
|
||||
|
||||
//pdir_info->delay = 30;
|
||||
/* TODO: if should configurable */
|
||||
pdir_info->delay = 30;
|
||||
pdir_info->shared = &pfile_dev_ctx->shared;
|
||||
pdir_info->directory = directory;
|
||||
TAILQ_INIT(&pdir_info->file_queue_head);
|
||||
@@ -184,17 +186,17 @@ static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_d
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ctx)
|
||||
static ssize_t pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ctx)
|
||||
{
|
||||
if (nullptr == pfile_dev_ctx) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* TODO: get conf and assign pfile_dev_ctx->shared */
|
||||
if ((g_engine_instance.config.packet_io.mode == PACKET_IO_RUN_MODE_PCAP_FILE) &&
|
||||
g_engine_instance.config.packet_io.bpf_string != nullptr) {
|
||||
if ((g_packet_io_config.common.mode == PACKET_IO_RUN_MODE_PCAP_FILE) &&
|
||||
g_packet_io_config.pcap.bpf_string != nullptr) {
|
||||
memset(pfile_dev_ctx->shared.bpf_string, 0, sizeof(pfile_dev_ctx->shared.bpf_string));
|
||||
int ret = strncpy_safe(pfile_dev_ctx->shared.bpf_string, g_engine_instance.config.packet_io.bpf_string,
|
||||
ssize_t ret = strncpy_safe(pfile_dev_ctx->shared.bpf_string, g_packet_io_config.pcap.bpf_string,
|
||||
sizeof(pfile_dev_ctx->shared.bpf_string));
|
||||
if (ret < 0) {
|
||||
log_error(ST_ERR_STR_COPY, "pcap file bpf string copy failed.");
|
||||
@@ -202,7 +204,7 @@ static int pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_
|
||||
}
|
||||
}
|
||||
|
||||
pfile_dev_ctx->shared.should_delete = g_engine_instance.config.packet_io.should_delete;
|
||||
pfile_dev_ctx->shared.should_delete = g_packet_io_config.pcap.should_delete;
|
||||
|
||||
/* init pcap file device packet queue */
|
||||
|
||||
@@ -239,9 +241,9 @@ static void cleanup_pcap_directory_info(struct pcap_file_directory_info *pdir_in
|
||||
|
||||
}
|
||||
|
||||
int pio_pcap_file_device_open(struct packet_io_device *pdev)
|
||||
ssize_t pio_pcap_file_device_open(struct packet_io_device *pdev)
|
||||
{
|
||||
int status = -1;
|
||||
ssize_t status = -1;
|
||||
DIR *directory = nullptr;
|
||||
|
||||
if (nullptr == pdev) {
|
||||
@@ -255,7 +257,7 @@ int pio_pcap_file_device_open(struct packet_io_device *pdev)
|
||||
return -1;
|
||||
}
|
||||
|
||||
pdev->entity.pcap_file_dev_ctx->pio_dev = pdev;
|
||||
pdev->entity.pcap_file_dev_ctx->pdev = pdev;
|
||||
|
||||
status = pcap_file_shared_init(pdev->entity.pcap_file_dev_ctx);
|
||||
if (status < 0) {
|
||||
@@ -287,24 +289,26 @@ int pio_pcap_file_device_open(struct packet_io_device *pdev)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pio_pcap_file_device_close(struct packet_io_device *pdev)
|
||||
ssize_t pio_pcap_file_device_close(struct packet_io_device *pdev)
|
||||
{
|
||||
if (nullptr == pdev) {
|
||||
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "invalid pdev pointer so close pcap file device failed!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pdev->entity.pcap_file_dev_ctx->entity.file != nullptr) {
|
||||
cleanup_pcap_plain_file_info(pdev->entity.pcap_file_dev_ctx->entity.file);
|
||||
}
|
||||
if (pdev->entity.pcap_file_dev_ctx != nullptr) {
|
||||
if (pdev->entity.pcap_file_dev_ctx->entity.file != nullptr) {
|
||||
cleanup_pcap_plain_file_info(pdev->entity.pcap_file_dev_ctx->entity.file);
|
||||
}
|
||||
|
||||
if (pdev->entity.pcap_file_dev_ctx->entity.dir != nullptr) {
|
||||
cleanup_pcap_directory_info(pdev->entity.pcap_file_dev_ctx->entity.dir);
|
||||
}
|
||||
if (pdev->entity.pcap_file_dev_ctx->entity.dir != nullptr) {
|
||||
cleanup_pcap_directory_info(pdev->entity.pcap_file_dev_ctx->entity.dir);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) {
|
||||
if (pdev->entity.pcap_file_dev_ctx->pkt_queues[i].len != 0) {
|
||||
release_pio_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]);
|
||||
for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) {
|
||||
if (pdev->entity.pcap_file_dev_ctx->pkt_queues[i].len != 0) {
|
||||
release_pio_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,7 +345,7 @@ void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_c
|
||||
}
|
||||
|
||||
/* nr_rxq <= PKT_QUEUE_MAX_NUM */
|
||||
uint16_t nr_rxq = pfile_dev_ctx->pio_dev->rxq_num;
|
||||
uint16_t nr_rxq = pfile_dev_ctx->pdev->rxq_num;
|
||||
uint16_t rxq_id = pio_packet_hash(p) % nr_rxq;
|
||||
|
||||
/* hash to specific queue id and enqueue */
|
||||
@@ -350,8 +354,8 @@ void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_c
|
||||
pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
|
||||
}
|
||||
|
||||
static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint16_t rxq_id,
|
||||
struct stellar_packet **pkts, int nr_pkts)
|
||||
static ssize_t pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id,
|
||||
struct stellar_packet **pkts, size_t nr_pkts)
|
||||
{
|
||||
if (pfile_dev_ctx->entity.file->first_pkt_hdr != nullptr) {
|
||||
pthread_mutex_lock(&pfile_dev_ctx->entity.file->handle_mutex);
|
||||
@@ -365,7 +369,7 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx
|
||||
}
|
||||
|
||||
int packet_q_len = nr_pkts;
|
||||
int res = -1;
|
||||
ssize_t res = -1;
|
||||
|
||||
pthread_mutex_lock(&pfile_dev_ctx->entity.file->handle_mutex);
|
||||
res = pcap_dispatch(pfile_dev_ctx->entity.file->pcap_handle, packet_q_len,
|
||||
@@ -380,17 +384,19 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx
|
||||
} else {
|
||||
// success
|
||||
struct pio_packet *p = nullptr;
|
||||
int i = 0;
|
||||
size_t i = 0;
|
||||
uint32_t q_len = 0;
|
||||
pthread_mutex_lock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
|
||||
do {
|
||||
p = pio_packet_dequeue(&pfile_dev_ctx->pkt_queues[rxq_id]);
|
||||
q_len = pfile_dev_ctx->pkt_queues[rxq_id].len;
|
||||
pkts[i] = (struct stellar_packet *)p;
|
||||
i++;
|
||||
} while (p != nullptr && (i < nr_pkts));
|
||||
} while ((q_len != 0) && (i < nr_pkts));
|
||||
pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
|
||||
|
||||
if (nullptr == p) {
|
||||
res = i - 1;
|
||||
if (q_len == 0) {
|
||||
res = i;
|
||||
} else {
|
||||
res = nr_pkts;
|
||||
}
|
||||
@@ -399,10 +405,10 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx
|
||||
return res;
|
||||
}
|
||||
|
||||
static int pcap_directory_get_modified_time(char *pfile, struct timespec *out)
|
||||
static ssize_t pcap_directory_get_modified_time(char *pfile, struct timespec *out)
|
||||
{
|
||||
struct stat buf;
|
||||
int ret = -1;
|
||||
ssize_t ret = -1;
|
||||
|
||||
if (nullptr == pfile) {
|
||||
return ret;
|
||||
@@ -443,7 +449,7 @@ find_pending_file_to_add(struct pio_pcap_file_device_context *pfile_dev_ctx, str
|
||||
}
|
||||
|
||||
struct pending_file *file_to_add = CALLOC(struct pending_file, 1);
|
||||
int ret = strncpy_safe(file_to_add->file_name, abs_path, sizeof(file_to_add->file_name));
|
||||
ssize_t ret = strncpy_safe(file_to_add->file_name, abs_path, sizeof(file_to_add->file_name));
|
||||
if (ret < 0) {
|
||||
log_error(ST_ERR_STR_COPY, "file_to_add file name copy failed.");
|
||||
return nullptr;
|
||||
@@ -456,7 +462,7 @@ find_pending_file_to_add(struct pio_pcap_file_device_context *pfile_dev_ctx, str
|
||||
return file_to_add;
|
||||
}
|
||||
|
||||
static int pcap_directory_insert_file(struct pio_pcap_file_device_context *pfile_dev_ctx, struct pending_file *file_to_add)
|
||||
static ssize_t pcap_directory_insert_file(struct pio_pcap_file_device_context *pfile_dev_ctx, struct pending_file *file_to_add)
|
||||
{
|
||||
if (nullptr == pfile_dev_ctx || file_to_add) {
|
||||
log_error(ST_ERR_PCAP_FILE_COLLECT_FAILED, "invalid directory or file parameters.");
|
||||
@@ -486,7 +492,7 @@ static int pcap_directory_insert_file(struct pio_pcap_file_device_context *pfile
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pcap_directory_collect_pending_files(struct pio_pcap_file_device_context *pfile_dev_ctx, struct timespec *deadline)
|
||||
static ssize_t pcap_directory_collect_pending_files(struct pio_pcap_file_device_context *pfile_dev_ctx, struct timespec *deadline)
|
||||
{
|
||||
if (nullptr == pfile_dev_ctx) {
|
||||
return -1;
|
||||
@@ -526,10 +532,10 @@ static int pcap_directory_collect_pending_files(struct pio_pcap_file_device_cont
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint16_t rxq_id,
|
||||
struct stellar_packet **pkts, int nr_pkts)
|
||||
static ssize_t pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id,
|
||||
struct stellar_packet **pkts, size_t nr_pkts)
|
||||
{
|
||||
int res = -1;
|
||||
ssize_t res = -1;
|
||||
struct timespec deadline;
|
||||
|
||||
memset(&deadline, 0, sizeof(struct timespec));
|
||||
@@ -611,7 +617,7 @@ static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_de
|
||||
return res;
|
||||
}
|
||||
|
||||
int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
|
||||
ssize_t pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts)
|
||||
{
|
||||
struct pio_pcap_file_device_context *pfile_dev_ctx = pdev->entity.pcap_file_dev_ctx;
|
||||
if (nullptr == pfile_dev_ctx) {
|
||||
@@ -619,7 +625,7 @@ int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id,
|
||||
return -1;
|
||||
}
|
||||
|
||||
int res = -1;
|
||||
ssize_t res = -1;
|
||||
if (pfile_dev_ctx->is_dir == 0) {
|
||||
log_info("Start reading file:%s", pfile_dev_ctx->entity.file->file_name);
|
||||
res = pcap_file_dispatch(pfile_dev_ctx, rxq_id, pkts, nr_pkts);
|
||||
@@ -631,15 +637,15 @@ int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id,
|
||||
return res;
|
||||
}
|
||||
|
||||
void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint16_t qid, struct stellar_packet **pkts, int nr_pkts)
|
||||
void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts)
|
||||
{
|
||||
for (int i = 0; i < nr_pkts; i++) {
|
||||
for (size_t i = 0; i < nr_pkts; i++) {
|
||||
struct pio_packet *p = (struct pio_packet *)pkts[i];
|
||||
FREE(p);
|
||||
}
|
||||
}
|
||||
|
||||
int pio_pcap_file_instance_create(struct packet_io_instance *pinst)
|
||||
ssize_t pio_pcap_file_instance_create(struct packet_io_instance *pinst)
|
||||
{
|
||||
if (nullptr == pinst) {
|
||||
log_error(ST_ERR_PIO_PCAP_FILE_INSTANCE, "invalid pcap file instance pointer.");
|
||||
@@ -661,34 +667,29 @@ void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst)
|
||||
return;
|
||||
}
|
||||
|
||||
FREE(pinst->entity.pcap_file_inst_ctx);
|
||||
|
||||
for (uint32_t i = 0; i < pinst->dev_cnt; i++) {
|
||||
pio_pcap_file_device_close(pinst->devices[i]);
|
||||
FREE(pinst->devices[i]);
|
||||
if (pinst->entity.pcap_file_inst_ctx != nullptr) {
|
||||
FREE(pinst->entity.pcap_file_inst_ctx);
|
||||
}
|
||||
|
||||
struct packet_io_device *node = nullptr;
|
||||
while ((node = TAILQ_FIRST(&pinst->device_queue_head)) != nullptr) {
|
||||
TAILQ_REMOVE(&pinst->device_queue_head, node, next);
|
||||
pinst->dev_cnt--;
|
||||
pio_pcap_file_device_close(node);
|
||||
FREE(node);
|
||||
}
|
||||
}
|
||||
|
||||
void *pio_pcap_file_device_buff_ctrlzone(struct stellar_packet *p)
|
||||
char *pio_pcap_file_device_buff_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len)
|
||||
{
|
||||
struct pio_packet *pkt = (struct pio_packet *)p;
|
||||
return pkt->pkt_hdr;
|
||||
*ctrlzone_len = CUSTOM_ZONE_LEN;
|
||||
return (char *)pkt->pkt_hdr;
|
||||
}
|
||||
|
||||
char *pio_pcap_file_device_buff_mtod(struct stellar_packet *p)
|
||||
char *pio_pcap_file_device_buff_mtod(struct stellar_packet *p, size_t *data_len)
|
||||
{
|
||||
struct pio_packet *pkt = (struct pio_packet *)p;
|
||||
*data_len = pkt->pkt_len;
|
||||
return (char *)pkt->pkt_payload;
|
||||
}
|
||||
|
||||
uint32_t pio_pcap_file_device_buff_buflen(struct stellar_packet *p)
|
||||
{
|
||||
struct pio_packet *pkt = (struct pio_packet *)p;
|
||||
return (pkt->pkt_len + CUSTOM_ZONE_LEN);
|
||||
}
|
||||
|
||||
uint32_t pio_pcap_file_device_buff_datalen(struct stellar_packet *p)
|
||||
{
|
||||
struct pio_packet *pkt = (struct pio_packet *)p;
|
||||
return (pkt->pkt_len);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user