[PACKET_IO] add pio_packet structure for pcap_live/pcap_file mode

This commit is contained in:
liuwentan
2022-08-09 15:58:46 +08:00
parent 7442cb09d5
commit c2d5b9cdb7
17 changed files with 157 additions and 360 deletions

View File

@@ -1,7 +1,7 @@
add_library(packet_io
../common/global_var.cpp
../common/packet_queue.cpp
../common/pio_packet_queue.cpp
../common/time_helper.cpp
packet_io.cpp
pcap_live_mode/pio_pcap_live.cpp

View File

@@ -12,7 +12,7 @@
#include <stdint.h>
#include "marsio.h"
#include <marsio.h>
/*
* dll is short for dynamic link lib

View File

@@ -63,6 +63,18 @@ struct pio_instance_operations pio_instance_ops_array[PACKET_IO_RUN_MODE_MAX] =
struct packet_io_instance *
packet_io_instance_create(const char *inst_name, const enum packet_io_run_mode mode, const int wrk_thread_num)
{
if (nullptr == inst_name) {
return nullptr;
}
if (mode < PACKET_IO_RUN_MODE_PCAP_FILE || mode >= PACKET_IO_RUN_MODE_MAX) {
return nullptr;
}
if (wrk_thread_num < 0) {
return nullptr;
}
struct packet_io_instance *pio_instance = CALLOC(struct packet_io_instance, 1);
if (nullptr == pio_instance) {
log_error(ST_ERR_MEM_ALLOC, "packet_io instance alloc failed.");
@@ -156,16 +168,14 @@ void packet_io_pkts_free(struct packet_io_instance *pinst, uint32_t qid, struct
}
static int packet_copy_data_offset(struct packet *p, uint32_t offset, const uint8_t *data, uint32_t data_len)
static int packet_copy_data_offset(uint8_t *ptr, uint32_t offset, const uint8_t *data, uint32_t data_len)
{
memcpy(GET_PKT_DIRECT_DATA(p) + offset, data, data_len);
memcpy(ptr + 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);
int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len)
{
return packet_copy_data_offset(ptr, 0, pkt_data, pkt_len);
}

View File

@@ -144,4 +144,4 @@ int packet_io_device_tx(struct packet_io_device *pdev, uint32_t txq_id, struct p
**/
void packet_io_pkts_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts);
int packet_copy_data(struct packet *p, const uint8_t *pkt_data, uint32_t pkt_len);
int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len);

View File

@@ -287,7 +287,7 @@ int pio_pcap_file_device_close(struct packet_io_device *pdev)
for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) {
if (pdev->entity.pcap_file_dev_ctx->pkt_queues[i].len != 0) {
release_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]);
release_pio_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]);
}
}
@@ -299,12 +299,16 @@ int pio_pcap_file_device_close(struct packet_io_device *pdev)
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);
struct pio_packet *p = (struct pio_packet *)malloc(SIZE_OF_PIO_PACKET);
if (nullptr == p) {
return;
}
memset(p, 0, SIZE_OF_PIO_PACKET);
if (packet_copy_data(p, pkt, pkt_hdr->caplen)) {
p->pkt_hdr = p;
p->pkt_payload = (uint8_t *)p + CUSTOM_ZONE_LEN;
p->pkt_len = pkt_hdr->caplen;
if (packet_copy_data((uint8_t *)p->pkt_payload, (uint8_t *)pkt, pkt_hdr->caplen)) {
FREE(p);
return;
}
@@ -314,10 +318,10 @@ void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_c
hash_id = decode_packet(p) % nr_rxq;
packet_enqueue(&pfile_dev_ctx->pkt_queues[hash_id], p);
*/
/*
int rxq_id = 0;
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); */
pio_packet_enqueue(&pfile_dev_ctx->pkt_queues[rxq_id], p);
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,
@@ -349,12 +353,12 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx
//TODO: close pcap file
} else {
// success
struct packet *p = nullptr;
struct pio_packet *p = nullptr;
int i = 0;
pthread_mutex_lock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
do {
p = packet_dequeue(&pfile_dev_ctx->pkt_queues[rxq_id]);
pkts[i] = p;
p = pio_packet_dequeue(&pfile_dev_ctx->pkt_queues[rxq_id]);
pkts[i] = (struct packet *)p;
i++;
} while (p != nullptr && (i < nr_pkts));
pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
@@ -603,9 +607,12 @@ int pio_pcap_file_device_send(struct packet_io_device *pdev, uint32_t txq_id, st
return 0;
}
void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts)
void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct packet **pkts, int nr_pkts)
{
for (uint32_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, __unused int wrk_thread_num)

View File

@@ -13,9 +13,10 @@
#include <stdint.h>
#include <dirent.h>
#include <pcap/pcap.h>
#include <sys/queue.h>
#include "../../common/global_var.h"
#include "../../common/packet_queue.h"
#include "../../common/pio_packet_queue.h"
struct pio_pcap_file_instance_context {
@@ -87,8 +88,8 @@ struct pio_pcap_file_device_context {
bool is_dir;
/* rx packet queue */
struct packet_queue pkt_queues[PKT_QUEUE_MAX_NUM];
/* rx pio packet queue */
struct pio_packet_queue pkt_queues[PKT_QUEUE_MAX_NUM];
struct pcap_file_shared_info shared;
};

View File

@@ -133,7 +133,7 @@ int pio_pcap_live_device_close(struct packet_io_device *pdev)
for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) {
if (pdev->entity.pcap_live_dev_ctx->pkt_queues[i].len != 0) {
release_packet_queue(&pdev->entity.pcap_live_dev_ctx->pkt_queues[i]);
release_pio_packet_queue(&pdev->entity.pcap_live_dev_ctx->pkt_queues[i]);
}
}
@@ -145,12 +145,15 @@ int pio_pcap_live_device_close(struct packet_io_device *pdev)
static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_char *pkt)
{
struct pio_pcap_live_device_context *plive_dev_ctx = (struct pio_pcap_live_device_context *)user;
struct packet *p = CALLOC(struct packet, 1);
struct pio_packet *p = (struct pio_packet *)malloc(SIZE_OF_PIO_PACKET);
if (nullptr == p) {
return;
}
if (packet_copy_data(p, (uint8_t *)pkt, pkt_hdr->caplen)) {
memset(p, 0, sizeof(SIZE_OF_PIO_PACKET));
p->pkt_hdr = p;
p->pkt_payload = (uint8_t *)p + CUSTOM_ZONE_LEN;
p->pkt_len = pkt_hdr->caplen;
if (packet_copy_data((uint8_t *)p->pkt_payload, (uint8_t *)pkt, pkt_hdr->caplen)) {
FREE(p);
return;
}
@@ -160,10 +163,10 @@ static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_h
hash_id = decode_packet(p) % nr_rxq;
packet_enqueue(&pfile_dev_ctx->pkt_queues[hash_id], p);
*/
/*
int rxq_id = 0;
pthread_mutex_lock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q);
packet_enqueue(&plive_dev_ctx->pkt_queues[rxq_id], p);
pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); */
pio_packet_enqueue(&plive_dev_ctx->pkt_queues[rxq_id], p);
pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q);
}
int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet **pkts, int nr_pkts)
@@ -187,12 +190,12 @@ int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id,
} else if (res == 0) {
} else {
struct packet *p = nullptr;
struct pio_packet *p = nullptr;
int i = 0;
pthread_mutex_lock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q);
do {
p = packet_dequeue(&plive_dev_ctx->pkt_queues[rxq_id]);
pkts[i] = p;
p = pio_packet_dequeue(&plive_dev_ctx->pkt_queues[rxq_id]);
pkts[i] = (struct packet *)p;
i++;
} while (p != nullptr && (i < nr_pkts));
pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q);
@@ -220,16 +223,21 @@ int pio_pcap_live_device_send(struct packet_io_device *pdev, uint32_t txq_id, st
int packet_q_len = nr_pkts;
pthread_mutex_lock(&plive_dev_ctx->handle_mutex);
for (uint32_t i = 0; i < nr_pkts; i++) {
res = pcap_sendpacket(plive_dev_ctx->pcap_handle, (u_char *)pkts[i], sizeof(struct packet));
struct pio_packet *p = (struct pio_packet *)pkts[i];
res = pcap_sendpacket(plive_dev_ctx->pcap_handle, (u_char *)p->pkt_payload, p->pkt_len);
}
pthread_mutex_unlock(&plive_dev_ctx->handle_mutex);
/*TODO: when to free pio_packet? */
return 0;
}
void pio_pcap_live_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts)
void pio_pcap_live_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct packet **pkts, int nr_pkts)
{
for (uint32_t i = 0; i < nr_pkts; i++) {
struct pio_packet *p = (struct pio_packet *)pkts[i];
FREE(p);
}
}
int pio_pcap_live_instance_create(struct packet_io_instance *pinst, __unused int wrk_thread_num)

View File

@@ -14,7 +14,7 @@
#include <pcap/pcap.h>
#include "../../common/global_var.h"
#include "../../common/packet_queue.h"
#include "../../common/pio_packet_queue.h"
#define PCAP_STATE_UP 1
#define PCAP_STATE_DOWN 0
@@ -51,7 +51,7 @@ struct pio_pcap_live_device_context {
int pcap_snaplen;
/* rx packet queue */
struct packet_queue pkt_queues[PKT_QUEUE_MAX_NUM];
struct pio_packet_queue pkt_queues[PKT_QUEUE_MAX_NUM];
};
/*

View File

@@ -2,6 +2,12 @@
#include "../packet_io.h"
TEST(PACKET_IO_Test, packet_io_instance_create) {
struct packet_io_config ppio_config;
struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE, 2);
EXPECT_NE(ppio_inst, nullptr);
}
TEST(PACKET_IO_Test, packet_io_open_device) {
struct packet_io_config ppio_config;
struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE, 2);