[PACKET_IO] add function for operating packets

This commit is contained in:
liuwentan
2022-08-09 18:32:25 +08:00
parent c2d5b9cdb7
commit 873b25794b
10 changed files with 218 additions and 55 deletions

View File

@@ -1,6 +1,6 @@
/*
**********************************************************************************************
* File: packet_queue.cpp
* File: pio_packet_queue.cpp
* Description:
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
@@ -8,9 +8,23 @@
***********************************************************************************************
*/
#include <string.h>
#include "../../sdk/include/utils.h"
#include "pio_packet_queue.h"
static int packet_copy_data_offset(uint8_t *ptr, uint32_t offset, const uint8_t *data, uint32_t data_len)
{
memcpy(ptr + offset, data, data_len);
return 0;
}
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);
}
void pio_packet_enqueue(struct pio_packet_queue *q, struct pio_packet *p)
{
if (nullptr == p)

View File

@@ -21,7 +21,7 @@
#define SIZE_OF_PIO_PACKET (DEFAULT_PACKET_SIZE + sizeof(struct pio_packet))
/*
/**
* @brief pcap_live/pcap_file mode packet structure
*
* |<-pkt_hdr |<-pkt_payload
@@ -37,13 +37,13 @@ struct pio_packet {
void *pkt_hdr;
/* pkt length */
uint64_t pkt_len;
uint32_t pkt_len;
/* pkt payload pointer */
void *pkt_payload;
/* reference counts */
uint64_t ref_cnt;
uint32_t ref_cnt;
struct pio_packet *prev;
@@ -57,6 +57,8 @@ struct pio_packet_queue {
pthread_mutex_t mutex_q;
};
int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len);
void pio_packet_enqueue(struct pio_packet_queue *, struct pio_packet *);
struct pio_packet *pio_packet_dequeue(struct pio_packet_queue *);
void release_pio_packet_queue(struct pio_packet_queue *);

View File

@@ -282,6 +282,8 @@ int pio_marsio_device_open(struct packet_io_device *pdev)
return -1;
}
pdev->entity.marsio_dev_ctx->pio_dev = pdev;
struct mr_instance *mr_inst_handle = pdev->ppio_inst->entity.marsio_inst_ctx->mr_inst_handle;
/* marsio_open_device() return marsio device handle*/
pdev->entity.marsio_dev_ctx->mr_dev_handle = \
@@ -424,4 +426,26 @@ void pio_marsio_instance_destroy(struct packet_io_instance *pinst)
pio_marsio_device_close(pinst->devices[i]);
FREE(pinst->devices[i]);
}
}
void *pio_marsio_device_buff_ctrlzone(struct packet *p)
{
int zone_id = 0;
return g_marsio_dll_func.marsio_buff_ctrlzone((marsio_buff_t *)p, zone_id);
}
char *pio_marsio_device_buff_mtod(struct packet *p)
{
return g_marsio_dll_func.marsio_buff_mtod((marsio_buff_t *)p);
}
uint32_t pio_marsio_device_buff_buflen(struct packet *p)
{
return g_marsio_dll_func.marsio_buff_buflen((marsio_buff_t *)p);
}
uint32_t pio_marsio_device_buff_datalen(struct packet *p)
{
return g_marsio_dll_func.marsio_buff_datalen((marsio_buff_t *)p);
}

View File

@@ -14,9 +14,9 @@
#include <marsio.h>
/*
* dll is short for dynamic link lib
* the following entries is supported by marsio system
/**
* @brief dll is short for dynamic link lib
* the following entries is supported by marsio system
**/
struct marsio_dll_function_entries {
@@ -82,7 +82,7 @@ struct pio_marsio_instance_context {
struct mr_instance *mr_inst_handle;
};
/*
/**
* struct pio_marsio_device_context - marsio device context
* @mr_dev_handle: marsio device handle
* if marsio device receive packets, use mr_dev_handle
@@ -92,13 +92,15 @@ struct pio_marsio_instance_context {
struct pio_marsio_device_context {
struct mr_vdev *mr_dev_handle;
struct mr_sendpath * mr_sendpath_handle;
struct packet_io_device *pio_dev;
};
/*
/**
* @brief open marsio device
*
* @param pdev: the marsio device's pointer
* pdev->dev_name: the name of marsio device, such as eth1, eth2, ...
* pdev->dev_name: the name of marsio device, such as eth1, eth2, ...
* pdev->rxq_num: number of the packet receiving queues for the device
* pdev->txq_num: number of the packet sending queues for the device
**/
@@ -109,7 +111,7 @@ int pio_marsio_device_open(struct packet_io_device *pdev);
**/
int pio_marsio_device_close(struct packet_io_device *pdev);
/*
/**
* @brief receive packets from device's single rx queue which specified by rxq_id
*
* @param pdev: the marsio device's pointer
@@ -121,7 +123,7 @@ int pio_marsio_device_close(struct packet_io_device *pdev);
*/
int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet **pkts, int nr_pkts);
/*
/**
* @brief send packets by device's single tx queue which specified by txq_id
*
* @param pdev: the marsio device's pointer
@@ -133,7 +135,7 @@ int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, st
*/
int pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct packet **pkts, int nr_pkts);
/*
/**
* @brief manually free packet's memory
*
* @param pdev: the marsio device's pointer
@@ -145,4 +147,12 @@ void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, str
int pio_marsio_instance_create(struct packet_io_instance *pinst, int wrk_thread_num);
void pio_marsio_instance_destroy(struct packet_io_instance *pinst);
void pio_marsio_instance_destroy(struct packet_io_instance *pinst);
void *pio_marsio_device_buff_ctrlzone(struct packet *p);
char *pio_marsio_device_buff_mtod(struct packet *p);
uint32_t pio_marsio_device_buff_buflen(struct packet *p);
uint32_t pio_marsio_device_buff_datalen(struct packet *p);

View File

@@ -21,8 +21,11 @@ struct pio_device_operations pio_device_ops_array[PACKET_IO_RUN_MODE_MAX] =
.open = pio_pcap_file_device_open,
.close = pio_pcap_file_device_close,
.recv = pio_pcap_file_device_receive,
.send = pio_pcap_file_device_send,
.pkt_free = pio_pcap_file_device_pkt_free,
.buff_ctrlzone = pio_pcap_file_device_buff_ctrlzone,
.buff_mtod = pio_pcap_file_device_buff_mtod,
.buff_buflen = pio_pcap_file_device_buff_buflen,
.buff_datalen = pio_pcap_file_device_buff_datalen,
},
{
@@ -31,6 +34,10 @@ struct pio_device_operations pio_device_ops_array[PACKET_IO_RUN_MODE_MAX] =
.recv = pio_pcap_live_device_receive,
.send = pio_pcap_live_device_send,
.pkt_free = pio_pcap_live_device_pkt_free,
.buff_ctrlzone = pio_pcap_live_device_buff_ctrlzone,
.buff_mtod = pio_pcap_live_device_buff_mtod,
.buff_buflen = pio_pcap_live_device_buff_buflen,
.buff_datalen = pio_pcap_live_device_buff_datalen,
},
{
@@ -39,6 +46,10 @@ struct pio_device_operations pio_device_ops_array[PACKET_IO_RUN_MODE_MAX] =
.recv = pio_marsio_device_receive,
.send = pio_marsio_device_send,
.pkt_free = pio_marsio_device_pkt_free,
.buff_ctrlzone = pio_marsio_device_buff_ctrlzone,
.buff_mtod = pio_marsio_device_buff_mtod,
.buff_buflen = pio_marsio_device_buff_buflen,
.buff_datalen = pio_marsio_device_buff_datalen,
}
};
@@ -163,19 +174,27 @@ int packet_io_device_tx(struct packet_io_device *pdev, uint32_t txq_id, struct p
return pdev->dev_ops->send(pdev, txq_id, pkts, nr_pkts);
}
void packet_io_pkts_free(struct packet_io_instance *pinst, uint32_t qid, struct packet **pkts, int nr_pkts)
void packet_io_pkts_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts)
{
return pdev->dev_ops->pkt_free(pdev, qid, pkts, nr_pkts);
}
static int packet_copy_data_offset(uint8_t *ptr, uint32_t offset, const uint8_t *data, uint32_t data_len)
void *packet_io_buff_ctrlzone(struct packet_io_device *pdev, struct packet *p)
{
memcpy(ptr + offset, data, data_len);
return 0;
return pdev->dev_ops->buff_ctrlzone(p);
}
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);
char *packet_io_buff_mtod(struct packet_io_device *pdev, struct packet *p)
{
return pdev->dev_ops->buff_mtod(p);
}
uint32_t packet_io_buff_buflen(struct packet_io_device *pdev, struct packet *p)
{
return pdev->dev_ops->buff_buflen(p);
}
uint32_t packet_io_buff_datalen(struct packet_io_device *pdev, struct packet *p)
{
return pdev->dev_ops->buff_datalen(p);
}

View File

@@ -19,7 +19,7 @@
#include "./pcap_file_mode/pio_pcap_file.h"
#include "./marsio_mode/pio_marsio.h"
/*
/**
* note:
* 1. packet_io_XXX function is supported by packet_io.h
* 2. pio_XXX function is supported by pio_pcap_live.h/pio_pcap_file.h/pio_marsio.h
@@ -65,6 +65,14 @@ struct pio_device_operations {
int (*send)(struct packet_io_device *pdev, uint32_t txq_id, struct packet **pkts, int nr_pkts);
void (*pkt_free)(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts);
void *(*buff_ctrlzone)(struct packet *p);
char *(*buff_mtod)(struct packet *p);
uint32_t (*buff_buflen)(struct packet *p);
uint32_t (*buff_datalen)(struct packet *p);
};
struct packet_io_device {
@@ -91,7 +99,7 @@ struct packet_io_device {
struct packet_io_instance *ppio_inst;
};
/*
/**
* @brief create packet_io instance which will manage packet_io device
*
* @param instance_name: packet_io instance name
@@ -105,7 +113,7 @@ packet_io_instance_create(const char *instance_name, const enum packet_io_run_mo
/* destroy packet_io instance */
void packet_io_instance_destroy(struct packet_io_instance *pinst);
/*
/**
* @brief open packet_io device for send/receive packets
*
* @param pinst: packet_io instance pointer
@@ -116,10 +124,10 @@ void packet_io_instance_destroy(struct packet_io_instance *pinst);
struct packet_io_device *
packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq);
/* close packet_io device */
/** close packet_io device */
void packet_io_device_close(struct packet_io_device *dev);
/*
/**
* @brief packet_io device receive function
*
* @param pdev: packet_io device pointer
@@ -129,7 +137,7 @@ void packet_io_device_close(struct packet_io_device *dev);
**/
int packet_io_device_rx(struct packet_io_device *pdev, uint32_t rxq_id, struct packet **pkts, int nr_pkts);
/*
/**
* @brief packet_io device send function
*
* @param pdev: packet_io device pointer
@@ -144,4 +152,23 @@ 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(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len);
/**
* @brief get packet_io packet's ctrlzone
* @note ctrlzone's memory is 64 bytes, do not exceed it
*/
void *packet_io_buff_ctrlzone(struct packet_io_device *pdev, struct packet *p);
/**
* @brief get packet_io packet's data pointer
*/
char *packet_io_buff_mtod(struct packet_io_device *pdev, struct packet *p);
/**
* @brief get packet_io packet's buffer length
*/
uint32_t packet_io_buff_buflen(struct packet_io_device *pdev, struct packet *p);
/**
* @brief get packet_io packet's data length
*/
uint32_t packet_io_buff_datalen(struct packet_io_device *pdev, struct packet *p);

View File

@@ -240,6 +240,8 @@ int pio_pcap_file_device_open(struct packet_io_device *pdev)
return -1;
}
pdev->entity.pcap_file_dev_ctx->pio_dev = pdev;
status = pcap_file_shared_init(pdev->entity.pcap_file_dev_ctx, pdev->rxq_num);
if (status < 0) {
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "pcap file shared init failed.");
@@ -313,6 +315,7 @@ void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_c
return;
}
uint32_t nr_rxq = pfile_dev_ctx->pio_dev->rxq_num;
/*
hash to specific queue id and enqueue
hash_id = decode_packet(p) % nr_rxq;
@@ -324,7 +327,7 @@ 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, uint32_t nr_rxq, uint32_t rxq_id,
static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id,
struct packet **pkts, int nr_pkts)
{
if (pfile_dev_ctx->entity.file->first_pkt_hdr != nullptr) {
@@ -495,7 +498,7 @@ 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, uint32_t nr_rxq, uint32_t rxq_id,
static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id,
struct packet **pkts, int nr_pkts)
{
int res = -1;
@@ -542,7 +545,7 @@ static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_de
return -1;
} else {
pfile_dev_ctx->entity.dir->current_file = pfile_info;
res = pcap_file_dispatch(pfile_dev_ctx, nr_rxq, rxq_id, pkts, nr_pkts);
res = pcap_file_dispatch(pfile_dev_ctx, rxq_id, pkts, nr_pkts);
if (res < 0) {
FREE(current_file);
return -1;
@@ -559,7 +562,7 @@ static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_de
}
} else {
/* file has been opened */
res = pcap_file_dispatch(pfile_dev_ctx, nr_rxq, rxq_id, pkts, nr_pkts);
res = pcap_file_dispatch(pfile_dev_ctx, rxq_id, pkts, nr_pkts);
if (res < 0) {
return -1;
}
@@ -590,10 +593,10 @@ int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id,
int 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, pdev->rxq_num, rxq_id, pkts, nr_pkts);
res = pcap_file_dispatch(pfile_dev_ctx, rxq_id, pkts, nr_pkts);
} else {
log_info("Start reading directory:%s", pfile_dev_ctx->entity.dir->dir_name);
res = pcap_directory_dispatch(pfile_dev_ctx, pdev->rxq_num, rxq_id, pkts, nr_pkts);
res = pcap_directory_dispatch(pfile_dev_ctx, rxq_id, pkts, nr_pkts);
}
//pcap_file_exit(status, &pfile_dev_ctx->shared.last_processed_ts);
@@ -601,12 +604,6 @@ int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id,
return res;
}
int pio_pcap_file_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct packet **pkts, int nr_pkts)
{
return 0;
}
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++) {
@@ -643,4 +640,28 @@ void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst)
pio_pcap_file_device_close(pinst->devices[i]);
FREE(pinst->devices[i]);
}
}
void *pio_pcap_file_device_buff_ctrlzone(struct packet *p)
{
struct pio_packet *pkt = (struct pio_packet *)p;
return pkt->pkt_hdr;
}
char *pio_pcap_file_device_buff_mtod(struct packet *p)
{
struct pio_packet *pkt = (struct pio_packet *)p;
return (char *)pkt->pkt_payload;
}
uint32_t pio_pcap_file_device_buff_buflen(struct 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 packet *p)
{
struct pio_packet *pkt = (struct pio_packet *)p;
return (pkt->pkt_len);
}

View File

@@ -77,7 +77,7 @@ struct pcap_file_directory_info {
struct pcap_file_shared_info *shared;
};
/*
/**
* @brief pio_pcap_file_device_context - pcap file device context
**/
struct pio_pcap_file_device_context {
@@ -92,9 +92,12 @@ struct pio_pcap_file_device_context {
struct pio_packet_queue pkt_queues[PKT_QUEUE_MAX_NUM];
struct pcap_file_shared_info shared;
/* point to packet_io device it belongs to */
struct packet_io_device *pio_dev;
};
/*
/**
* @brief open pcap_file device
*
* @param pdev: pcap_file device's pointer which support following params
@@ -104,17 +107,23 @@ struct pio_pcap_file_device_context {
**/
int pio_pcap_file_device_open(struct packet_io_device *pdev);
/*
/**
* @brief close pcap_live device
**/
int pio_pcap_file_device_close(struct packet_io_device *pdev);
int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet **pkts, int nr_pkts);
int pio_pcap_file_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct packet **pkts, int nr_pkts);
void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct packet **pkts, int nr_pkts);
int pio_pcap_file_instance_create(struct packet_io_instance *pinst, int wrk_thread_num);
void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst);
void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst);
void *pio_pcap_file_device_buff_ctrlzone(struct packet *p);
char *pio_pcap_file_device_buff_mtod(struct packet *p);
uint32_t pio_pcap_file_device_buff_buflen(struct packet *p);
uint32_t pio_pcap_file_device_buff_datalen(struct packet *p);

View File

@@ -21,7 +21,7 @@
#define DEFAULT_MAX_PACKET_SIZE 65535
#define TIMEOUT_MS 500
static int pcap_live_init(struct pio_pcap_live_device_context *pcap_live_dev_ctx, const char *dev_name, uint32_t nr_rxq)
static int pcap_live_init(struct pio_pcap_live_device_context *pcap_live_dev_ctx, const char *dev_name)
{
if (nullptr == pcap_live_dev_ctx) {
return -1;
@@ -114,7 +114,9 @@ int pio_pcap_live_device_open(struct packet_io_device *pdev)
pthread_mutex_init(&pdev->entity.pcap_live_dev_ctx->handle_mutex, nullptr);
res = pcap_live_init(pdev->entity.pcap_live_dev_ctx, pdev->dev_name, pdev->rxq_num);
pdev->entity.pcap_live_dev_ctx->pio_dev = pdev;
res = pcap_live_init(pdev->entity.pcap_live_dev_ctx, pdev->dev_name);
if (res < 0) {
log_error(ST_ERR_PIO_PCAP_LIVE_DEVICE, "init pcap live failed.");
FREE(pdev->entity.pcap_live_dev_ctx);
@@ -158,6 +160,7 @@ static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_h
return;
}
uint32_t nr_rxq = plive_dev_ctx->pio_dev->rxq_num;
/*
hash to specific queue id and enqueue
hash_id = decode_packet(p) % nr_rxq;
@@ -268,4 +271,28 @@ void pio_pcap_live_instance_destroy(struct packet_io_instance *pinst)
pio_pcap_live_device_close(pinst->devices[i]);
FREE(pinst->devices[i]);
}
}
void *pio_pcap_live_device_buff_ctrlzone(struct packet *p)
{
struct pio_packet *pkt = (struct pio_packet *)p;
return pkt->pkt_hdr;
}
char *pio_pcap_live_device_buff_mtod(struct packet *p)
{
struct pio_packet *pkt = (struct pio_packet *)p;
return (char *)pkt->pkt_payload;
}
uint32_t pio_pcap_live_device_buff_buflen(struct packet *p)
{
struct pio_packet *pkt = (struct pio_packet *)p;
return (pkt->pkt_len + CUSTOM_ZONE_LEN);
}
uint32_t pio_pcap_live_device_buff_datalen(struct packet *p)
{
struct pio_packet *pkt = (struct pio_packet *)p;
return (pkt->pkt_len);
}

View File

@@ -23,7 +23,7 @@ struct pio_pcap_live_instance_context {
};
/*
/**
* @brief pio_pcap_file_device_context - pcap file device context
**/
struct pio_pcap_live_device_context {
@@ -52,9 +52,11 @@ struct pio_pcap_live_device_context {
/* rx packet queue */
struct pio_packet_queue pkt_queues[PKT_QUEUE_MAX_NUM];
struct packet_io_device *pio_dev;
};
/*
/**
* @brief open pcap_live device
*
* @param pdev: pcap_live device's pointer which support following params
@@ -64,7 +66,7 @@ struct pio_pcap_live_device_context {
**/
int pio_pcap_live_device_open(struct packet_io_device *pdev);
/*
/**
* @brief close pcap_live device
**/
int pio_pcap_live_device_close(struct packet_io_device *pdev);
@@ -77,4 +79,12 @@ void pio_pcap_live_device_pkt_free(struct packet_io_device *pdev, uint32_t qid,
int pio_pcap_live_instance_create(struct packet_io_instance *pinst, int wrk_thread_num);
void pio_pcap_live_instance_destroy(struct packet_io_instance *pinst);
void pio_pcap_live_instance_destroy(struct packet_io_instance *pinst);
void *pio_pcap_live_device_buff_ctrlzone(struct packet *p);
char *pio_pcap_live_device_buff_mtod(struct packet *p);
uint32_t pio_pcap_live_device_buff_buflen(struct packet *p);
uint32_t pio_pcap_live_device_buff_datalen(struct packet *p);