[ADD] packet_io framework
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
|
||||
add_library(packet_io
|
||||
packet_io.cpp
|
||||
pcap_live_mode/pcap_live.cpp
|
||||
pcap_file_mode/pcap_file.cpp
|
||||
marsio_mode/marsio.cpp
|
||||
)
|
||||
|
||||
add_subdirectory(test)
|
||||
40
src/packet_io/marsio_mode/marsio.cpp
Normal file
40
src/packet_io/marsio_mode/marsio.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: marsio.cpp
|
||||
* Description:
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#include "marsio.h"
|
||||
|
||||
int marsio_device_init(const void *init_data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int marsio_device_fini(const void *init_data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int marsio_device_receive(void *data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int marsio_device_send(void *data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int marsio_instance_create(struct packet_io_instance *pinst) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void marsio_instance_destroy(void) {
|
||||
|
||||
}
|
||||
31
src/packet_io/marsio_mode/marsio.h
Normal file
31
src/packet_io/marsio_mode/marsio.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: marsio.h
|
||||
* Description: marsio runmode api
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct mr_instance;
|
||||
|
||||
struct pio_marsio_device_context {
|
||||
|
||||
};
|
||||
|
||||
int marsio_device_init(const void *init_data);
|
||||
|
||||
int marsio_device_fini(const void *init_data);
|
||||
|
||||
int marsio_device_receive(void *data);
|
||||
|
||||
int marsio_device_send(void *data);
|
||||
|
||||
int marsio_instance_create(struct packet_io_instance *pinst);
|
||||
|
||||
void marsio_instance_destroy(void);
|
||||
@@ -1,37 +1,128 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: packet_io.cpp
|
||||
* Description:
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "packet_io.h"
|
||||
#include "../../sdk/include/logger.h"
|
||||
#include "../../sdk/include/utils.h"
|
||||
#include "../../sdk/include/util_errors.h"
|
||||
#include "./pcap_live_mode/pcap_live.h"
|
||||
#include "./pcap_file_mode/pcap_file.h"
|
||||
#include "./marsio_mode/marsio.h"
|
||||
|
||||
struct packet_io_instance *packet_io_create(packe_io_mode mode, const char *instance_name)
|
||||
struct pio_device_operations device_ops_array[PACKET_IO_RUN_MODE_MAX] =
|
||||
{
|
||||
{
|
||||
.init = pcap_file_device_init,
|
||||
.fini = pcap_file_device_fini,
|
||||
.recv = pcap_file_device_receive,
|
||||
.send = pcap_file_device_send,
|
||||
},
|
||||
|
||||
{
|
||||
.init = pcap_live_device_init,
|
||||
.fini = pcap_live_device_fini,
|
||||
.recv = pcap_live_device_receive,
|
||||
.send = pcap_live_device_send,
|
||||
},
|
||||
|
||||
{
|
||||
.init = marsio_device_init,
|
||||
.fini = marsio_device_fini,
|
||||
.recv = marsio_device_receive,
|
||||
.send = marsio_device_send,
|
||||
}
|
||||
};
|
||||
|
||||
struct pio_instance_operations instance_ops_array[PACKET_IO_RUN_MODE_MAX] =
|
||||
{
|
||||
|
||||
{
|
||||
.create = pcap_file_instance_create,
|
||||
.destroy = pcap_file_instance_destroy,
|
||||
},
|
||||
|
||||
{
|
||||
.create = pcap_live_instance_create,
|
||||
.destroy = pcap_live_instance_destroy,
|
||||
},
|
||||
|
||||
{
|
||||
.create = marsio_instance_create,
|
||||
.destroy = marsio_instance_destroy,
|
||||
}
|
||||
};
|
||||
|
||||
struct packet_io_instance *packet_io_instance_init(struct packet_io_config *ppio_config) {
|
||||
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.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
memset(pio_instance, 0, sizeof(*pio_instance));
|
||||
pio_instance->mode = ppio_config->mode;
|
||||
pio_instance->inst_ops = &instance_ops_array[ppio_config->mode];
|
||||
|
||||
return pio_instance;
|
||||
}
|
||||
|
||||
void packet_io_instance_destroy(struct packet_io_instance *ppio_inst) {
|
||||
if (nullptr != ppio_inst) {
|
||||
if (nullptr != ppio_inst->device_handle) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct packet_io_device *packet_io_device_init(struct packet_io_instance *ppio_inst, void *dev_ctx, uint32_t nr_rxqueue, uint32_t nr_txqueue)
|
||||
{
|
||||
struct packet_io_device *ppio_dev = CALLOC(struct packet_io_device, 1);
|
||||
if (nullptr == ppio_dev) {
|
||||
log_error(ST_ERR_MEM_ALLOC, "packet_io_device alloc failed.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
memset(ppio_dev, 0, sizeof(*ppio_dev));
|
||||
|
||||
ppio_dev->dev_ctx = dev_ctx;
|
||||
ppio_dev->ppio_inst = ppio_inst;
|
||||
ppio_dev->dev_ops = &device_ops_array[ppio_inst->mode];
|
||||
|
||||
int ret = ppio_dev->dev_ops->init(dev_ctx);
|
||||
if (ret < 0) {
|
||||
log_error(ST_ERR_PIO_DEVICE, "packet_io_device init failed.");
|
||||
FREE(ppio_dev);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ppio_dev;
|
||||
}
|
||||
|
||||
struct packet_io_device *packet_io_open_device(struct packet_io_instance *instance,
|
||||
void *dev_ctx, uint32_t nr_rxqueue, uint32_t nr_txqueue) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct packet_io_instance *packet_io_current()
|
||||
void packet_io_close_device(struct packet_io_device *ppio_dev)
|
||||
{
|
||||
return nullptr;
|
||||
ppio_dev->dev_ops->fini(nullptr);
|
||||
}
|
||||
|
||||
void packet_io_destory(struct packet_io_instance *instance)
|
||||
int packet_io_rx(struct packet_io_device *ppio_dev, uint32_t rx_queue_id, struct packet *p[], int nr_p)
|
||||
{
|
||||
return;
|
||||
return ppio_dev->dev_ops->recv(nullptr);
|
||||
}
|
||||
|
||||
|
||||
struct packet_io_device *packet_io_open_device(struct packet_io_instance *instance, const char *dev_sym, uint32_t nr_rxqueue, uint32_t nr_txqueue)
|
||||
int packet_io_tx(struct packet_io_device *ppio_dev, uint32_t rx_queue_id, struct packet *p[], int nr_p)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void packet_io_close_device(struct packet_io_device *dev)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int packet_io_tx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p)
|
||||
{
|
||||
return 0;
|
||||
return ppio_dev->dev_ops->send(nullptr);
|
||||
}
|
||||
@@ -1,22 +1,91 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: packet_io.h
|
||||
* Description: packet io module entry
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include "./marsio_mode/marsio.h"
|
||||
#include "./pcap_file_mode/pcap_file.h"
|
||||
#include "./pcap_live_mode/pcap_live.h"
|
||||
|
||||
struct packet_io_instance;
|
||||
struct packet_io_device;
|
||||
#define DEV_MAX_CNT 16
|
||||
#define NAME_MAX_LEN 64
|
||||
|
||||
enum packe_io_mode
|
||||
{
|
||||
PACKET_IO_PCAP,
|
||||
PACKET_IO_MARSIO,
|
||||
enum packet_io_run_mode {
|
||||
PACKET_IO_RUN_MODE_PCAP_FILE,
|
||||
PACKET_IO_RUN_MODE_PCAP_LIVE,
|
||||
PACKET_IO_RUN_MODE_MARSIO,
|
||||
PACKET_IO_RUN_MODE_MAX,
|
||||
};
|
||||
|
||||
struct packet_io_instance *packet_io_create(packe_io_mode mode, const char *instance_name);
|
||||
struct packet_io_instance *packet_io_current();
|
||||
void packet_io_destory(struct packet_io_instance *instance);
|
||||
struct packet_io_config {
|
||||
enum packet_io_run_mode mode;
|
||||
char dev_name[DEV_MAX_CNT][NAME_MAX_LEN];
|
||||
uint32_t dev_cnt;
|
||||
};
|
||||
|
||||
struct packet_io_device *packet_io_open_device(struct packet_io_instance *instance, const char *dev_sym, uint32_t nr_rxqueue, uint32_t nr_txqueue);
|
||||
struct pio_instance_operations {
|
||||
int (*create)(struct packet_io_instance *);
|
||||
|
||||
void (*destroy)(void);
|
||||
};
|
||||
|
||||
struct packet_io_instance {
|
||||
/* packet io run mode of the instance */
|
||||
enum packet_io_run_mode mode;
|
||||
|
||||
/* device handle set in this instance */
|
||||
struct packet_io_device **device_handle;
|
||||
uint32_t dev_cnt;
|
||||
|
||||
/* instance operations */
|
||||
struct pio_instance_operations *inst_ops;
|
||||
|
||||
union
|
||||
{
|
||||
struct pcap_file_instance *ptr_pcap_file_inst;
|
||||
struct pcap_live_instance *ptr_pcap_live_inst;
|
||||
struct mr_instance *ptr_marsio_inst;
|
||||
} entity;
|
||||
|
||||
};
|
||||
|
||||
struct pio_device_operations {
|
||||
int (*init)(const void *init_data);
|
||||
|
||||
int (*fini)(const void *init_data);
|
||||
|
||||
int (*recv)(void *data);
|
||||
|
||||
int (*send)(void *data);
|
||||
};
|
||||
|
||||
struct packet_io_device {
|
||||
/* device name */
|
||||
const char *dev_name;
|
||||
|
||||
/* device operations */
|
||||
struct pio_device_operations *dev_ops;
|
||||
|
||||
/* packet io device context */
|
||||
void *dev_ctx;
|
||||
|
||||
/* the packet_io's instance of the device */
|
||||
struct packet_io_instance *ppio_inst;
|
||||
};
|
||||
|
||||
struct packet_io_instance *packet_io_instance_init(struct packet_io_config *ppio_config);
|
||||
|
||||
struct packet_io_device *packet_io_open_device(struct packet_io_instance *instance, void *dev_ctx, uint32_t nr_rxqueue, uint32_t nr_txqueue);
|
||||
void packet_io_close_device(struct packet_io_device *dev);
|
||||
|
||||
int packet_io_rx(struct packet_io_device *dev, uint32_t rx_queue_id, struct packet *p[], int nr_p);
|
||||
|
||||
43
src/packet_io/pcap_file_mode/pcap_file.cpp
Normal file
43
src/packet_io/pcap_file_mode/pcap_file.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: pcap_file.cpp
|
||||
* Description: pcap file runmode api
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "pcap_file.h"
|
||||
|
||||
int pcap_file_device_init(const void *init_data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pcap_file_device_fini(const void *init_data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pcap_file_device_receive(void *data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pcap_file_device_send(void *data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int pcap_file_instance_create(struct packet_io_instance *pinst) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pcap_file_instance_destroy(void) {
|
||||
|
||||
}
|
||||
31
src/packet_io/pcap_file_mode/pcap_file.h
Normal file
31
src/packet_io/pcap_file_mode/pcap_file.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: pcap_file.h
|
||||
* Description: pcap file runmode api
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct pcap_file_instance;
|
||||
|
||||
struct pio_pcap_file_device_context {
|
||||
|
||||
};
|
||||
|
||||
int pcap_file_device_init(const void *init_data);
|
||||
|
||||
int pcap_file_device_fini(const void *init_data);
|
||||
|
||||
int pcap_file_device_receive(void *data);
|
||||
|
||||
int pcap_file_device_send(void *data);
|
||||
|
||||
int pcap_file_instance_create(struct packet_io_instance *pinst);
|
||||
|
||||
void pcap_file_instance_destroy(void);
|
||||
40
src/packet_io/pcap_live_mode/pcap_live.cpp
Normal file
40
src/packet_io/pcap_live_mode/pcap_live.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: pcap_live.cpp
|
||||
* Description:
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#include "pcap_live.h"
|
||||
|
||||
int pcap_live_device_init(const void *init_data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pcap_live_device_fini(const void *init_data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pcap_live_device_receive(void *data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pcap_live_device_send(void *data) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pcap_live_instance_create(struct packet_io_instance *pinst) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pcap_live_instance_destroy(void) {
|
||||
|
||||
}
|
||||
31
src/packet_io/pcap_live_mode/pcap_live.h
Normal file
31
src/packet_io/pcap_live_mode/pcap_live.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
**********************************************************************************************
|
||||
* File: pcap_live.h
|
||||
* Description: pcap live runmode api
|
||||
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
||||
* Date: 2022-07-15
|
||||
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
||||
***********************************************************************************************
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct pcap_live_instance;
|
||||
|
||||
struct pio_pcap_live_device_context {
|
||||
|
||||
};
|
||||
|
||||
int pcap_live_device_init(const void *init_data);
|
||||
|
||||
int pcap_live_device_fini(const void *init_data);
|
||||
|
||||
int pcap_live_device_receive(void *data);
|
||||
|
||||
int pcap_live_device_send(void *data);
|
||||
|
||||
int pcap_live_instance_create(struct packet_io_instance *pinst);
|
||||
|
||||
void pcap_live_instance_destroy(void);
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
#include "packet_io.h"
|
||||
|
||||
TEST(PACKET_IO_Test, packet_io_create) {
|
||||
EXPECT_EQ(packet_io_create(PACKET_IO_PCAP, "TEST"), nullptr);
|
||||
TEST(PACKET_IO_Test, packet_io_open_device) {
|
||||
struct packet_io_config ppio_config;
|
||||
struct packet_io_instance *ppio_inst = packet_io_instance_init(&ppio_config);
|
||||
EXPECT_EQ(packet_io_open_device(ppio_inst, nullptr, 1, 1), nullptr);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
|
||||
Reference in New Issue
Block a user