[ADD] packet_io framework

This commit is contained in:
liuwentan
2022-07-26 15:05:14 +08:00
parent da47b80442
commit 82a97ff067
16 changed files with 516 additions and 46 deletions

View File

@@ -1,3 +1,19 @@
/*
**********************************************************************************************
* File: logger.h
* Description: log module
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#pragma once
struct logger;
#define log_debug(x, ...)
#define log_info(x, ...)
#define log_notice(x, ...)
#define log_error(x, ...)

34
sdk/include/util_errors.h Normal file
View File

@@ -0,0 +1,34 @@
/*
**********************************************************************************************
* File: util_errors.h
* Description: different error type
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#define CASE_CODE(E) case E: return #E
/* ST is short for stellar */
typedef enum {
ST_OK = 0,
ST_ERR_MEM_ALLOC,
ST_ERR_RUN_MODE,
ST_ERR_PIO_INSTANCE,
ST_ERR_PIO_DEVICE,
ST_ERR_MAX
} error_code_t;
const char *st_error_to_string(error_code_t err)
{
switch (err) {
CASE_CODE (ST_OK);
CASE_CODE (ST_ERR_MEM_ALLOC);
CASE_CODE (ST_ERR_RUN_MODE);
CASE_CODE (ST_ERR_PIO_INSTANCE);
CASE_CODE (ST_ERR_PIO_DEVICE);
}
return "UNKNOWN_ERROR";
}

View File

@@ -2,8 +2,8 @@
#include <stdlib.h> //calloc
#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))
#define FREE(p) {free(*p);*p=NULL;}
#define CALLOC(type, number) ((type *)calloc(sizeof(type), number))
#define FREE(p) {free(p);p=NULL;}
#define TRUE 1
#define FALSE 0

16
src/app.toml Normal file
View File

@@ -0,0 +1,16 @@
[THREAD]
thread_num = 1
[PACKET_IO]
"""
example1:
run_mode = pcap_live
interface = [eth0, eth1]
"""
run_mode = pcap_live # three mode: pcap_file/pcap_live/marsio
interface = [eth0, eth1]

View File

@@ -2,6 +2,13 @@
#include "session_manager.h"
#include "plugin_manager.h"
#include "http.h"
#include "../sdk/include/logger.h"
struct engine_instance {
struct packet_io_config pio_config;
};
struct engine_instance g_engine_instance;
struct packet_io_loop_arg
{
@@ -39,19 +46,35 @@ void packet_io_loop(struct packet_io_loop_arg *arg)
}
struct packet_io_device *packet_io_init(int worker_thread_num, const char *instance_name, const char *device_name)
struct packet_io_device *packet_io_init(int worker_thread_num, const char *device_name)
{
struct packet_io_instance * instance = packet_io_create(PACKET_IO_PCAP, instance_name);
struct packet_io_device *dev = packet_io_open_device(instance, device_name, worker_thread_num, worker_thread_num);
return dev;
struct packet_io_instance *ppio_instance = packet_io_instance_init(&g_engine_instance.pio_config);
if (nullptr == ppio_instance) {
log_error(ST_ERR_PIO_INSTANCE, "packet_io instance init failed.");
return nullptr;
}
struct packet_io_device *ppio_device = packet_io_open_device(ppio_instance, nullptr, worker_thread_num, worker_thread_num);
if (nullptr == ppio_device) {
log_error(ST_ERR_PIO_DEVICE, "packet_io device open failed.");
}
return ppio_device;
}
int main(int argc, char ** argv)
{
//config_manager_init
/* global engine instance init */
memset(&g_engine_instance, 0, sizeof(g_engine_instance));
g_engine_instance.pio_config.mode = PACKET_IO_RUN_MODE_PCAP_FILE;
strncpy(g_engine_instance.pio_config.dev_name[0], "./test.pcap", sizeof(NAME_MAX_LEN));
g_engine_instance.pio_config.dev_cnt = 1;
/* packet io init */
packet_io_instance_init(&g_engine_instance.pio_config);
session_manager_session_event_register(http_decoder, SESSION_TYPE_HTTP);
//packet_io_init
//create_worker_thread

View File

@@ -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)

View 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) {
}

View 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);

View File

@@ -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);
}

View File

@@ -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);

View 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) {
}

View 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);

View 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) {
}

View 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);

View File

@@ -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)

View File

@@ -39,7 +39,7 @@ void plugin_manager_dispatch(struct session_event *event)
case SESSION_EVENT_OPENING:
TAILQ_FOREACH(session_ev_cb, &t_map->session_ev_cb_list, session_event_cb_tq_entries)
{
struct session_event *ev = ALLOC(struct session_event, 1);
struct session_event *ev = CALLOC(struct session_event, 1);
ev->callback = session_ev_cb->callback;
ev->callback(s, SESSION_EVENT_OPENING, NULL, NULL, 0, &ev->cb_pme);
if(ev->state == 1)
@@ -48,7 +48,7 @@ void plugin_manager_dispatch(struct session_event *event)
}
else
{
FREE(&ev);// TODO;
FREE(ev);// TODO;
}
}
break;