[PACKET_IO] finish pcap live mode

This commit is contained in:
liuwentan
2022-08-05 18:32:54 +08:00
parent 9cf895fa07
commit 7442cb09d5
10 changed files with 317 additions and 67 deletions

View File

@@ -17,17 +17,11 @@
#include "pio_pcap_file.h"
#include "../packet_io.h"
#include "../../sdk/include/utils.h"
#include "../../sdk/include/util_errors.h"
#include "../../sdk/include/logger.h"
#include "../../../sdk/include/utils.h"
#include "../../../sdk/include/util_errors.h"
#include "../../../sdk/include/logger.h"
#include "../../common/time_helper.h"
#define SET_PKT_LEN(p, len) do { \
(p)->pkt_len = (len); \
} while(0)
#define GET_PKT_DIRECT_DATA(p) (uint8_t *)((p) + 1)
/*
* @brief validate path is a valid plain file or directory
*
@@ -168,9 +162,7 @@ static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_d
/* TODO: get conf and assign pdir_info */
strncpy(pdir_info->dir_name, dir_name, strlen(dir_name));
//pdir_info->should_loop = false;
//pdir_info->delay = 30;
//pdir_info->poll_interval = 5;
pdir_info->shared = &pfile_dev_ctx->shared;
pdir_info->directory = directory;
TAILQ_INIT(&pdir_info->file_queue_head);
@@ -304,18 +296,7 @@ int pio_pcap_file_device_close(struct packet_io_device *pdev)
return 0;
}
int packet_copy_data_offset(struct packet *p, uint32_t offset, const uint8_t *data, uint32_t data_len) {
memcpy(GET_PKT_DIRECT_DATA(p) + offset, data, data_len);
return 0;
}
int packet_copy_data(struct packet *p, const uint8_t *pkt_data, uint32_t pkt_len) {
SET_PKT_LEN(p, (size_t)pkt_len);
return packet_copy_data_offset(p, 0, pkt_data, pkt_len);
}
static void pcap_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_char *pkt, uint32_t nr_rxq, uint32_t rxq_id)
void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_char *pkt)
{
struct pio_pcap_file_device_context *pfile_dev_ctx = (struct pio_pcap_file_device_context *)user;
struct packet *p = CALLOC(struct packet, 1);
@@ -333,9 +314,10 @@ static void pcap_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u
hash_id = decode_packet(p) % nr_rxq;
packet_enqueue(&pfile_dev_ctx->pkt_queues[hash_id], p);
*/
/*
pthread_mutex_lock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
packet_enqueue(&pfile_dev_ctx->pkt_queues[rxq_id], p);
pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
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, uint32_t nr_rxq, uint32_t rxq_id,
@@ -344,8 +326,8 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx
if (pfile_dev_ctx->entity.file->first_pkt_hdr != nullptr) {
pthread_mutex_lock(&pfile_dev_ctx->entity.file->handle_mutex);
if (pfile_dev_ctx->entity.file->first_pkt_hdr != nullptr) {
pcap_pkt_callback_oneshot((char *)pfile_dev_ctx, pfile_dev_ctx->entity.file->first_pkt_hdr,
(u_char *)pfile_dev_ctx->entity.file->first_pkt_data, nr_rxq, rxq_id);
pcap_file_pkt_callback_oneshot((char *)pfile_dev_ctx, pfile_dev_ctx->entity.file->first_pkt_hdr,
(u_char *)pfile_dev_ctx->entity.file->first_pkt_data);
pfile_dev_ctx->entity.file->first_pkt_hdr = nullptr;
pfile_dev_ctx->entity.file->first_pkt_data = nullptr;
}
@@ -357,7 +339,7 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx
pthread_mutex_lock(&pfile_dev_ctx->entity.file->handle_mutex);
res = pcap_dispatch(pfile_dev_ctx->entity.file->pcap_handle, packet_q_len,
(pcap_handler)pcap_pkt_callback_oneshot, (u_char *)pfile_dev_ctx);
(pcap_handler)pcap_file_pkt_callback_oneshot, (u_char *)pfile_dev_ctx);
pthread_mutex_unlock(&pfile_dev_ctx->entity.file->handle_mutex);
if (res < 0) {
log_error(ST_ERR_PCAP_DISPATCH, "error code %d %s for %s", res,
@@ -644,6 +626,10 @@ int pio_pcap_file_instance_create(struct packet_io_instance *pinst, __unused int
void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst)
{
if (nullptr == pinst) {
return;
}
FREE(pinst->entity.pcap_file_inst_ctx);
for (uint32_t i = 0; i < pinst->dev_cnt; i++) {