From 536bf31be1637eb53bdec75fe305bd386824aac7 Mon Sep 17 00:00:00 2001 From: liuwentan Date: Thu, 28 Jul 2022 20:30:48 +0800 Subject: [PATCH] [PAKCET_IO] framework intermediate state --- sdk/include/utils.h | 1 + src/common/global_var.h | 2 +- src/main.cpp | 13 ++- src/packet_io/marsio_mode/pio_marsio.cpp | 109 ++++++++++++------ src/packet_io/marsio_mode/pio_marsio.h | 17 ++- src/packet_io/packet_io.cpp | 44 ++++--- src/packet_io/packet_io.h | 32 ++--- .../pcap_file_mode/pio_pcap_file.cpp | 2 +- src/packet_io/pcap_file_mode/pio_pcap_file.h | 13 ++- .../pcap_live_mode/pio_pcap_live.cpp | 2 +- src/packet_io/pcap_live_mode/pio_pcap_live.h | 13 ++- src/packet_io/test/gtest_packet_io.cpp | 2 +- 12 files changed, 168 insertions(+), 82 deletions(-) diff --git a/sdk/include/utils.h b/sdk/include/utils.h index 47968d5..6dac461 100644 --- a/sdk/include/utils.h +++ b/sdk/include/utils.h @@ -3,6 +3,7 @@ #include //calloc #define CALLOC(type, number) ((type *)calloc(sizeof(type), number)) +#define REALLOC(addr, number) ((void *)realloc(addr, number)) #define FREE(p) {free(p);p=NULL;} #define TRUE 1 diff --git a/src/common/global_var.h b/src/common/global_var.h index b3ed09f..05f8ab6 100644 --- a/src/common/global_var.h +++ b/src/common/global_var.h @@ -13,7 +13,7 @@ #include #include -#define DEV_MAX_CNT 16 +#define DEV_MAX_CNT 64 enum packet_io_run_mode { PACKET_IO_RUN_MODE_PCAP_FILE, diff --git a/src/main.cpp b/src/main.cpp index fd47c36..caf9025 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -51,20 +51,21 @@ void packet_io_loop(struct packet_io_loop_arg *arg) } -struct packet_io_device *packet_io_init(const char *instance_name, enum packet_io_run_mode mode, int worker_thread_num) +struct packet_io_device * +packet_io_init(const char *instance_name, const enum packet_io_run_mode mode, const int worker_thread_num) { - struct packet_io_instance *ppio_instance = packet_io_instance_create(instance_name, mode, worker_thread_num); - if (nullptr == ppio_instance) { + struct packet_io_instance *ppio_inst = packet_io_instance_create(instance_name, mode, worker_thread_num); + if (nullptr == ppio_inst) { 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) { + struct packet_io_device *ppio_dev = packet_io_open_device(ppio_inst, "eth1", worker_thread_num, worker_thread_num); + if (nullptr == ppio_dev) { log_error(ST_ERR_PIO_DEVICE, "packet_io device open failed."); } - return ppio_device; + return ppio_dev; } int main(int argc, char ** argv) diff --git a/src/packet_io/marsio_mode/pio_marsio.cpp b/src/packet_io/marsio_mode/pio_marsio.cpp index de5349c..0bb712f 100644 --- a/src/packet_io/marsio_mode/pio_marsio.cpp +++ b/src/packet_io/marsio_mode/pio_marsio.cpp @@ -15,6 +15,7 @@ #include "../../common/global_var.h" #include "../../common/common.h" #include "../../sdk/include/logger.h" +#include "../../sdk/include/utils.h" #include "../../sdk/include/util_errors.h" static struct marsio_dll_function_entries shared_marsio_dll_func_entries; @@ -36,154 +37,176 @@ static int pio_get_marsio_dll_function_entries(void) shared_marsio_dll_func_entries.ptr_marsio_create = (struct mr_instance *(*)(void))dlsym(marsio_so_handle, "marsio_create"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_create) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_create", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_create", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_init = (int (*)(struct mr_instance *, const char *))dlsym(marsio_so_handle, "marsio_init"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_init) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_init", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_init", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_destroy = (int (*)(struct mr_instance *))dlsym(marsio_so_handle, "marsio_destory"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_destroy) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_destory", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_destory", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_open_device = (struct mr_vdev *(*)(struct mr_instance *, const char *, unsigned int, unsigned int))dlsym(marsio_so_handle, "marsio_open_device"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_open_device) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_open_device", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_open_device", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_close_device = (void (*)(struct mr_vdev *))dlsym(marsio_so_handle, "marsio_close_device"); if (nullptr ==shared_marsio_dll_func_entries.ptr_marsio_close_device) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_close_device", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_close_device", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_option_set = (int (*)(struct mr_instance *, marsio_opt_type_t, void *, size_t))dlsym(marsio_so_handle, "marsio_option_set"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_option_set) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_option_set", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_option_set", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_sendpath_create_by_vdev = (struct mr_sendpath *(*)(struct mr_vdev *))dlsym(marsio_so_handle, "marsio_sendpath_create_by_vdev"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_sendpath_create_by_vdev) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_sendpath_create_by_vdev", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_sendpath_create_by_vdev", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_sendpath_destroy = (void (*)(struct mr_sendpath *))dlsym(marsio_so_handle, "marsio_sendpath_destory"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_sendpath_destroy) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_sendpath_destory", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_sendpath_destory", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_thread_init = (int (*)(struct mr_instance *))dlsym(marsio_so_handle, "marsio_thread_init"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_thread_init) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_thread_init", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_thread_init", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_recv_burst = (int (*)(struct mr_vdev *, queue_id_t, marsio_buff_t **, int))dlsym(marsio_so_handle, "marsio_recv_burst"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_recv_burst) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_recv_burst", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_recv_burst", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_send_burst = (int (*)(struct mr_sendpath *, queue_id_t, marsio_buff_t **, int))dlsym(marsio_so_handle, "marsio_send_burst"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_send_burst) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_send_burst_with_options = (int (*)(struct mr_sendpath *, queue_id_t, marsio_buff_t **, int, uint16_t))dlsym(marsio_so_handle, "marsio_send_burst_with_options"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_send_burst_with_options) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst_with_options", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst_with_options", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_malloc_global = (int (*)(struct mr_instance *, marsio_buff_t **, unsigned int, int, int))dlsym(marsio_so_handle, "marsio_buff_malloc_global"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_malloc_global) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_malloc_global", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_malloc_global", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_free = (void (*)(struct mr_instance *, marsio_buff_t **,unsigned int, int, int))dlsym(marsio_so_handle, "marsio_buff_free"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_free) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_free", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_free", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_append = (char * (*)(marsio_buff_t *, uint16_t))dlsym(marsio_so_handle, "marsio_buff_append"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_append) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_append", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_append", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_ctrlzone = (void * (*)(marsio_buff_t *, uint8_t))dlsym(marsio_so_handle, "marsio_buff_ctrlzone"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_ctrlzone) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_ctrlzone", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_ctrlzone", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_ctrlzone_set = (void (*)(marsio_buff_t *, uint8_t, void *, uint8_t))dlsym(marsio_so_handle, "marsio_buff_ctrlzone_set"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_ctrlzone_set) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_ctrlzone_set", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_ctrlzone_set", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_set_rehash_index = (void (*)(marsio_buff_t *, uint32_t))dlsym(marsio_so_handle, "marsio_buff_set_rehash_index"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_set_rehash_index) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_set_rehash_index", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_set_rehash_index", + g_engine_instance.config.lib.libmarsio_path); shared_marsio_dll_func_entries.ptr_marsio_buff_set_rehash_index = fake_marsio_buff_set_rehash_index; } shared_marsio_dll_func_entries.ptr_marsio_buff_mtod = (char * (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_mtod"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_mtod) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_mtod", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_mtod", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_datalen = (uint32_t (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_datalen"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_datalen) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_datalen", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_datalen", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_buflen = (uint32_t (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_buflen"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_buflen) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_buflen", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_buflen", + g_engine_instance.config.lib.libmarsio_path); return -1; } shared_marsio_dll_func_entries.ptr_marsio_buff_clone_with_options = (marsio_buff_t * (*)(struct mr_instance *, marsio_buff_t *, int, int, uint16_t))dlsym(marsio_so_handle, "marsio_buff_clone_with_options"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_clone_with_options) { - printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_clone_with_options", g_engine_instance.config.lib.libmarsio_path); + printf("\033[1;31;40m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_clone_with_options", + g_engine_instance.config.lib.libmarsio_path); return -1; } @@ -191,37 +214,53 @@ static int pio_get_marsio_dll_function_entries(void) shared_marsio_dll_func_entries.ptr_marsio_send_burst_flush = (void (*)(struct mr_sendpath *, queue_id_t))dlsym(marsio_so_handle, "marsio_send_burst_flush"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_send_burst_flush) { - printf("\033[33m[Warning]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst_flush", g_engine_instance.config.lib.libmarsio_path); + printf("\033[33m[Warning]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_send_burst_flush", + g_engine_instance.config.lib.libmarsio_path); } /* for vlan flipping */ shared_marsio_dll_func_entries.ptr_marsio_buff_get_metadata = (int (*)(marsio_buff_t *, enum mr_buff_metadata_type, void *, unsigned int))dlsym(marsio_so_handle, "marsio_buff_get_metadata"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_get_metadata) { - printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_get_metadata", g_engine_instance.config.lib.libmarsio_path); - /* in order to be forward compatible with the previous version of mrzcpd, no error is returned here. vlan_flipping will become invalid */ + printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_get_metadata", + g_engine_instance.config.lib.libmarsio_path); + /* + in order to be forward compatible with the previous version of mrzcpd, no error is returned here. + vlan_flipping will become invalid + */ } shared_marsio_dll_func_entries.ptr_marsio_buff_set_metadata = (int (*)(marsio_buff_t *, enum mr_buff_metadata_type, void *, unsigned int))dlsym(marsio_so_handle, "marsio_buff_set_metadata"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_get_metadata) { - printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_set_metadata", g_engine_instance.config.lib.libmarsio_path); - /* in order to be forward compatible with the previous version of mrzcpd, no error is returned here. vlan_flipping will become invalid */ + printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_set_metadata", + g_engine_instance.config.lib.libmarsio_path); + /* + in order to be forward compatible with the previous version of mrzcpd, no error is returned here. + vlan_flipping will become invalid + */ } shared_marsio_dll_func_entries.ptr_marsio_buff_unset_metadata = (int (*)(marsio_buff_t *, enum mr_buff_metadata_type))dlsym(marsio_so_handle, "marsio_buff_unset_metadata"); if (nullptr == shared_marsio_dll_func_entries.ptr_marsio_buff_unset_metadata) { - printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_unset_metadata", g_engine_instance.config.lib.libmarsio_path); - /* in order to be forward compatible with the previous version of mrzcpd, no error is returned here. vlan_flipping will become invalid */ + printf("\033[33m[Error]dlsym function '%s' from '%s' failed!\033[0m\n", "marsio_buff_unset_metadata", + g_engine_instance.config.lib.libmarsio_path); + /* + in order to be forward compatible with the previous version of mrzcpd, no error is returned here. + vlan_flipping will become invalid + */ } return 0; } -int pio_marsio_device_open(const void *init_data) +int pio_marsio_device_open(struct packet_io_device *pdev, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq) { - + struct mr_instance *mr_inst_handle = pdev->ppio_inst->entity.marsio_inst->mr_inst; + struct mr_vdev *mr_dev_handle = \ + shared_marsio_dll_func_entries.ptr_marsio_open_device(mr_inst_handle, dev_name, nr_rxq, nr_txq); + return 0; } @@ -252,21 +291,21 @@ static int marsio_instance_init(struct packet_io_instance *pinst, int worker_thr return -1; } - pinst->entity.ppio_marsio_inst->ptr_mr_inst = shared_marsio_dll_func_entries.ptr_marsio_create(); - if (nullptr == pinst->entity.ppio_marsio_inst->ptr_mr_inst) { + pinst->entity.marsio_inst->mr_inst = shared_marsio_dll_func_entries.ptr_marsio_create(); + if (nullptr == pinst->entity.marsio_inst->mr_inst) { fprintf(stderr,"%s\n","marsio_create error!\n"); return -1; } /* TODO: MARSIO_OPT_THREAD_NUM */ - shared_marsio_dll_func_entries.ptr_marsio_option_set(pinst->entity.ppio_marsio_inst->ptr_mr_inst, + shared_marsio_dll_func_entries.ptr_marsio_option_set(pinst->entity.marsio_inst->mr_inst, MARSIO_OPT_THREAD_NUM, &worker_thread_num, sizeof(int)); /* TODO: MARSIO_OPT_THREAD_MASK_IN_CPUSET */ /* marsio_init */ - ret = shared_marsio_dll_func_entries.ptr_marsio_init(pinst->entity.ppio_marsio_inst->ptr_mr_inst, pinst->inst_name); + ret = shared_marsio_dll_func_entries.ptr_marsio_init(pinst->entity.marsio_inst->mr_inst, pinst->inst_name); if (ret < 0) { fprintf(stderr,"%s\n","marsio_init error!\n"); return -1; diff --git a/src/packet_io/marsio_mode/pio_marsio.h b/src/packet_io/marsio_mode/pio_marsio.h index 290033e..f9b4847 100644 --- a/src/packet_io/marsio_mode/pio_marsio.h +++ b/src/packet_io/marsio_mode/pio_marsio.h @@ -103,19 +103,30 @@ struct marsio_dll_function_entries { }; struct pio_marsio_instance { - struct mr_instance *ptr_mr_inst; + struct mr_instance *mr_inst; }; struct pio_marsio_device { - struct mr_vdev *vdev; + struct mr_vdev *mr_dev; }; struct pio_marsio_device_context { }; -int pio_marsio_device_open(const void *init_data); +/* + @brief open marsio device + @param pdev: the marsio device's pointer + @param dev_name: device name, such as eth1, eth2 ... + @param nr_rxq: number of the packet receiving queues for the device + @param nr_txq: number of the packet sending queues for the device +*/ +int pio_marsio_device_open(struct packet_io_device *pdev, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq); + +/* + @brief close pcap_live device +*/ int pio_marsio_device_close(const void *init_data); int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet *p[], int nr_p); diff --git a/src/packet_io/packet_io.cpp b/src/packet_io/packet_io.cpp index e718cf2..6129cbe 100644 --- a/src/packet_io/packet_io.cpp +++ b/src/packet_io/packet_io.cpp @@ -58,7 +58,9 @@ struct pio_instance_operations pio_instance_ops_array[PACKET_IO_RUN_MODE_MAX] = } }; -struct packet_io_instance *packet_io_instance_create(const char *instance_name, enum packet_io_run_mode mode, int worker_thread_num) { +struct packet_io_instance * +packet_io_instance_create(const char *instance_name, const enum packet_io_run_mode mode, const int worker_thread_num) +{ 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."); @@ -79,15 +81,16 @@ struct packet_io_instance *packet_io_instance_create(const char *instance_name, return pio_instance; } -void packet_io_instance_destroy(struct packet_io_instance *ppio_inst) { - if (nullptr != ppio_inst) { - if (nullptr != ppio_inst->device_handle) { +void packet_io_instance_destroy(struct packet_io_instance *pinst) { + if (nullptr != pinst) { + if (nullptr != pinst->device_handle) { } } } -struct packet_io_device *packet_io_open_device(struct packet_io_instance *ppio_inst, void *dev_ctx, uint32_t nr_rxqueue, uint32_t nr_txqueue) +struct packet_io_device * +packet_io_open_device(struct packet_io_instance *pinst, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq) { struct packet_io_device *ppio_dev = CALLOC(struct packet_io_device, 1); if (nullptr == ppio_dev) { @@ -97,38 +100,45 @@ struct packet_io_device *packet_io_open_device(struct packet_io_instance *ppio_i memset(ppio_dev, 0, sizeof(*ppio_dev)); - ppio_dev->dev_ctx = dev_ctx; - ppio_dev->ppio_inst = ppio_inst; - ppio_dev->dev_ops = &pio_device_ops_array[ppio_inst->mode]; + strncpy(ppio_dev->dev_name, dev_name, strlen(dev_name)); + // TODO: ppio_dev->dev_ctx = dev_ctx; + ppio_dev->ppio_inst = pinst; + ppio_dev->dev_ops = &pio_device_ops_array[pinst->mode]; - int ret = ppio_dev->dev_ops->open(dev_ctx); + /* + ppio_inst->device_handle --> | struct packet_io_device * | struct packet_io_device * | struct packet_io_device * | + array[0] array[1] array[2] + */ + pinst->device_handle[pinst->dev_cnt++] = ppio_dev; + + int ret = ppio_dev->dev_ops->open(ppio_dev, dev_name, nr_rxq, nr_txq); if (ret < 0) { log_error(ST_ERR_PIO_DEVICE, "packet_io device open failed."); FREE(ppio_dev); return nullptr; } - + return ppio_dev; } -void packet_io_close_device(struct packet_io_device *ppio_dev) +void packet_io_close_device(struct packet_io_device *pdev) { - if (nullptr == ppio_dev || nullptr == ppio_dev->dev_ops) { + if (nullptr == pdev || nullptr == pdev->dev_ops) { return; } - int ret = ppio_dev->dev_ops->close(nullptr); + int ret = pdev->dev_ops->close(nullptr); if (ret < 0) { log_error(ST_ERR_PIO_DEVICE, "packet_io device close failed."); } } -int packet_io_rx(struct packet_io_device *ppio_dev, uint32_t rxq_id, struct packet *p[], int nr_p) +int packet_io_rx(struct packet_io_device *pdev, uint32_t rxq_id, struct packet *p[], int nr_p) { - return ppio_dev->dev_ops->recv(ppio_dev, rxq_id, p, nr_p); + return pdev->dev_ops->recv(pdev, rxq_id, p, nr_p); } -int packet_io_tx(struct packet_io_device *ppio_dev, uint32_t txq_id, struct packet *p[], int nr_p) +int packet_io_tx(struct packet_io_device *pdev, uint32_t txq_id, struct packet *p[], int nr_p) { - return ppio_dev->dev_ops->send(ppio_dev, txq_id, p, nr_p); + return pdev->dev_ops->send(pdev, txq_id, p, nr_p); } \ No newline at end of file diff --git a/src/packet_io/packet_io.h b/src/packet_io/packet_io.h index da9c0d7..7f5b98b 100644 --- a/src/packet_io/packet_io.h +++ b/src/packet_io/packet_io.h @@ -19,8 +19,6 @@ #include "./pcap_file_mode/pio_pcap_file.h" #include "./marsio_mode/pio_marsio.h" -#define DEV_MAX_CNT 16 - struct pio_instance_operations { int (*create)(struct packet_io_instance *instance, int worker_thread_num); @@ -35,9 +33,9 @@ struct packet_io_instance { enum packet_io_run_mode mode; /* device handle set in this instance */ - struct packet_io_device **device_handle; + struct packet_io_device *device_handle[DEV_MAX_CNT]; - /* device count */ + /* device's exactly count */ uint32_t dev_cnt; /* instance operations */ @@ -45,14 +43,14 @@ struct packet_io_instance { union { - struct pio_pcap_file_instance *ppio_pcap_file_inst; - struct pio_pcap_live_instance *ppio_pcap_live_inst; - struct pio_marsio_instance *ppio_marsio_inst; + struct pio_pcap_file_instance *pcap_file_inst; + struct pio_pcap_live_instance *pcap_live_inst; + struct pio_marsio_instance *marsio_inst; } entity; }; struct pio_device_operations { - int (*open)(const void *init_data); + int (*open)(struct packet_io_device *pinst, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq); int (*close)(const void *init_data); @@ -69,22 +67,26 @@ struct packet_io_device { struct pio_device_operations *dev_ops; union { - struct pio_pcap_file_device *ppio_pcap_file_dev; - struct pio_pcap_live_device *ppio_pcap_live_dev; - struct pio_marsio_device *ppio_marsio_dev; + struct pio_pcap_file_device *pcap_file_dev; + struct pio_pcap_live_device *pcap_live_dev; + struct pio_marsio_device *marsio_dev; } entity; /* packet io device context */ void *dev_ctx; - /* the packet_io's instance of the device */ + /* packet_io instance which the device belongs to */ struct packet_io_instance *ppio_inst; }; -struct packet_io_instance *packet_io_instance_create(const char *instance_name, enum packet_io_run_mode mode, int worker_thread_num); -void packet_io_instance_destroy(struct packet_io_instance *ppio_inst); +struct packet_io_instance * +packet_io_instance_create(const char *instance_name, const enum packet_io_run_mode mode, const int worker_thread_num); + +void packet_io_instance_destroy(struct packet_io_instance *pinst); + +struct packet_io_device * +packet_io_open_device(struct packet_io_instance *instance, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq); -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); diff --git a/src/packet_io/pcap_file_mode/pio_pcap_file.cpp b/src/packet_io/pcap_file_mode/pio_pcap_file.cpp index 0c67703..2a6e8db 100644 --- a/src/packet_io/pcap_file_mode/pio_pcap_file.cpp +++ b/src/packet_io/pcap_file_mode/pio_pcap_file.cpp @@ -12,7 +12,7 @@ #include "pio_pcap_file.h" -int pio_pcap_file_device_open(const void *init_data) { +int pio_pcap_file_device_open(struct packet_io_device *pdev, const char *path, uint32_t nr_rxq, uint32_t nr_txq) { return 0; } diff --git a/src/packet_io/pcap_file_mode/pio_pcap_file.h b/src/packet_io/pcap_file_mode/pio_pcap_file.h index a71f531..11b3467 100644 --- a/src/packet_io/pcap_file_mode/pio_pcap_file.h +++ b/src/packet_io/pcap_file_mode/pio_pcap_file.h @@ -26,8 +26,19 @@ struct pio_pcap_file_device_context { }; -int pio_pcap_file_device_open(const void *init_data); +/* + @brief open pcap_file device + @param pdev: pcap_file device's pointer + @param dev_name: the path of pcap file + @param nr_rxq: number of the packet receiving queues for the device + @param nr_txq: number of the packet sending queues for the device +*/ +int pio_pcap_file_device_open(struct packet_io_device *pdev, const char *path, uint32_t nr_rxq, uint32_t nr_txq); + +/* + @brief close pcap_live device +*/ int pio_pcap_file_device_close(const void *init_data); int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet *p[], int nr_p); diff --git a/src/packet_io/pcap_live_mode/pio_pcap_live.cpp b/src/packet_io/pcap_live_mode/pio_pcap_live.cpp index b6cb0bb..d78e029 100644 --- a/src/packet_io/pcap_live_mode/pio_pcap_live.cpp +++ b/src/packet_io/pcap_live_mode/pio_pcap_live.cpp @@ -10,7 +10,7 @@ #include "pio_pcap_live.h" -int pio_pcap_live_device_open(const void *init_data) { +int pio_pcap_live_device_open(struct packet_io_device *pdev, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq) { return 0; } diff --git a/src/packet_io/pcap_live_mode/pio_pcap_live.h b/src/packet_io/pcap_live_mode/pio_pcap_live.h index 0f9cd6d..e07bc3d 100644 --- a/src/packet_io/pcap_live_mode/pio_pcap_live.h +++ b/src/packet_io/pcap_live_mode/pio_pcap_live.h @@ -24,8 +24,19 @@ struct pio_pcap_live_device_context { }; -int pio_pcap_live_device_open(const void *init_data); +/* + @brief open pcap_live device + @param pdev: pcap_live device's pointer + @param dev_name: device name, such as eth1, eth2 ... + @param nr_rxq: number of the packet receiving queues for the device + @param nr_txq: number of the packet sending queues for the device +*/ +int pio_pcap_live_device_open(struct packet_io_device *pdev, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq); + +/* + @brief close pcap_live device +*/ int pio_pcap_live_device_close(const void *init_data); int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct packet *p[], int nr_p); diff --git a/src/packet_io/test/gtest_packet_io.cpp b/src/packet_io/test/gtest_packet_io.cpp index bc068b8..6efcb71 100644 --- a/src/packet_io/test/gtest_packet_io.cpp +++ b/src/packet_io/test/gtest_packet_io.cpp @@ -4,7 +4,7 @@ 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); + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE, 2); EXPECT_EQ(packet_io_open_device(ppio_inst, nullptr, 1, 1), nullptr); }