diff --git a/CMakeLists.txt b/CMakeLists.txt index f11277d..2549c33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,10 @@ add_definitions(-D_GNU_SOURCE) set(CMAKE_CXX_STANDARD 11) set(CMAKE_C_STANDARD 11) +SET(CMAKE_BUILD_TYPE "Debug") +SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") +SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") + if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RelWithDebInfo) endif() diff --git a/sdk/include/packet.h b/sdk/include/packet.h index 8ffe7c2..3b64d74 100644 --- a/sdk/include/packet.h +++ b/sdk/include/packet.h @@ -1,3 +1,16 @@ #pragma once -struct stellar_packet; \ No newline at end of file +#include + +struct stellar_packet; + +/** + * @brief get stellar_packet's ctrlzone + * @note ctrlzone's memory is 64 bytes, do not exceed it + */ +char *get_stellar_packet_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len); + +/** + * @brief get stellar_packet's data pointer + */ +char *get_stellar_packet_data(struct stellar_packet *p, size_t *data_len); \ No newline at end of file diff --git a/sdk/include/util_errors.h b/sdk/include/util_errors.h index 1426a06..53997fa 100644 --- a/sdk/include/util_errors.h +++ b/sdk/include/util_errors.h @@ -39,6 +39,7 @@ typedef enum { ST_ERR_PCAP_SET_PROMISC, ST_ERR_PCAP_SET_TIMEOUT, ST_ERR_PCAP_ACTIVATE_HANDLE, + ST_ERR_PIO_CONFIG, ST_ERR_FOPEN, ST_ERR_BPF, ST_ERR_MAX diff --git a/src/app.toml b/src/app.toml index 83a8f4b..83a1eb4 100644 --- a/src/app.toml +++ b/src/app.toml @@ -1,19 +1,24 @@ -[THREAD] - -thread_num = 1 - [PACKET_IO] -""" -example1: -run_mode = pcap_live -interface = [eth0, eth1] +# example1: +# RUN_MODE="PCAP_LIVE_MODE" +# WORKER_THREAD_NUM=10 # Prompt marsio how many threads to start to receive packets +# INTERFACE=["eth0", "eth1"] +# SNAP_LEN=65535 # default 65535 +# PROMISC=1 # 0(disable) 1(enable), if enable nic promisc mode, default 0(disable) +# BPF_FILTER="port 80 and udp" # default null -example2: -run_mode = marsio -work_thread_num = 10 # Prompt marsio how many threads to start to receive packets -interface = [eth0] +# example2: +# RUN_MODE="MARSIO_MODE" +# WORKER_THREAD_NUM=10 # Prompt marsio how many threads to start to receive packets +# INTERFACE=["eth0", "eth1"] -example3: -run_mode = pcap_file -pcap_file_path = /tmp/pcapfile/001.pcap # if single file, specify dir+filename; if pcapfile directory, specify dir -""" \ No newline at end of file +# example3: +# RUN_MODE="PCAP_FILE_MODE" +# WORKER_THREAD_NUM=10 # Prompt marsio how many threads to start to receive packets +# PCAP_FILE_PATH="/tmp/pcapfile/001.pcap" # if single file, specify dir+filename; if pcapfile directory, specify dir +# DELETE_WHEN_DONE=0 # 0(false) 1(true), default 0, if delete it when the pcapfile is processed +# BPF_FILTER="port 80 and udp" # default null + +RUN_MODE="PCAP_LIVE_MODE" +WORKER_THREAD_NUM=3 # Prompt marsio how many threads to start to receive packets +INTERFACE=["eth0"] \ No newline at end of file diff --git a/src/common/global_var.cpp b/src/common/global_var.cpp deleted file mode 100644 index 4a63b76..0000000 --- a/src/common/global_var.cpp +++ /dev/null @@ -1,13 +0,0 @@ -/* -********************************************************************************************** -* File: global_var.cpp -* Description: -* Authors: Liu WenTan -* Date: 2022-07-15 -* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved. -*********************************************************************************************** -*/ - -#include "global_var.h" - -struct stellar_engine g_engine_instance; \ No newline at end of file diff --git a/src/common/global_var.h b/src/common/global_var.h deleted file mode 100644 index 5484363..0000000 --- a/src/common/global_var.h +++ /dev/null @@ -1,94 +0,0 @@ -/* -********************************************************************************************** -* File: global_var.h -* Description: global variable and data structure -* Authors: Liu WenTan -* Date: 2022-07-15 -* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved. -*********************************************************************************************** -*/ - -#ifndef _GLOBAL_VAR_H_ -#define _GLOBAL_VAR_H_ - -#ifdef __cpluscplus -extern "C" -{ -#endif - -#include -#include -#include - -#define DEV_MAX_CNT 64 -#define STR_MAX_LEN 1024 - -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 cpu_config {}; -struct mem_config {}; - -struct system_config { - char instance_name[NAME_MAX]; -}; - -/* store packet_io config */ -struct packet_io_config { - /* packet_io run mode */ - enum packet_io_run_mode mode; - - /* worker thread num */ - uint32_t thread_num; - - /* device name list */ - char dev_name[DEV_MAX_CNT][NAME_MAX]; - - /* device counts */ - uint32_t dev_cnt; - - /* bpf filter string, such as "tcp and port 25"*/ - char bpf_string[STR_MAX_LEN]; - - /* delete after the pcap file is read */ - bool should_delete; - - time_t delay; - - /* snapshot length */ - int snaplen; - - /* promiscuous value */ - int promisc; - - /* marsio ctrlzone id */ - int mr_ctrlzone_id; -}; - -struct lib_config { - const char *libmarsio_path; -}; - -struct stellar_config { - struct cpu_config cpu; - struct mem_config mem; - struct system_config system; - struct packet_io_config packet_io; - struct lib_config lib; -}; - -struct stellar_engine { - struct stellar_config config; -}; - -extern struct stellar_engine g_engine_instance; - -#ifdef __cpluscplus -} -#endif - -#endif /* _GLOBAL_VAR_H_ */ \ No newline at end of file diff --git a/src/common/time_helper.cpp b/src/common/time_helper.cpp index 2545ba7..2eb2399 100644 --- a/src/common/time_helper.cpp +++ b/src/common/time_helper.cpp @@ -21,7 +21,7 @@ void get_current_timespec(struct timespec *tm) } } -int compare_timespec(struct timespec *left, struct timespec *right) +ssize_t compare_timespec(struct timespec *left, struct timespec *right) { if (left->tv_sec < right->tv_sec) { return -1; diff --git a/src/common/time_helper.h b/src/common/time_helper.h index 55e7e60..fd1073a 100644 --- a/src/common/time_helper.h +++ b/src/common/time_helper.h @@ -18,10 +18,11 @@ extern "C" #include #include +#include void get_current_timespec(struct timespec *tm); -int compare_timespec(struct timespec *left, struct timespec *right); +ssize_t compare_timespec(struct timespec *left, struct timespec *right); void copy_timespec(struct timespec *from, struct timespec *to); diff --git a/src/main.cpp b/src/main.cpp index 4b56e52..615e046 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,8 @@ *********************************************************************************************** */ +#include +#include #include #include #include @@ -15,13 +17,13 @@ #include #include -#include "global_var.h" #include "logger.h" #include "packet_io.h" #include "packet_io_util.h" #include "session_manager.h" #include "plugin_manager.h" #include "http.h" +#include "utils.h" #include "util_errors.h" struct worker_thread_ctx @@ -59,6 +61,7 @@ void *worker_thread_cycle(void *arg) // dispatch to trigger polling event } #endif + } } return nullptr; } diff --git a/src/packet_io/CMakeLists.txt b/src/packet_io/CMakeLists.txt index d84f4ac..bc0f786 100644 --- a/src/packet_io/CMakeLists.txt +++ b/src/packet_io/CMakeLists.txt @@ -1,9 +1,8 @@ add_library(packet_io - ../common/global_var.cpp ../common/time_helper.cpp - packet_io.cpp packet_io_util.cpp + packet_io_internal.cpp pcap_live_mode/pio_pcap_live.cpp pcap_file_mode/pio_pcap_file.cpp marsio_mode/pio_marsio.cpp diff --git a/src/packet_io/marsio_mode/pio_marsio.cpp b/src/packet_io/marsio_mode/pio_marsio.cpp index 8a95266..a867ab2 100644 --- a/src/packet_io/marsio_mode/pio_marsio.cpp +++ b/src/packet_io/marsio_mode/pio_marsio.cpp @@ -11,12 +11,12 @@ #include #include -#include "global_var.h" #include "logger.h" #include "utils.h" #include "util_errors.h" #include "pio_marsio.h" #include "packet_io.h" +#include "packet_io_internal.h" #define MARSIO_BURST_PKT_MAX (256) @@ -28,14 +28,14 @@ static void fake_marsio_buff_set_rehash_index(marsio_buff_t *m, uint32_t hash) return; } -static int pio_get_marsio_dll_function_entries(void) +static ssize_t pio_get_marsio_dll_function_entries(void) { - void *marsio_so_handle = dlopen(g_engine_instance.config.lib.libmarsio_path, + void *marsio_so_handle = dlopen(g_packet_io_config.marsio.libmarsio_path, RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE); if (nullptr == marsio_so_handle) { printf("\033[1;31;40m[Error]dlopen '%s' failed, %s\033[0m\n", - g_engine_instance.config.lib.libmarsio_path, dlerror()); + g_packet_io_config.marsio.libmarsio_path, dlerror()); return -1; } @@ -43,7 +43,7 @@ static int pio_get_marsio_dll_function_entries(void) (struct mr_instance *(*)(void))dlsym(marsio_so_handle, "marsio_create"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -51,7 +51,7 @@ static int pio_get_marsio_dll_function_entries(void) (int (*)(struct mr_instance *, const char *))dlsym(marsio_so_handle, "marsio_init"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -59,7 +59,7 @@ static int pio_get_marsio_dll_function_entries(void) (int (*)(struct mr_instance *))dlsym(marsio_so_handle, "marsio_destory"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -68,7 +68,7 @@ static int pio_get_marsio_dll_function_entries(void) unsigned int, unsigned int))dlsym(marsio_so_handle, "marsio_open_device"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -76,7 +76,7 @@ static int pio_get_marsio_dll_function_entries(void) (void (*)(struct mr_vdev *))dlsym(marsio_so_handle, "marsio_close_device"); if (nullptr ==g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -84,7 +84,7 @@ static int pio_get_marsio_dll_function_entries(void) (int (*)(struct mr_instance *, marsio_opt_type_t, void *, size_t))dlsym(marsio_so_handle, "marsio_option_set"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -92,7 +92,7 @@ static int pio_get_marsio_dll_function_entries(void) (struct mr_sendpath *(*)(struct mr_vdev *))dlsym(marsio_so_handle, "marsio_sendpath_create_by_vdev"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -100,14 +100,14 @@ static int pio_get_marsio_dll_function_entries(void) (void (*)(struct mr_sendpath *))dlsym(marsio_so_handle, "marsio_sendpath_destory"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } g_marsio_dll_func.marsio_thread_init = (int (*)(struct mr_instance *))dlsym(marsio_so_handle, "marsio_thread_init"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -115,7 +115,7 @@ static int pio_get_marsio_dll_function_entries(void) (int (*)(struct mr_vdev *, queue_id_t, marsio_buff_t **, int))dlsym(marsio_so_handle, "marsio_recv_burst"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -123,7 +123,7 @@ static int pio_get_marsio_dll_function_entries(void) (int (*)(struct mr_sendpath *, queue_id_t, marsio_buff_t **, int))dlsym(marsio_so_handle, "marsio_send_burst"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -132,7 +132,7 @@ static int pio_get_marsio_dll_function_entries(void) marsio_buff_t **, int, uint16_t))dlsym(marsio_so_handle, "marsio_send_burst_with_options"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -141,7 +141,7 @@ static int pio_get_marsio_dll_function_entries(void) unsigned int, int, int))dlsym(marsio_so_handle, "marsio_buff_malloc_global"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -150,7 +150,7 @@ static int pio_get_marsio_dll_function_entries(void) unsigned int, int, int))dlsym(marsio_so_handle, "marsio_buff_free"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -158,7 +158,7 @@ static int pio_get_marsio_dll_function_entries(void) (char * (*)(marsio_buff_t *, uint16_t))dlsym(marsio_so_handle, "marsio_buff_append"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -166,7 +166,7 @@ static int pio_get_marsio_dll_function_entries(void) (void * (*)(marsio_buff_t *, uint8_t))dlsym(marsio_so_handle, "marsio_buff_ctrlzone"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -175,7 +175,7 @@ static int pio_get_marsio_dll_function_entries(void) void *, uint8_t))dlsym(marsio_so_handle, "marsio_buff_ctrlzone_set"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -183,28 +183,28 @@ static int pio_get_marsio_dll_function_entries(void) (void (*)(marsio_buff_t *, uint32_t))dlsym(marsio_so_handle, "marsio_buff_set_rehash_index"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); g_marsio_dll_func.marsio_buff_set_rehash_index = fake_marsio_buff_set_rehash_index; } g_marsio_dll_func.marsio_buff_mtod = (char * (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_mtod"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } g_marsio_dll_func.marsio_buff_datalen = (uint32_t (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_datalen"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } g_marsio_dll_func.marsio_buff_buflen = (uint32_t (*)(marsio_buff_t *))dlsym(marsio_so_handle, "marsio_buff_buflen"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -213,7 +213,7 @@ static int pio_get_marsio_dll_function_entries(void) int, int, uint16_t))dlsym(marsio_so_handle, "marsio_buff_clone_with_options"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); return -1; } @@ -222,7 +222,7 @@ static int pio_get_marsio_dll_function_entries(void) (void (*)(struct mr_sendpath *, queue_id_t))dlsym(marsio_so_handle, "marsio_send_burst_flush"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); } /* for vlan flipping */ @@ -231,7 +231,7 @@ static int pio_get_marsio_dll_function_entries(void) void *, unsigned int))dlsym(marsio_so_handle, "marsio_buff_get_metadata"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); /* in order to be forward compatible with the previous version of mrzcpd, no error is returned here. vlan_flipping will become invalid @@ -243,7 +243,7 @@ static int pio_get_marsio_dll_function_entries(void) void *, unsigned int))dlsym(marsio_so_handle, "marsio_buff_set_metadata"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); /* in order to be forward compatible with the previous version of mrzcpd, no error is returned here. vlan_flipping will become invalid @@ -254,7 +254,7 @@ static int pio_get_marsio_dll_function_entries(void) (int (*)(marsio_buff_t *, enum mr_buff_metadata_type))dlsym(marsio_so_handle, "marsio_buff_unset_metadata"); if (nullptr == g_marsio_dll_func.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); + g_packet_io_config.marsio.libmarsio_path); /* in order to be forward compatible with the previous version of mrzcpd, no error is returned here. vlan_flipping will become invalid @@ -264,7 +264,7 @@ static int pio_get_marsio_dll_function_entries(void) return 0; } -int pio_marsio_device_open(struct packet_io_device *pdev) +ssize_t pio_marsio_device_open(struct packet_io_device *pdev) { if (nullptr == pdev) { log_error(ST_ERR_PIO_MARSIO_DEVICE, "invalid packet_io_device pointer."); @@ -282,7 +282,7 @@ int pio_marsio_device_open(struct packet_io_device *pdev) return -1; } - pdev->entity.marsio_dev_ctx->pio_dev = pdev; + pdev->entity.marsio_dev_ctx->pdev = pdev; struct mr_instance *mr_inst_handle = pdev->ppio_inst->entity.marsio_inst_ctx->mr_inst_handle; /* marsio_open_device() return marsio device handle*/ @@ -304,7 +304,7 @@ int pio_marsio_device_open(struct packet_io_device *pdev) return 0; } -int pio_marsio_device_close(struct packet_io_device *pdev) +ssize_t pio_marsio_device_close(struct packet_io_device *pdev) { if (nullptr == pdev) { log_error(ST_ERR_PIO_MARSIO_DEVICE, "invalid pdev pointer so close marsio device failed!"); @@ -319,12 +319,12 @@ int pio_marsio_device_close(struct packet_io_device *pdev) return 0; } -int pio_marsio_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts) +ssize_t pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts) { struct mr_vdev *mr_dev_handle = pdev->entity.marsio_dev_ctx->mr_dev_handle; marsio_buff_t *rx_buff[MARSIO_BURST_PKT_MAX]; - int recv_res = g_marsio_dll_func.marsio_recv_burst(mr_dev_handle, rxq_id, rx_buff, nr_pkts); + ssize_t recv_res = g_marsio_dll_func.marsio_recv_burst(mr_dev_handle, rxq_id, rx_buff, nr_pkts); /* receive some pkts, copy mbuf pointer to packet structure */ if (recv_res > 0) { for (int i = 0; i < recv_res; i++) { @@ -335,10 +335,10 @@ int pio_marsio_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, st return recv_res; } -int pio_marsio_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts) +ssize_t pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, size_t nr_pkts) { struct mr_sendpath *sendpath_handle = pdev->entity.marsio_dev_ctx->mr_sendpath_handle; - int ret = g_marsio_dll_func.marsio_send_burst(sendpath_handle, txq_id, (marsio_buff_t **)pkts, nr_pkts); + ssize_t ret = g_marsio_dll_func.marsio_send_burst(sendpath_handle, txq_id, (marsio_buff_t **)pkts, nr_pkts); if (ret < 0) { g_marsio_dll_func.marsio_buff_free(pdev->ppio_inst->entity.marsio_inst_ctx->mr_inst_handle, (marsio_buff_t **)pkts, nr_pkts, MARSIO_SOCKET_ID_ANY, txq_id); @@ -347,15 +347,15 @@ int pio_marsio_device_send(struct packet_io_device *pdev, uint16_t txq_id, struc return ret; } -void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts) +void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts) { struct mr_instance *mr_inst = pdev->ppio_inst->entity.marsio_inst_ctx->mr_inst_handle; g_marsio_dll_func.marsio_buff_free(mr_inst, (marsio_buff_t **)pkts, nr_pkts, MARSIO_SOCKET_ID_ANY, qid); } -static int marsio_instance_init(struct packet_io_instance *pinst) +static ssize_t marsio_instance_init(struct packet_io_instance *pinst) { - int ret = -1; + ssize_t ret = -1; ret = pio_get_marsio_dll_function_entries(); if (ret < 0) { printf("\033[1;31;40m[Error]dlopen marsio.so symbol failed!\033[0m\n"); @@ -368,7 +368,7 @@ static int marsio_instance_init(struct packet_io_instance *pinst) return -1; } - int wrk_thread_num = g_engine_instance.config.packet_io.thread_num; + int wrk_thread_num = g_packet_io_config.common.thread_num; /* TODO: MARSIO_OPT_THREAD_NUM */ ret = g_marsio_dll_func.marsio_option_set(pinst->entity.marsio_inst_ctx->mr_inst_handle, MARSIO_OPT_THREAD_NUM, @@ -390,7 +390,7 @@ static int marsio_instance_init(struct packet_io_instance *pinst) return 0; } -int pio_marsio_instance_create(struct packet_io_instance *pinst) +ssize_t pio_marsio_instance_create(struct packet_io_instance *pinst) { if (nullptr == pinst) { log_error(ST_ERR_PIO_MARSIO_INSTANCE, "invalid marsio instance pointer."); @@ -404,7 +404,7 @@ int pio_marsio_instance_create(struct packet_io_instance *pinst) } /* instance init */ - int ret = marsio_instance_init(pinst); + ssize_t ret = marsio_instance_init(pinst); if (ret < 0) { log_error(ST_ERR_PIO_MARSIO_INSTANCE, "marsio instance init failed."); return -1; @@ -418,30 +418,24 @@ void pio_marsio_instance_destroy(struct packet_io_instance *pinst) g_marsio_dll_func.marsio_destroy(pinst->entity.marsio_inst_ctx->mr_inst_handle); FREE(pinst->entity.marsio_inst_ctx); - for (uint32_t i = 0; i < pinst->dev_cnt; i++) { - pio_marsio_device_close(pinst->devices[i]); - FREE(pinst->devices[i]); + struct packet_io_device *node = nullptr; + while ((node = TAILQ_FIRST(&pinst->device_queue_head)) != nullptr) { + TAILQ_REMOVE(&pinst->device_queue_head, node, next); + pinst->dev_cnt--; + pio_marsio_device_close(node); + FREE(node); } } -void *pio_marsio_device_buff_ctrlzone(struct stellar_packet *p) +char *pio_marsio_device_buff_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len) { int zone_id = 0; - - return g_marsio_dll_func.marsio_buff_ctrlzone((marsio_buff_t *)p, zone_id); + *ctrlzone_len = (size_t)g_marsio_dll_func.marsio_buff_buflen((marsio_buff_t *)p); + return (char *)g_marsio_dll_func.marsio_buff_ctrlzone((marsio_buff_t *)p, zone_id); } -char *pio_marsio_device_buff_mtod(struct stellar_packet *p) +char *pio_marsio_device_buff_mtod(struct stellar_packet *p, size_t *data_len) { + *data_len = (size_t)g_marsio_dll_func.marsio_buff_datalen((marsio_buff_t *)p); return g_marsio_dll_func.marsio_buff_mtod((marsio_buff_t *)p); -} - -uint32_t pio_marsio_device_buff_buflen(struct stellar_packet *p) -{ - return g_marsio_dll_func.marsio_buff_buflen((marsio_buff_t *)p); -} - -uint32_t pio_marsio_device_buff_datalen(struct stellar_packet *p) -{ - return g_marsio_dll_func.marsio_buff_datalen((marsio_buff_t *)p); -} +} \ No newline at end of file diff --git a/src/packet_io/marsio_mode/pio_marsio.h b/src/packet_io/marsio_mode/pio_marsio.h index 0e4814a..cf640d6 100644 --- a/src/packet_io/marsio_mode/pio_marsio.h +++ b/src/packet_io/marsio_mode/pio_marsio.h @@ -98,7 +98,7 @@ struct pio_marsio_device_context { struct mr_vdev *mr_dev_handle; struct mr_sendpath * mr_sendpath_handle; - struct packet_io_device *pio_dev; + struct packet_io_device *pdev; }; /** @@ -107,7 +107,7 @@ struct pio_marsio_device_context { * @param pinst * @return int */ -int pio_marsio_instance_create(struct packet_io_instance *pinst); +ssize_t pio_marsio_instance_create(struct packet_io_instance *pinst); /** * @brief @@ -124,12 +124,12 @@ void pio_marsio_instance_destroy(struct packet_io_instance *pinst); * pdev->rxq_num: number of the packet receiving queues for the device * pdev->txq_num: number of the packet sending queues for the device */ -int pio_marsio_device_open(struct packet_io_device *pdev); +ssize_t pio_marsio_device_open(struct packet_io_device *pdev); /** * @brief close pcap_live device */ -int pio_marsio_device_close(struct packet_io_device *pdev); +ssize_t pio_marsio_device_close(struct packet_io_device *pdev); /** * @brief receive packets from device's single rx queue which specified by rxq_id @@ -141,7 +141,7 @@ int pio_marsio_device_close(struct packet_io_device *pdev); * * @retval number of packets actually received */ -int pio_marsio_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts); +ssize_t pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts); /** * @brief send packets by device's single tx queue which specified by txq_id @@ -153,7 +153,7 @@ int pio_marsio_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, st * * @retval if ret<0, means the sending fails; if ret==0 means the sending succeeds */ -int pio_marsio_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts); +ssize_t pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, size_t nr_pkts); /** * @brief manually free packet's memory @@ -163,15 +163,11 @@ int pio_marsio_device_send(struct packet_io_device *pdev, uint16_t txq_id, struc * @param pkts: * @param nr_pkts: */ -void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts); +void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts); -void *pio_marsio_device_buff_ctrlzone(struct stellar_packet *p); +char *pio_marsio_device_buff_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len); -char *pio_marsio_device_buff_mtod(struct stellar_packet *p); - -uint32_t pio_marsio_device_buff_buflen(struct stellar_packet *p); - -uint32_t pio_marsio_device_buff_datalen(struct stellar_packet *p); +char *pio_marsio_device_buff_mtod(struct stellar_packet *p, size_t *data_len); #ifdef __cpluscplus } diff --git a/src/packet_io/packet_io.cpp b/src/packet_io/packet_io.cpp deleted file mode 100644 index 957034b..0000000 --- a/src/packet_io/packet_io.cpp +++ /dev/null @@ -1,203 +0,0 @@ -/* -********************************************************************************************** -* File: packet_io.cpp -* Description: -* Authors: Liu WenTan -* Date: 2022-07-15 -* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved. -*********************************************************************************************** -*/ - -#include - -#include "logger.h" -#include "utils.h" -#include "util_errors.h" -#include "packet_io.h" - -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 = nullptr, - .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, - }, - - { - .open = pio_pcap_live_device_open, - .close = pio_pcap_live_device_close, - .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, - }, - - { - .open = pio_marsio_device_open, - .close = pio_marsio_device_close, - .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, - } -}; - -struct pio_instance_operations pio_instance_ops_array[PACKET_IO_RUN_MODE_MAX] = -{ - { - .create = pio_pcap_file_instance_create, - .destroy = pio_pcap_file_instance_destroy, - }, - - { - .create = pio_pcap_live_instance_create, - .destroy = pio_pcap_live_instance_destroy, - }, - - { - .create = pio_marsio_instance_create, - .destroy = pio_marsio_instance_destroy, - } -}; - -struct packet_io_instance * -packet_io_instance_create(const char *inst_name, const enum packet_io_run_mode mode) -{ - if (nullptr == inst_name || mode < PACKET_IO_RUN_MODE_PCAP_FILE || mode >= PACKET_IO_RUN_MODE_MAX) { - 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."); - return nullptr; - } - - int ret = strncpy_safe(pio_instance->inst_name, inst_name, sizeof(pio_instance->inst_name)); - if (ret < 0) { - log_error(ST_ERR_STR_COPY, "packet_io instance name copy failed."); - return nullptr; - } - - pio_instance->mode = mode; - pio_instance->inst_ops = &pio_instance_ops_array[mode]; - - ret = pio_instance->inst_ops->create(pio_instance); - if (ret < 0) { - log_error(ST_ERR_PIO_INSTANCE, "packet_io instance create failed."); - return nullptr; - } - - return pio_instance; -} - -void packet_io_instance_destroy(struct packet_io_instance *pinst) { - if (nullptr == pinst) { - return; - } - - pinst->inst_ops->destroy(pinst); - FREE(pinst); -} - -struct packet_io_device * -packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, uint16_t nr_rxq, uint16_t nr_txq) -{ - 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; - } - - int ret = strncpy_safe(ppio_dev->dev_name, dev_name, sizeof(ppio_dev->dev_name)); - if (ret < 0) { - log_error(ST_ERR_STR_COPY, "packet_io device name copy failed."); - return nullptr; - } - - ppio_dev->rxq_num = nr_rxq; - ppio_dev->txq_num = nr_txq; - ppio_dev->ppio_inst = pinst; - ppio_dev->dev_ops = &pio_device_ops_array[pinst->mode]; - - /* - * ppio_inst->devices --> | struct packet_io_device * | struct packet_io_device * | struct packet_io_device * | - array[0] array[1] array[2] - **/ - pinst->devices[pinst->dev_cnt++] = ppio_dev; - - ret = ppio_dev->dev_ops->open(ppio_dev); - 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_device_close(struct packet_io_device *pdev) -{ - if (nullptr == pdev) { - return; - } - - if (nullptr == pdev->dev_ops) { - FREE(pdev); - return; - } - - int ret = pdev->dev_ops->close(pdev); - if (ret < 0) { - log_error(ST_ERR_PIO_DEVICE, "packet_io device close failed."); - } - - FREE(pdev); -} - -int packet_io_device_rx(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts) -{ - return pdev->dev_ops->recv(pdev, rxq_id, pkts, nr_pkts); -} - -int packet_io_device_tx(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts) -{ - return pdev->dev_ops->send(pdev, txq_id, pkts, nr_pkts); -} - -void packet_io_pkts_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts) -{ - return pdev->dev_ops->pkt_free(pdev, qid, pkts, nr_pkts); -} - -void *packet_io_buff_ctrlzone(struct packet_io_device *pdev, struct stellar_packet *p) -{ - return pdev->dev_ops->buff_ctrlzone(p); -} - -char *packet_io_buff_mtod(struct packet_io_device *pdev, struct stellar_packet *p) -{ - return pdev->dev_ops->buff_mtod(p); -} - -uint32_t packet_io_buff_buflen(struct packet_io_device *pdev, struct stellar_packet *p) -{ - return pdev->dev_ops->buff_buflen(p); -} - -uint32_t packet_io_buff_datalen(struct packet_io_device *pdev, struct stellar_packet *p) -{ - return pdev->dev_ops->buff_datalen(p); -} diff --git a/src/packet_io/packet_io.h b/src/packet_io/packet_io.h index fc55082..868bb15 100644 --- a/src/packet_io/packet_io.h +++ b/src/packet_io/packet_io.h @@ -17,119 +17,50 @@ extern "C" #endif #include -#include -#include +#include +#include -#include "global_var.h" -#include "./pcap_live_mode/pio_pcap_live.h" -#include "./pcap_file_mode/pio_pcap_file.h" -#include "./marsio_mode/pio_marsio.h" +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; +struct packet_io_device; /** - * 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 - */ - -struct pio_instance_operations { - int (*create)(struct packet_io_instance *pinst); - - void (*destroy)(struct packet_io_instance *pinst); -}; - -struct packet_io_instance { - /* packet_io instance name */ - char inst_name[NAME_MAX]; - - /* packet_io run mode of the instance */ - enum packet_io_run_mode mode; - - /* device handle set in this instance */ - struct packet_io_device *devices[DEV_MAX_CNT]; - - /* device's exactly count */ - uint32_t dev_cnt; - - /* instance operations */ - struct pio_instance_operations *inst_ops; - - union - { - struct pio_pcap_file_instance_context *pcap_file_inst_ctx; - struct pio_pcap_live_instance_context *pcap_live_inst_ctx; - struct pio_marsio_instance_context *marsio_inst_ctx; - } entity; -}; - -struct pio_device_operations { - int (*open)(struct packet_io_device *pdev); - - int (*close)(struct packet_io_device *pdev); - - int (*recv)(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts); - - int (*send)(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts); - - void (*pkt_free)(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts); - - void *(*buff_ctrlzone)(struct stellar_packet *p); - - char *(*buff_mtod)(struct stellar_packet *p); - - uint32_t (*buff_buflen)(struct stellar_packet *p); - - uint32_t (*buff_datalen)(struct stellar_packet *p); -}; - -struct packet_io_device { - /* device name */ - char dev_name[NAME_MAX]; - - /* device operations */ - struct pio_device_operations *dev_ops; - - /* number of receive queue */ - uint16_t rxq_num; - - /* number of send queue */ - uint16_t txq_num; - - /* packet io device context */ - union { - struct pio_pcap_file_device_context *pcap_file_dev_ctx; - struct pio_pcap_live_device_context *pcap_live_dev_ctx; - struct pio_marsio_device_context *marsio_dev_ctx; - } entity; - - /* packet_io instance which the device belongs to */ - struct packet_io_instance *ppio_inst; -}; - -/** - * @brief create packet_io instance which will manage packet_io device - * - * @param instance_name: packet_io instance name - * @param mode: packet_io run mode + * @brief + * + * @param instance_name: packet_io instance's name + * @param filename: packet_io config file's name + * @param devices(in/out): return packet_io_device pointer array, each pointer stands for an opened packet_io device + * @param dev_num: the num of opened packet_io_device's pointer + * @return struct packet_io_instance* */ struct packet_io_instance * -packet_io_instance_create(const char *instance_name, const enum packet_io_run_mode mode); +packet_io_init(const char *instance_name, const char *filename, struct packet_io_device *devices[], size_t *dev_num); /* destroy packet_io instance */ -void packet_io_instance_destroy(struct packet_io_instance *pinst); +void packet_io_fini(struct packet_io_instance *pinst); /** - * @brief open packet_io device for send/receive packets - * - * @param pinst: packet_io instance pointer - * @param dev_name: packet_io device name - * @param nr_rxq: number of receive queue for the device - * @param nr_txq: number of send queue for the device + * @brief + * + * @param inst_name + * @param mode + * @return struct packet_io_instance* */ -struct packet_io_device * -packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, uint16_t nr_rxq, uint16_t nr_txq); +struct packet_io_instance * +packet_io_instance_create(const char *inst_name, const enum packet_io_run_mode mode); -/** close packet_io device */ -void packet_io_device_close(struct packet_io_device *dev); +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, size_t nr_rxq, size_t nr_txq); + +void packet_io_device_close(struct packet_io_device *pdev); /** * @brief packet_io device receive function @@ -139,7 +70,7 @@ void packet_io_device_close(struct packet_io_device *dev); * @param p: received packet's pointer array * @param nr_p: number of received packets */ -int packet_io_device_rx(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts); +ssize_t packet_io_device_rx(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts); /** * @brief packet_io device send function @@ -149,33 +80,12 @@ int packet_io_device_rx(struct packet_io_device *pdev, uint16_t rxq_id, struct s * @param p: prepare to send packet's pointer array * @param nr_p: number of packets which prepare to send */ -int packet_io_device_tx(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts); +ssize_t packet_io_device_tx(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, size_t nr_pkts); /* * @brief packet_io free packet buff */ -void packet_io_pkts_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts); - -/** - * @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 stellar_packet *p); - -/** - * @brief get packet_io packet's data pointer - */ -char *packet_io_buff_mtod(struct packet_io_device *pdev, struct stellar_packet *p); - -/** - * @brief get packet_io packet's buffer length - */ -uint32_t packet_io_buff_buflen(struct packet_io_device *pdev, struct stellar_packet *p); - -/** - * @brief get packet_io packet's data length - */ -uint32_t packet_io_buff_datalen(struct packet_io_device *pdev, struct stellar_packet *p); +void packet_io_pkts_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts); #ifdef __cpluscplus } diff --git a/src/packet_io/packet_io_internal.cpp b/src/packet_io/packet_io_internal.cpp new file mode 100644 index 0000000..0a9e78e --- /dev/null +++ b/src/packet_io/packet_io_internal.cpp @@ -0,0 +1,461 @@ +/* +********************************************************************************************** +* File: packet_io_internal.cpp +* Description: +* Authors: Liu WenTan +* Date: 2022-07-15 +* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved. +*********************************************************************************************** +*/ + +#include + +#include "logger.h" +#include "utils.h" +#include "util_errors.h" +#include "packet_io.h" +#include "packet_io_util.h" +#include "packet_io_internal.h" +#include "toml/toml.h" +#include "./pcap_live_mode/pio_pcap_live.h" +#include "./pcap_file_mode/pio_pcap_file.h" +#include "./marsio_mode/pio_marsio.h" + +struct packet_io_config g_packet_io_config; + +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 = nullptr, + .pkt_free = pio_pcap_file_device_pkt_free, + .buff_ctrlzone = pio_pcap_file_device_buff_ctrlzone, + .buff_mtod = pio_pcap_file_device_buff_mtod, + }, + + { + .open = pio_pcap_live_device_open, + .close = pio_pcap_live_device_close, + .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, + }, + + { + .open = pio_marsio_device_open, + .close = pio_marsio_device_close, + .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, + } +}; + +struct pio_instance_operations pio_instance_ops_array[PACKET_IO_RUN_MODE_MAX] = +{ + { + .create = pio_pcap_file_instance_create, + .destroy = pio_pcap_file_instance_destroy, + }, + + { + .create = pio_pcap_live_instance_create, + .destroy = pio_pcap_live_instance_destroy, + }, + + { + .create = pio_marsio_instance_create, + .destroy = pio_marsio_instance_destroy, + } +}; + +struct packet_io_instance * +packet_io_instance_create(const char *inst_name, const enum packet_io_run_mode mode) +{ + if (nullptr == inst_name || mode < PACKET_IO_RUN_MODE_PCAP_FILE || mode >= PACKET_IO_RUN_MODE_MAX) { + 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."); + return nullptr; + } + + ssize_t ret = strncpy_safe(pio_instance->inst_name, inst_name, sizeof(pio_instance->inst_name)); + if (ret < 0) { + log_error(ST_ERR_STR_COPY, "packet_io instance name copy failed."); + return nullptr; + } + + TAILQ_INIT(&pio_instance->device_queue_head); + pio_instance->mode = mode; + pio_instance->inst_ops = &pio_instance_ops_array[mode]; + + ret = pio_instance->inst_ops->create(pio_instance); + if (ret < 0) { + log_error(ST_ERR_PIO_INSTANCE, "packet_io instance create failed."); + return nullptr; + } + + return pio_instance; +} + +static ssize_t toml_parse_table(toml_table_t *table, const char *string_key, const char *file_name, toml_table_t **out) +{ + *out = toml_table_in(table, string_key); + if (nullptr == *out) { + log_error(ST_ERR_PIO_CONFIG, "can't find '%s' section in %s", string_key, file_name); + return -1; + } + + return 0; +} + +static ssize_t toml_parse_string(toml_table_t *table, const char *string_key, const char *file_name, char *out) +{ + toml_datum_t string_val = toml_string_in(table, string_key); + if (!string_val.ok) { + log_error(ST_ERR_PIO_CONFIG, "can't find '%s' configuration iterm in %s", string_key, file_name); + return -1; + } + + if (strlen(string_val.u.s) <= 0) { + log_error(ST_ERR_PIO_CONFIG, "invalid value for '%s' configuration item in %s", string_key, file_name); + FREE(string_val.u.s); + return -1; + } + + strncpy_safe(out, string_val.u.s, strlen(string_val.u.s)); + FREE(string_val.u.s); + + return 0; +} + +static ssize_t toml_parse_int(toml_table_t *table, const char *int_key, const char *file_name, int *out) +{ + toml_datum_t int_val = toml_int_in(table, int_key); + if (!int_val.ok) { + log_error(ST_ERR_PIO_CONFIG, "can't find '%s' configuration iterm in %s", int_key, file_name); + return -1; + } + *out = int_val.u.i; + + return 0; +} + +enum packet_io_run_mode pio_run_mode_str2int(const char *mode_str) +{ + enum packet_io_run_mode mode_int = PACKET_IO_RUN_MODE_MAX; + if (strncmp("PCAP_FILE_MODE", mode_str, strlen(mode_str)) == 0) { + mode_int = PACKET_IO_RUN_MODE_PCAP_FILE; + } else if (strncmp("PCAP_LIVE_MODE", mode_str, strlen(mode_str)) == 0) { + mode_int = PACKET_IO_RUN_MODE_PCAP_LIVE; + } else if (strncmp("MARSIO_MODE", mode_str, strlen(mode_str)) == 0) { + mode_int = PACKET_IO_RUN_MODE_MARSIO; + } else { + log_error(ST_ERR_RUN_MODE, "unknown run mode '%s'", mode_str); + } + + return mode_int; +} + +static ssize_t toml_parse_pcap_file_mode(toml_table_t *tab, struct packet_io_config *config, const char *file_name) +{ + if (toml_parse_string(tab, "PCAP_FILE_PATH", file_name, config->pcap.path) < 0) { + log_error(ST_ERR_PIO_CONFIG, "can't parse 'PCAP_FILE_PATH' config iterm in 'PACKET_IO' section of %s", file_name); + return -1; + } + + if (toml_parse_int(tab, "DELETE_WHEN_DONE", file_name, &config->pcap.should_delete) < 0) { + log_notice("can't parse 'DELETE_WHEN_DONE' config iterm in 'PACKET_IO' section of %s", file_name); + } + + if (toml_parse_string(tab, "BPF_FILTER", file_name, config->pcap.bpf_string) < 0) { + log_notice("can't parse 'BPF_FILTER' config iterm in 'PACKET_IO' section of %s", file_name); + } + + return 0; +} + +static ssize_t toml_parse_pcap_live_mode(toml_table_t *tab, struct packet_io_config *config, const char *file_name) +{ + toml_array_t *interface_array = nullptr; + toml_datum_t interface_str; + + interface_array = toml_array_in(tab, "INTERFACE"); + if (nullptr == interface_array) { + log_error(ST_ERR_PIO_CONFIG, "can't find 'INTERFACE' config iterm in 'PACKET_IO' section of %s", file_name); + return -1; + } + + for (ssize_t i = 0; i < toml_array_nelem(interface_array); i++) { + interface_str = toml_string_at(interface_array, i); + if (!interface_str.ok) { + log_error(ST_ERR_PIO_CONFIG, "can't parse 'INTERFACE' config iterm in 'PACKET_IO' section of %s", file_name); + return -1; + } + + strncpy_safe(config->common.dev_name[i], interface_str.u.s, sizeof(config->common.dev_name[i])); + config->common.dev_cnt++; + } + + if (toml_parse_int(tab, "SNAP_LEN", file_name, &config->pcap.snaplen) < 0) { + log_notice("can't parse 'SNAP_LEN' config iterm in 'PACKET_IO' section of %s", file_name); + } + + if (toml_parse_int(tab, "PROMISC", file_name, &config->pcap.promisc) < 0) { + log_notice("can't parse 'PROMISC' config iterm in 'PACKET_IO' section of %s", file_name); + } + + if (toml_parse_string(tab, "BPF_FILTER", file_name, config->pcap.bpf_string) < 0) { + log_notice("can't parse 'BPF_FILTER' config iterm in 'PACKET_IO' section of %s", file_name); + } + + return 0; +} + +static ssize_t toml_parse_marsio_mode(toml_table_t *tab, struct packet_io_config *config, const char *file_name) +{ + toml_array_t *interface_array = nullptr; + toml_datum_t interface_str; + + interface_array = toml_array_in(tab, "INTERFACE"); + if (nullptr == interface_array) { + log_error(ST_ERR_PIO_CONFIG, "can't find 'INTERFACE' config iterm in 'PACKET_IO' section of %s", file_name); + return -1; + } + + for (int i = 0; i < toml_array_nelem(interface_array); i++) { + interface_str = toml_string_at(interface_array, i); + if (!interface_str.ok) { + log_error(ST_ERR_PIO_CONFIG, "can't parse 'INTERFACE' config iterm in 'PACKET_IO' section of %s", file_name); + return -1; + } + strncpy_safe(config->common.dev_name[i], interface_str.u.s, sizeof(config->common.dev_name[i])); + config->common.dev_cnt++; + } + + return 0; +} + +static ssize_t toml_parse_packet_io_section(toml_table_t *root, struct packet_io_config *config, const char *file_name) +{ + toml_table_t *packet_io_section = nullptr; + + if (toml_parse_table(root, "PACKET_IO", file_name, &packet_io_section) < 0) { + return -1; + } + + char run_mode[STR_MAX_LEN] = {0}; + if (toml_parse_string(packet_io_section, "RUN_MODE", file_name, run_mode) < 0) { + return -1; + } + + config->common.mode = pio_run_mode_str2int(run_mode); + if (config->common.mode == PACKET_IO_RUN_MODE_MAX) { + return -1; + } + + if (toml_parse_int(packet_io_section, "WORKER_THREAD_NUM", file_name, &config->common.thread_num) < 0) { + return -1; + } + + if (config->common.mode == PACKET_IO_RUN_MODE_PCAP_FILE) { + if (toml_parse_pcap_file_mode(packet_io_section, config, file_name) < 0) { + return -1; + } + } else if (config->common.mode == PACKET_IO_RUN_MODE_PCAP_LIVE) { + if (toml_parse_pcap_live_mode(packet_io_section, config, file_name) < 0) { + return -1; + } + } else { + if (toml_parse_marsio_mode(packet_io_section, config, file_name) < 0) { + return -1; + } + } + + return 0; +} + +ssize_t packet_io_config_parse(struct packet_io_config *config, const char *file_name) +{ + char errbuf[BUFSIZ] = {0}; + + FILE *fp = fopen(file_name, "r"); + if (nullptr == fp) { + log_error(ST_ERR_FOPEN, "open packet_io config file failed."); + return -1; + } + + toml_table_t *root = nullptr; + root = toml_parse_file(fp, errbuf, sizeof(errbuf)); + if (nullptr == root) { + goto err; + } + + if (toml_parse_packet_io_section(root, config, file_name) < 0) { + goto err; + } + + toml_free(root); + fclose(fp); + log_info("packet_io config file '%s' parse success", file_name); + + return 0; +err: + if (root) { + toml_free(root); + } + + if (fp) { + fclose(fp); + fp = nullptr; + } + + return -1; +} + +struct packet_io_device * +packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, size_t nr_rxq, size_t nr_txq) +{ + if (nullptr == pinst || nullptr == dev_name) { + return nullptr; + } + + struct packet_io_device *pdev = CALLOC(struct packet_io_device, 1); + if (nullptr == pdev) { + log_error(ST_ERR_MEM_ALLOC, "packet_io device alloc failed."); + return nullptr; + } + + ssize_t ret = strncpy_safe(pdev->dev_name, dev_name, sizeof(pdev->dev_name)); + if (ret < 0) { + log_error(ST_ERR_STR_COPY, "packet_io device name copy failed."); + return nullptr; + } + + pdev->rxq_num = nr_rxq; + pdev->txq_num = nr_txq; + pdev->ppio_inst = pinst; + pdev->dev_ops = &pio_device_ops_array[pinst->mode]; + + TAILQ_INSERT_TAIL(&pinst->device_queue_head, pdev, next); + pinst->dev_cnt++; + + ret = pdev->dev_ops->open(pdev); + if (ret < 0) { + log_error(ST_ERR_PIO_DEVICE, "packet_io device open failed."); + return nullptr; + } + + return pdev; +} + +void packet_io_device_close(struct packet_io_device *pdev) +{ + ssize_t ret = pdev->dev_ops->close(pdev); + if (ret < 0) { + log_error(ST_ERR_PIO_DEVICE, "packet_io device close failed."); + return; + } + + struct packet_io_device *node = nullptr; + struct packet_io_device *next_node = nullptr; + for (node = TAILQ_FIRST(&pdev->ppio_inst->device_queue_head); node != nullptr; node = next_node) { + next_node = TAILQ_NEXT(node, next); + if (node == pdev) { + pdev->ppio_inst->dev_cnt--; + /* Remove the item from the tail queue. */ + TAILQ_REMOVE(&pdev->ppio_inst->device_queue_head, node, next); + + /* Free the item as we don't need it anymore. */ + FREE(node); + break; + } + } +} + +struct packet_io_instance * +packet_io_init(const char *instance_name, const char *file_name, struct packet_io_device *devices[], size_t *dev_num) +{ + /* parse config file */ + memset(&g_packet_io_config, 0, sizeof(g_packet_io_config)); + ssize_t ret = packet_io_config_parse(&g_packet_io_config, file_name); + if (ret < 0) { + log_error(ST_ERR_PIO_CONFIG, "packet_io config parse failed."); + return nullptr; + } + + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", g_packet_io_config.common.mode); + if (nullptr == ppio_inst) { + log_error(ST_ERR_PIO_INSTANCE, "packet_io instance init failed."); + return nullptr; + } + + size_t thread_num = g_packet_io_config.common.thread_num; + if (g_packet_io_config.common.mode == PACKET_IO_RUN_MODE_PCAP_FILE) { + devices[0] = packet_io_device_open(ppio_inst, g_packet_io_config.pcap.path, thread_num, thread_num); + if (nullptr == devices[0]) { + log_error(ST_ERR_PIO_DEVICE, "packet_io device '%s' open failed.", g_packet_io_config.pcap.path); + return nullptr; + } + *dev_num = 1; + } else { + *dev_num = g_packet_io_config.common.dev_cnt; + for (size_t i = 0; i < *dev_num; i++) { + devices[i] = packet_io_device_open(ppio_inst, g_packet_io_config.common.dev_name[i], thread_num, thread_num); + if (nullptr == devices[i]) { + log_error(ST_ERR_PIO_DEVICE, "packet_io device '%s' open failed.", g_packet_io_config.common.dev_name[i]); + return nullptr; + } + } + } + + return ppio_inst; +} + +void packet_io_fini(struct packet_io_instance *pinst) +{ + packet_io_instance_destroy(pinst); +} + +void packet_io_instance_destroy(struct packet_io_instance *pinst) +{ + if (nullptr == pinst) { + return; + } + + pinst->inst_ops->destroy(pinst); + FREE(pinst); +} + +ssize_t packet_io_device_rx(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts) +{ + return pdev->dev_ops->recv(pdev, rxq_id, pkts, nr_pkts); +} + +ssize_t packet_io_device_tx(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, size_t nr_pkts) +{ + return pdev->dev_ops->send(pdev, txq_id, pkts, nr_pkts); +} + +void packet_io_pkts_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts) +{ + return pdev->dev_ops->pkt_free(pdev, qid, pkts, nr_pkts); +} + +char *get_stellar_packet_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len) +{ + return pio_device_ops_array[g_packet_io_config.common.mode].buff_ctrlzone(p, ctrlzone_len); +} + +char *get_stellar_packet_data(struct stellar_packet *p, size_t *data_len) +{ + return pio_device_ops_array[g_packet_io_config.common.mode].buff_mtod(p, data_len); +} \ No newline at end of file diff --git a/src/packet_io/packet_io_internal.h b/src/packet_io/packet_io_internal.h new file mode 100644 index 0000000..e52dd7c --- /dev/null +++ b/src/packet_io/packet_io_internal.h @@ -0,0 +1,155 @@ +/* +********************************************************************************************** +* File: packet_io_internal.h +* Description: packet_io internal api +* Authors: Liu WenTan +* Date: 2022-07-15 +* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved. +*********************************************************************************************** +*/ + +#ifndef _PACKET_IO_INTERNAL_H_ +#define _PACKET_IO_INTERNAL_H_ + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include +#include + +#include "packet_io_util.h" + +#define DEV_MAX_CNT 64 + +/** + * 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 + */ + +struct pio_instance_operations { + ssize_t (*create)(struct packet_io_instance *pinst); + + void (*destroy)(struct packet_io_instance *pinst); +}; + +struct packet_io_instance { + /* packet_io instance name */ + char inst_name[NAME_MAX]; + + /* packet_io run mode of the instance */ + enum packet_io_run_mode mode; + + /* device handle set in this instance */ + TAILQ_HEAD(pio_device_queue, packet_io_device) device_queue_head; + + /* device's exactly count */ + uint32_t dev_cnt; + + /* instance operations */ + struct pio_instance_operations *inst_ops; + + union + { + struct pio_pcap_file_instance_context *pcap_file_inst_ctx; + struct pio_pcap_live_instance_context *pcap_live_inst_ctx; + struct pio_marsio_instance_context *marsio_inst_ctx; + } entity; +}; + +struct pio_device_operations { + ssize_t (*open)(struct packet_io_device *pdev); + + ssize_t (*close)(struct packet_io_device *pdev); + + ssize_t (*recv)(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts); + + ssize_t (*send)(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, size_t nr_pkts); + + void (*pkt_free)(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts); + + char *(*buff_ctrlzone)(struct stellar_packet *p, size_t *ctrlzone_len); + + char *(*buff_mtod)(struct stellar_packet *p, size_t *data_len); +}; + +struct packet_io_device { + /* device name */ + char dev_name[NAME_MAX]; + + /* device operations */ + struct pio_device_operations *dev_ops; + + /* number of receive queue */ + uint16_t rxq_num; + + /* number of send queue */ + uint16_t txq_num; + + /* packet io device context */ + union { + struct pio_pcap_file_device_context *pcap_file_dev_ctx; + struct pio_pcap_live_device_context *pcap_live_dev_ctx; + struct pio_marsio_device_context *marsio_dev_ctx; + } entity; + + /* packet_io instance which the device belongs to */ + struct packet_io_instance *ppio_inst; + + TAILQ_ENTRY(packet_io_device) next; +}; + +struct pio_marsio_config { + /* marsio ctrlzone id */ + int mr_ctrlzone_id; + + const char *libmarsio_path; +}; + +struct pio_pcap_config { + char path[PATH_MAX]; + /* bpf filter string, such as "tcp and port 25"*/ + char bpf_string[STR_MAX_LEN]; + + /* delete after the pcap file is processed */ + int should_delete; + + //time_t delay; + + /* snapshot length */ + int snaplen; + + /* promiscuous value */ + int promisc; +}; + +struct pio_common_config { + /* packet_io run mode */ + enum packet_io_run_mode mode; + + /* worker thread num */ + int thread_num; + + /* device name list */ + char dev_name[DEV_MAX_CNT][NAME_MAX]; + + /* device counts */ + uint32_t dev_cnt; +}; + +/* store packet_io configuration */ +struct packet_io_config { + struct pio_common_config common; + struct pio_pcap_config pcap; + struct pio_marsio_config marsio; +}; + +extern struct packet_io_config g_packet_io_config; + +#ifdef __cpluscplus +} +#endif + +#endif /* _PACKET_IO_INTERNAL_H_ */ \ No newline at end of file diff --git a/src/packet_io/packet_io_util.cpp b/src/packet_io/packet_io_util.cpp index c5cc4ac..e94237f 100644 --- a/src/packet_io/packet_io_util.cpp +++ b/src/packet_io/packet_io_util.cpp @@ -16,14 +16,14 @@ #include "utils.h" #include "packet_io_util.h" -static int packet_copy_data_offset(uint8_t *ptr, uint32_t offset, const uint8_t *data, uint32_t data_len) +static ssize_t 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) +ssize_t 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); } @@ -99,12 +99,11 @@ void release_pio_packet_queue(struct pio_packet_queue *q) while (q->len != 0) { struct pio_packet *p = pio_packet_dequeue(q); - q->len--; FREE(p); } } -int strncpy_safe(char *dst, const char *src, size_t dst_size) +ssize_t strncpy_safe(char *dst, const char *src, size_t dst_size) { if (nullptr == dst || nullptr == src || dst_size == 0) { return -1; @@ -173,7 +172,7 @@ static uint64_t generic_2tuple_hash(uint8_t *src, uint8_t *dst, size_t n) uint64_t key1 = simple_murmur_hash(src, n); uint64_t key2 = simple_murmur_hash(dst, n); - return (key1 | key2); + return (key1 ^ key2); } uint64_t pio_packet_hash(struct pio_packet *p) diff --git a/src/packet_io/packet_io_util.h b/src/packet_io/packet_io_util.h index ddd2422..15cc1df 100644 --- a/src/packet_io/packet_io_util.h +++ b/src/packet_io/packet_io_util.h @@ -19,6 +19,8 @@ extern "C" #include #include +#define STR_MAX_LEN 1024 + #ifndef DLT_EN10MB #define DLT_EN10MB 1 #endif @@ -82,9 +84,9 @@ struct pio_packet_queue { * * @retval -1(failed), 0(success) */ -int strncpy_safe(char *dst, const char *src, size_t dst_size); +ssize_t strncpy_safe(char *dst, const char *src, size_t dst_size); -int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len); +ssize_t packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len); /** * @brief ip hash function for struct pio_packet, 2 tuple(sip/dip) hash 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 024634b..17c3dce 100644 --- a/src/packet_io/pcap_file_mode/pio_pcap_file.cpp +++ b/src/packet_io/pcap_file_mode/pio_pcap_file.cpp @@ -22,6 +22,7 @@ #include "pio_pcap_file.h" #include "packet_io.h" #include "packet_io_util.h" +#include "packet_io_internal.h" /** * @brief validate path is a valid plain file or directory @@ -30,10 +31,10 @@ * if success, dir == nullptr <---> means path is plain file * dir != nullptr <---> means path is directory */ -static int validate_directory_or_file(const char *path, DIR **dir) +static ssize_t validate_directory_or_file(const char *path, DIR **dir) { DIR *temp_dir = nullptr; - int ret = -1; + ssize_t ret = -1; temp_dir = opendir(path); if (nullptr == temp_dir) { @@ -79,7 +80,7 @@ static bool peek_first_packet_timestamp(struct pcap_plain_file_info *pfile_info) return true; } -static int init_pcap_file(struct pcap_plain_file_info *pfile_info) +static ssize_t init_pcap_file(struct pcap_plain_file_info *pfile_info) { char errbuf[PCAP_ERRBUF_SIZE] = ""; @@ -121,7 +122,7 @@ static int init_pcap_file(struct pcap_plain_file_info *pfile_info) return 0; } -static int pcap_plain_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *file_name) +static ssize_t pcap_plain_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *file_name) { if (nullptr == pfile_dev_ctx) { return -1; @@ -154,7 +155,7 @@ static int pcap_plain_file_init(struct pio_pcap_file_device_context *pfile_dev_c return 0; } -static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *dir_name, DIR *directory) +static ssize_t pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_dev_ctx, const char *dir_name, DIR *directory) { if (nullptr == pfile_dev_ctx) { return -1; @@ -173,7 +174,8 @@ static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_d return -1; } - //pdir_info->delay = 30; + /* TODO: if should configurable */ + pdir_info->delay = 30; pdir_info->shared = &pfile_dev_ctx->shared; pdir_info->directory = directory; TAILQ_INIT(&pdir_info->file_queue_head); @@ -184,17 +186,17 @@ static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_d return 0; } -static int pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ctx) +static ssize_t pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ctx) { if (nullptr == pfile_dev_ctx) { return -1; } /* TODO: get conf and assign pfile_dev_ctx->shared */ - if ((g_engine_instance.config.packet_io.mode == PACKET_IO_RUN_MODE_PCAP_FILE) && - g_engine_instance.config.packet_io.bpf_string != nullptr) { + if ((g_packet_io_config.common.mode == PACKET_IO_RUN_MODE_PCAP_FILE) && + g_packet_io_config.pcap.bpf_string != nullptr) { memset(pfile_dev_ctx->shared.bpf_string, 0, sizeof(pfile_dev_ctx->shared.bpf_string)); - int ret = strncpy_safe(pfile_dev_ctx->shared.bpf_string, g_engine_instance.config.packet_io.bpf_string, + ssize_t ret = strncpy_safe(pfile_dev_ctx->shared.bpf_string, g_packet_io_config.pcap.bpf_string, sizeof(pfile_dev_ctx->shared.bpf_string)); if (ret < 0) { log_error(ST_ERR_STR_COPY, "pcap file bpf string copy failed."); @@ -202,7 +204,7 @@ static int pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ } } - pfile_dev_ctx->shared.should_delete = g_engine_instance.config.packet_io.should_delete; + pfile_dev_ctx->shared.should_delete = g_packet_io_config.pcap.should_delete; /* init pcap file device packet queue */ @@ -239,9 +241,9 @@ static void cleanup_pcap_directory_info(struct pcap_file_directory_info *pdir_in } -int pio_pcap_file_device_open(struct packet_io_device *pdev) +ssize_t pio_pcap_file_device_open(struct packet_io_device *pdev) { - int status = -1; + ssize_t status = -1; DIR *directory = nullptr; if (nullptr == pdev) { @@ -255,7 +257,7 @@ int pio_pcap_file_device_open(struct packet_io_device *pdev) return -1; } - pdev->entity.pcap_file_dev_ctx->pio_dev = pdev; + pdev->entity.pcap_file_dev_ctx->pdev = pdev; status = pcap_file_shared_init(pdev->entity.pcap_file_dev_ctx); if (status < 0) { @@ -287,24 +289,26 @@ int pio_pcap_file_device_open(struct packet_io_device *pdev) return 0; } -int pio_pcap_file_device_close(struct packet_io_device *pdev) +ssize_t pio_pcap_file_device_close(struct packet_io_device *pdev) { if (nullptr == pdev) { log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "invalid pdev pointer so close pcap file device failed!"); return -1; } - if (pdev->entity.pcap_file_dev_ctx->entity.file != nullptr) { - cleanup_pcap_plain_file_info(pdev->entity.pcap_file_dev_ctx->entity.file); - } + if (pdev->entity.pcap_file_dev_ctx != nullptr) { + if (pdev->entity.pcap_file_dev_ctx->entity.file != nullptr) { + cleanup_pcap_plain_file_info(pdev->entity.pcap_file_dev_ctx->entity.file); + } - if (pdev->entity.pcap_file_dev_ctx->entity.dir != nullptr) { - cleanup_pcap_directory_info(pdev->entity.pcap_file_dev_ctx->entity.dir); - } + if (pdev->entity.pcap_file_dev_ctx->entity.dir != nullptr) { + cleanup_pcap_directory_info(pdev->entity.pcap_file_dev_ctx->entity.dir); + } - for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) { - if (pdev->entity.pcap_file_dev_ctx->pkt_queues[i].len != 0) { - release_pio_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]); + for (uint32_t i = 0; i < PKT_QUEUE_MAX_NUM; i++) { + if (pdev->entity.pcap_file_dev_ctx->pkt_queues[i].len != 0) { + release_pio_packet_queue(&pdev->entity.pcap_file_dev_ctx->pkt_queues[i]); + } } } @@ -341,7 +345,7 @@ void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_c } /* nr_rxq <= PKT_QUEUE_MAX_NUM */ - uint16_t nr_rxq = pfile_dev_ctx->pio_dev->rxq_num; + uint16_t nr_rxq = pfile_dev_ctx->pdev->rxq_num; uint16_t rxq_id = pio_packet_hash(p) % nr_rxq; /* hash to specific queue id and enqueue */ @@ -350,8 +354,8 @@ 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, uint16_t rxq_id, - struct stellar_packet **pkts, int nr_pkts) +static ssize_t pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id, + struct stellar_packet **pkts, size_t nr_pkts) { if (pfile_dev_ctx->entity.file->first_pkt_hdr != nullptr) { pthread_mutex_lock(&pfile_dev_ctx->entity.file->handle_mutex); @@ -365,7 +369,7 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx } int packet_q_len = nr_pkts; - int res = -1; + ssize_t res = -1; pthread_mutex_lock(&pfile_dev_ctx->entity.file->handle_mutex); res = pcap_dispatch(pfile_dev_ctx->entity.file->pcap_handle, packet_q_len, @@ -380,17 +384,19 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx } else { // success struct pio_packet *p = nullptr; - int i = 0; + size_t i = 0; + uint32_t q_len = 0; pthread_mutex_lock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q); do { p = pio_packet_dequeue(&pfile_dev_ctx->pkt_queues[rxq_id]); + q_len = pfile_dev_ctx->pkt_queues[rxq_id].len; pkts[i] = (struct stellar_packet *)p; i++; - } while (p != nullptr && (i < nr_pkts)); + } while ((q_len != 0) && (i < nr_pkts)); pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q); - if (nullptr == p) { - res = i - 1; + if (q_len == 0) { + res = i; } else { res = nr_pkts; } @@ -399,10 +405,10 @@ static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx return res; } -static int pcap_directory_get_modified_time(char *pfile, struct timespec *out) +static ssize_t pcap_directory_get_modified_time(char *pfile, struct timespec *out) { struct stat buf; - int ret = -1; + ssize_t ret = -1; if (nullptr == pfile) { return ret; @@ -443,7 +449,7 @@ find_pending_file_to_add(struct pio_pcap_file_device_context *pfile_dev_ctx, str } struct pending_file *file_to_add = CALLOC(struct pending_file, 1); - int ret = strncpy_safe(file_to_add->file_name, abs_path, sizeof(file_to_add->file_name)); + ssize_t ret = strncpy_safe(file_to_add->file_name, abs_path, sizeof(file_to_add->file_name)); if (ret < 0) { log_error(ST_ERR_STR_COPY, "file_to_add file name copy failed."); return nullptr; @@ -456,7 +462,7 @@ find_pending_file_to_add(struct pio_pcap_file_device_context *pfile_dev_ctx, str return file_to_add; } -static int pcap_directory_insert_file(struct pio_pcap_file_device_context *pfile_dev_ctx, struct pending_file *file_to_add) +static ssize_t pcap_directory_insert_file(struct pio_pcap_file_device_context *pfile_dev_ctx, struct pending_file *file_to_add) { if (nullptr == pfile_dev_ctx || file_to_add) { log_error(ST_ERR_PCAP_FILE_COLLECT_FAILED, "invalid directory or file parameters."); @@ -486,7 +492,7 @@ static int pcap_directory_insert_file(struct pio_pcap_file_device_context *pfile return 0; } -static int pcap_directory_collect_pending_files(struct pio_pcap_file_device_context *pfile_dev_ctx, struct timespec *deadline) +static ssize_t pcap_directory_collect_pending_files(struct pio_pcap_file_device_context *pfile_dev_ctx, struct timespec *deadline) { if (nullptr == pfile_dev_ctx) { return -1; @@ -526,10 +532,10 @@ 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, uint16_t rxq_id, - struct stellar_packet **pkts, int nr_pkts) +static ssize_t pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id, + struct stellar_packet **pkts, size_t nr_pkts) { - int res = -1; + ssize_t res = -1; struct timespec deadline; memset(&deadline, 0, sizeof(struct timespec)); @@ -611,7 +617,7 @@ static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_de return res; } -int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts) +ssize_t pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts) { struct pio_pcap_file_device_context *pfile_dev_ctx = pdev->entity.pcap_file_dev_ctx; if (nullptr == pfile_dev_ctx) { @@ -619,7 +625,7 @@ int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, return -1; } - int res = -1; + ssize_t 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, rxq_id, pkts, nr_pkts); @@ -631,15 +637,15 @@ int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, return res; } -void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint16_t qid, struct stellar_packet **pkts, int nr_pkts) +void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts) { - for (int i = 0; i < nr_pkts; i++) { + for (size_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) +ssize_t pio_pcap_file_instance_create(struct packet_io_instance *pinst) { if (nullptr == pinst) { log_error(ST_ERR_PIO_PCAP_FILE_INSTANCE, "invalid pcap file instance pointer."); @@ -661,34 +667,29 @@ void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst) return; } - FREE(pinst->entity.pcap_file_inst_ctx); - - for (uint32_t i = 0; i < pinst->dev_cnt; i++) { - pio_pcap_file_device_close(pinst->devices[i]); - FREE(pinst->devices[i]); + if (pinst->entity.pcap_file_inst_ctx != nullptr) { + FREE(pinst->entity.pcap_file_inst_ctx); } + + struct packet_io_device *node = nullptr; + while ((node = TAILQ_FIRST(&pinst->device_queue_head)) != nullptr) { + TAILQ_REMOVE(&pinst->device_queue_head, node, next); + pinst->dev_cnt--; + pio_pcap_file_device_close(node); + FREE(node); + } } -void *pio_pcap_file_device_buff_ctrlzone(struct stellar_packet *p) +char *pio_pcap_file_device_buff_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len) { struct pio_packet *pkt = (struct pio_packet *)p; - return pkt->pkt_hdr; + *ctrlzone_len = CUSTOM_ZONE_LEN; + return (char *)pkt->pkt_hdr; } -char *pio_pcap_file_device_buff_mtod(struct stellar_packet *p) +char *pio_pcap_file_device_buff_mtod(struct stellar_packet *p, size_t *data_len) { struct pio_packet *pkt = (struct pio_packet *)p; + *data_len = pkt->pkt_len; return (char *)pkt->pkt_payload; -} - -uint32_t pio_pcap_file_device_buff_buflen(struct stellar_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 stellar_packet *p) -{ - struct pio_packet *pkt = (struct pio_packet *)p; - return (pkt->pkt_len); -} +} \ No newline at end of file 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 e72781d..6bce0d9 100644 --- a/src/packet_io/pcap_file_mode/pio_pcap_file.h +++ b/src/packet_io/pcap_file_mode/pio_pcap_file.h @@ -21,7 +21,6 @@ extern "C" #include #include -#include "global_var.h" #include "packet_io_util.h" struct pio_pcap_file_instance_context { @@ -38,8 +37,8 @@ struct pcap_file_shared_info { /* bpf filter string, such as "tcp and port 25"*/ char bpf_string[STR_MAX_LEN]; - /* delete after the pcap file is read */ - bool should_delete; + /* if delete when pcapfile is processed */ + int should_delete; /* the timestamp of the last process */ struct timespec last_processed_ts; @@ -100,7 +99,7 @@ struct pio_pcap_file_device_context { struct pcap_file_shared_info shared; /* point to packet_io device it belongs to */ - struct packet_io_device *pio_dev; + struct packet_io_device *pdev; }; /** @@ -109,7 +108,7 @@ struct pio_pcap_file_device_context { * @param pinst * @return int */ -int pio_pcap_file_instance_create(struct packet_io_instance *pinst); +ssize_t pio_pcap_file_instance_create(struct packet_io_instance *pinst); /** * @brief @@ -126,24 +125,20 @@ void pio_pcap_file_instance_destroy(struct packet_io_instance *pinst); * pdev->rxq_num: number of the packet receiving queues for the device * pdev->txq_num: number of the packet sending queues for the device */ -int pio_pcap_file_device_open(struct packet_io_device *pdev); +ssize_t 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); +ssize_t pio_pcap_file_device_close(struct packet_io_device *pdev); -int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts); +ssize_t pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts); -void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts); +void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts); -void *pio_pcap_file_device_buff_ctrlzone(struct stellar_packet *p); +char *pio_pcap_file_device_buff_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len); -char *pio_pcap_file_device_buff_mtod(struct stellar_packet *p); - -uint32_t pio_pcap_file_device_buff_buflen(struct stellar_packet *p); - -uint32_t pio_pcap_file_device_buff_datalen(struct stellar_packet *p); +char *pio_pcap_file_device_buff_mtod(struct stellar_packet *p, size_t *data_len); #ifdef __cpluscplus } 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 73f6a72..0ccc804 100644 --- a/src/packet_io/pcap_live_mode/pio_pcap_live.cpp +++ b/src/packet_io/pcap_live_mode/pio_pcap_live.cpp @@ -18,11 +18,12 @@ #include "pio_pcap_live.h" #include "packet_io.h" #include "packet_io_util.h" +#include "packet_io_internal.h" #define DEFAULT_MAX_PACKET_SIZE 65535 #define TIMEOUT_MS 500 -static int pcap_live_init(struct pio_pcap_live_device_context *plive_dev_ctx, const char *dev_name) +static ssize_t pcap_live_init(struct pio_pcap_live_device_context *plive_dev_ctx, const char *dev_name) { if (nullptr == plive_dev_ctx) { return -1; @@ -36,14 +37,14 @@ static int pcap_live_init(struct pio_pcap_live_device_context *plive_dev_ctx, co return -1; } - if (g_engine_instance.config.packet_io.snaplen == 0) { + if (g_packet_io_config.pcap.snaplen == 0) { plive_dev_ctx->pcap_snaplen = DEFAULT_MAX_PACKET_SIZE; } else { - plive_dev_ctx->pcap_snaplen = g_engine_instance.config.packet_io.snaplen; + plive_dev_ctx->pcap_snaplen = g_packet_io_config.pcap.snaplen; } /* set snaplen */ - int res = pcap_set_snaplen(plive_dev_ctx->pcap_handle, plive_dev_ctx->pcap_snaplen); + ssize_t res = pcap_set_snaplen(plive_dev_ctx->pcap_handle, plive_dev_ctx->pcap_snaplen); if (res != 0) { log_error(ST_ERR_PCAP_SET_SNAPLEN, "could not set snaplen, error:%s", pcap_geterr(plive_dev_ctx->pcap_handle)); @@ -52,7 +53,7 @@ static int pcap_live_init(struct pio_pcap_live_device_context *plive_dev_ctx, co log_info("set snaplen to %d for %s", plive_dev_ctx->pcap_snaplen, dev_name); /* set promisc */ - res = pcap_set_promisc(plive_dev_ctx->pcap_handle, g_engine_instance.config.packet_io.promisc); + res = pcap_set_promisc(plive_dev_ctx->pcap_handle, g_packet_io_config.pcap.promisc); if (res != 0) { log_error(ST_ERR_PCAP_SET_PROMISC, "could not set promisc mode, error:%s", pcap_geterr(plive_dev_ctx->pcap_handle)); @@ -76,8 +77,8 @@ static int pcap_live_init(struct pio_pcap_live_device_context *plive_dev_ctx, co plive_dev_ctx->pcap_state = PCAP_STATE_UP; /* set bpf filter */ - if (strlen(g_engine_instance.config.packet_io.bpf_string) != 0) { - res = strncpy_safe(plive_dev_ctx->bpf_string, g_engine_instance.config.packet_io.bpf_string, + if (strlen(g_packet_io_config.pcap.bpf_string) != 0) { + res = strncpy_safe(plive_dev_ctx->bpf_string, g_packet_io_config.pcap.bpf_string, sizeof(plive_dev_ctx->bpf_string)); if (res < 0) { log_error(ST_ERR_STR_COPY, "plive_dev_ctx bpf string copy failed."); @@ -107,9 +108,9 @@ static int pcap_live_init(struct pio_pcap_live_device_context *plive_dev_ctx, co return res; } -int pio_pcap_live_device_open(struct packet_io_device *pdev) +ssize_t pio_pcap_live_device_open(struct packet_io_device *pdev) { - int res = -1; + ssize_t res = -1; if (nullptr == pdev) { log_error(ST_ERR_PIO_PCAP_LIVE_DEVICE, "invalid packet_io_device pointer."); @@ -124,7 +125,7 @@ int pio_pcap_live_device_open(struct packet_io_device *pdev) pthread_mutex_init(&pdev->entity.pcap_live_dev_ctx->handle_mutex, nullptr); - pdev->entity.pcap_live_dev_ctx->pio_dev = pdev; + pdev->entity.pcap_live_dev_ctx->pdev = pdev; res = pcap_live_init(pdev->entity.pcap_live_dev_ctx, pdev->dev_name); if (res < 0) { @@ -136,7 +137,7 @@ int pio_pcap_live_device_open(struct packet_io_device *pdev) return res; } -int pio_pcap_live_device_close(struct packet_io_device *pdev) +ssize_t pio_pcap_live_device_close(struct packet_io_device *pdev) { if (nullptr == pdev) { log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "invalid pdev pointer, so close pcap live device failed."); @@ -182,7 +183,7 @@ static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_h } /* nr_rxq <= PKT_QUEUE_MAX_NUM */ - uint16_t nr_rxq = plive_dev_ctx->pio_dev->rxq_num; + uint16_t nr_rxq = plive_dev_ctx->pdev->rxq_num; uint16_t rxq_id = pio_packet_hash(p) % nr_rxq; /* hash to specific queue id and enqueue */ @@ -191,9 +192,9 @@ static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_h pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); } -int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts) +ssize_t pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts) { - int res = -1; + ssize_t res = -1; struct pio_pcap_live_device_context *plive_dev_ctx = pdev->entity.pcap_live_dev_ctx; if (nullptr == plive_dev_ctx) { @@ -201,7 +202,7 @@ int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, return res; } - int packet_q_len = nr_pkts; + size_t packet_q_len = nr_pkts; pthread_mutex_lock(&plive_dev_ctx->handle_mutex); res = pcap_dispatch(plive_dev_ctx->pcap_handle, packet_q_len, (pcap_handler)pcap_live_pkt_callback_oneshot, (u_char *)plive_dev_ctx); @@ -213,17 +214,19 @@ int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, } else { struct pio_packet *p = nullptr; - int i = 0; + size_t i = 0; uint32_t q_len = 0; - pthread_mutex_lock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); - do { - p = pio_packet_dequeue(&plive_dev_ctx->pkt_queues[rxq_id]); - q_len = plive_dev_ctx->pkt_queues[rxq_id].len; - pkts[i] = (struct stellar_packet *)p; - printf("rxq_id:%d, i:%d, pkts[i]:%p\n", rxq_id, i, pkts[i]); - i++; - } while ((q_len != 0) && (i < nr_pkts)); - pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); + + if (plive_dev_ctx->pkt_queues[rxq_id].len > 0) { + pthread_mutex_lock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); + do { + p = pio_packet_dequeue(&plive_dev_ctx->pkt_queues[rxq_id]); + q_len = plive_dev_ctx->pkt_queues[rxq_id].len; + pkts[i] = (struct stellar_packet *)p; + i++; + } while ((q_len != 0) && (i < nr_pkts)); + pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q); + } if (q_len == 0) { res = i; @@ -235,9 +238,9 @@ int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, return res; } -int pio_pcap_live_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts) +ssize_t pio_pcap_live_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, size_t nr_pkts) { - int res = -1; + ssize_t res = -1; struct pio_pcap_live_device_context *plive_dev_ctx = pdev->entity.pcap_live_dev_ctx; if (nullptr == plive_dev_ctx) { @@ -246,7 +249,7 @@ int pio_pcap_live_device_send(struct packet_io_device *pdev, uint16_t txq_id, st } pthread_mutex_lock(&plive_dev_ctx->handle_mutex); - for (int i = 0; i < nr_pkts; i++) { + for (size_t i = 0; i < nr_pkts; i++) { 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); } @@ -256,17 +259,15 @@ int pio_pcap_live_device_send(struct packet_io_device *pdev, uint16_t txq_id, st return 0; } -void pio_pcap_live_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint16_t qid, struct stellar_packet **pkts, int nr_pkts) +void pio_pcap_live_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts) { void **pptr_pkts = (void **)pkts; - for (int i = 0; i < nr_pkts; i++) { - printf("before free pptr_pkts[%d]:%p\n", i, pptr_pkts[i]); + for (size_t i = 0; i < nr_pkts; i++) { FREE(pptr_pkts[i]); - printf("after free pptr_pkts[%d]:%p\n", i, pptr_pkts[i]); } } -int pio_pcap_live_instance_create(struct packet_io_instance *pinst) +ssize_t pio_pcap_live_instance_create(struct packet_io_instance *pinst) { if (nullptr == pinst) { log_error(ST_ERR_PIO_PCAP_LIVE_INSTANCE, "invalid pcap live instance pointer."); @@ -290,32 +291,25 @@ void pio_pcap_live_instance_destroy(struct packet_io_instance *pinst) FREE(pinst->entity.pcap_live_inst_ctx); - for (uint32_t i = 0; i < pinst->dev_cnt; i++) { - pio_pcap_live_device_close(pinst->devices[i]); - FREE(pinst->devices[i]); - } + struct packet_io_device *node = nullptr; + while ((node = TAILQ_FIRST(&pinst->device_queue_head)) != nullptr) { + TAILQ_REMOVE(&pinst->device_queue_head, node, next); + pinst->dev_cnt--; + pio_pcap_live_device_close(node); + FREE(node); + } } -void *pio_pcap_live_device_buff_ctrlzone(struct stellar_packet *p) +char *pio_pcap_live_device_buff_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len) { struct pio_packet *pkt = (struct pio_packet *)p; - return pkt->pkt_hdr; + *ctrlzone_len = CUSTOM_ZONE_LEN; + return (char *)pkt->pkt_hdr; } -char *pio_pcap_live_device_buff_mtod(struct stellar_packet *p) +char *pio_pcap_live_device_buff_mtod(struct stellar_packet *p, size_t *data_len) { struct pio_packet *pkt = (struct pio_packet *)p; + *data_len = pkt->pkt_len; return (char *)pkt->pkt_payload; -} - -uint32_t pio_pcap_live_device_buff_buflen(struct stellar_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 stellar_packet *p) -{ - struct pio_packet *pkt = (struct pio_packet *)p; - return (pkt->pkt_len); -} +} \ No newline at end of file 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 6e0e9bb..ed89e4b 100644 --- a/src/packet_io/pcap_live_mode/pio_pcap_live.h +++ b/src/packet_io/pcap_live_mode/pio_pcap_live.h @@ -19,7 +19,6 @@ extern "C" #include #include -#include "global_var.h" #include "packet_io_util.h" #define PCAP_STATE_UP 1 @@ -59,7 +58,7 @@ 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; + struct packet_io_device *pdev; }; /** @@ -69,7 +68,7 @@ struct pio_pcap_live_device_context { * @param wrk_thread_num * @return int */ -int pio_pcap_live_instance_create(struct packet_io_instance *pinst); +ssize_t pio_pcap_live_instance_create(struct packet_io_instance *pinst); /** * @brief @@ -86,26 +85,22 @@ void pio_pcap_live_instance_destroy(struct packet_io_instance *pinst); * pdev->rxq_num: number of the packet receiving queues for the device * pdev->txq_num: number of the packet sending queues for the device */ -int pio_pcap_live_device_open(struct packet_io_device *pdev); +ssize_t 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); +ssize_t pio_pcap_live_device_close(struct packet_io_device *pdev); -int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts); +ssize_t pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, size_t nr_pkts); -int pio_pcap_live_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts); +ssize_t pio_pcap_live_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, size_t nr_pkts); -void pio_pcap_live_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts); +void pio_pcap_live_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, size_t nr_pkts); -void *pio_pcap_live_device_buff_ctrlzone(struct stellar_packet *p); +char *pio_pcap_live_device_buff_ctrlzone(struct stellar_packet *p, size_t *ctrlzone_len); -char *pio_pcap_live_device_buff_mtod(struct stellar_packet *p); - -uint32_t pio_pcap_live_device_buff_buflen(struct stellar_packet *p); - -uint32_t pio_pcap_live_device_buff_datalen(struct stellar_packet *p); +char *pio_pcap_live_device_buff_mtod(struct stellar_packet *p, size_t *data_len); #ifdef __cpluscplus } diff --git a/src/packet_io/test/CMakeLists.txt b/src/packet_io/test/CMakeLists.txt index 949e697..e5ff651 100644 --- a/src/packet_io/test/CMakeLists.txt +++ b/src/packet_io/test/CMakeLists.txt @@ -8,6 +8,7 @@ target_link_libraries( packet_io dl pcap + toml ) include(GoogleTest) diff --git a/src/packet_io/test/gtest_packet_io.cpp b/src/packet_io/test/gtest_packet_io.cpp index 70ccff4..97993d8 100644 --- a/src/packet_io/test/gtest_packet_io.cpp +++ b/src/packet_io/test/gtest_packet_io.cpp @@ -1,15 +1,275 @@ #include +#include "utils.h" +#include "packet.h" #include "packet_io.h" +#include "packet_io_util.h" -TEST(PACKET_IO_Test, packet_io_instance_create) { - struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE); - EXPECT_NE(ppio_inst, nullptr); +TEST(PACKET_IO_Test, packet_io_instance_create_and_destroy) { + struct packet_io_instance *ppio_inst = packet_io_instance_create(nullptr, PACKET_IO_RUN_MODE_PCAP_FILE); + EXPECT_EQ(ppio_inst, nullptr); + ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_MAX); + EXPECT_EQ(ppio_inst, nullptr); + ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE); + EXPECT_NE(ppio_inst, nullptr); + packet_io_fini(ppio_inst); } -TEST(PACKET_IO_Test, packet_io_open_device) { +TEST(PACKET_IO_Test, packet_io_device_open_and_close) { + struct packet_io_device *pdev = packet_io_device_open(nullptr, nullptr, 1, 1); + EXPECT_EQ(pdev, nullptr); + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_LIVE); + EXPECT_NE(ppio_inst, nullptr); + pdev = packet_io_device_open(ppio_inst, nullptr, 1, 1); + EXPECT_EQ(pdev, nullptr); + pdev = packet_io_device_open(ppio_inst, "lo", 1, 1); + EXPECT_NE(pdev, nullptr); + packet_io_device_close(pdev); + packet_io_fini(ppio_inst); +} + +TEST(PACKET_IO_Test, packet_io_device_rx) { struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE); - EXPECT_EQ(packet_io_device_open(ppio_inst, NULL, 1, 1), nullptr); + struct packet_io_device *pdev = packet_io_device_open(ppio_inst, "./src/packet_io/test/test-64.pcapng", 1, 1); + EXPECT_NE(pdev, nullptr); + struct stellar_packet *rx_pkts[64]; + ssize_t fetch_num = packet_io_device_rx(pdev, 0, rx_pkts, 1); + EXPECT_EQ(fetch_num, 1); + packet_io_device_close(pdev); + packet_io_fini(ppio_inst); +} + +TEST(PACKET_IO_Test, packet_io_device_tx) { + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_LIVE); + struct packet_io_device *pdev = packet_io_device_open(ppio_inst, "lo", 1, 1); + EXPECT_NE(pdev, nullptr); + struct stellar_packet *rx_pkts[64]; + ssize_t send_num = packet_io_device_tx(pdev, 0, rx_pkts, 1); + EXPECT_EQ(send_num, 0); + packet_io_device_close(pdev); + packet_io_fini(ppio_inst); +} + +TEST(PACKET_IO_Test, packet_io_pkts_free) { + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE); + struct packet_io_device *pdev = packet_io_device_open(ppio_inst, "./src/packet_io/test/test-64.pcapng", 1, 1); + EXPECT_NE(pdev, nullptr); + struct stellar_packet *rx_pkts[64]; + ssize_t fetch_num = packet_io_device_rx(pdev, 0, rx_pkts, 1); + EXPECT_EQ(fetch_num, 1); + packet_io_pkts_free(pdev, 0, rx_pkts, 1); + packet_io_device_close(pdev); + packet_io_fini(ppio_inst); +} + +TEST(PACKET_IO_Test, get_stellar_packet_ctrlzone) { + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE); + struct packet_io_device *pdev = packet_io_device_open(ppio_inst, "./src/packet_io/test/test-64.pcapng", 1, 1); + EXPECT_NE(pdev, nullptr); + struct stellar_packet *rx_pkts[64]; + ssize_t fetch_num = packet_io_device_rx(pdev, 0, rx_pkts, 1); + EXPECT_EQ(fetch_num, 1); + size_t ctrlzone_len = 0; + void *buff_ctrlzone = get_stellar_packet_ctrlzone(rx_pkts[0], &ctrlzone_len); + EXPECT_EQ(buff_ctrlzone, rx_pkts[0]); + + packet_io_pkts_free(pdev, 0, rx_pkts, 1); + packet_io_device_close(pdev); + packet_io_fini(ppio_inst); +} + +TEST(PACKET_IO_Test, get_stellar_packet_data) { + struct packet_io_instance *ppio_inst = packet_io_instance_create("stellar", PACKET_IO_RUN_MODE_PCAP_FILE); + struct packet_io_device *pdev = packet_io_device_open(ppio_inst, "./src/packet_io/test/test-64.pcapng", 1, 1); + EXPECT_NE(pdev, nullptr); + struct stellar_packet *rx_pkts[64]; + ssize_t fetch_num = packet_io_device_rx(pdev, 0, rx_pkts, 1); + EXPECT_EQ(fetch_num, 1); + size_t data_len = 0; + void *buff_ctrlzone = get_stellar_packet_data(rx_pkts[0], &data_len); + EXPECT_EQ(buff_ctrlzone, (uint8_t *)rx_pkts[0]+64); + + packet_io_pkts_free(pdev, 0, rx_pkts, 1); + packet_io_device_close(pdev); + packet_io_fini(ppio_inst); +} + +TEST(PACKET_IO_UTIL_Test, strncpy_safe) { + ssize_t res = strncpy_safe(nullptr, nullptr, 0); + EXPECT_EQ(res, -1); + + char dst[5]; + res = strncpy_safe(dst, "hello", sizeof(dst)); + EXPECT_STREQ(dst, "hell"); + EXPECT_EQ(res, 0); + + memset(dst, 0, sizeof(dst)); + res = strncpy_safe(dst, "he", sizeof(dst)); + EXPECT_STREQ(dst, "he"); + EXPECT_EQ(res, 0); +} + +TEST(PACKET_IO_UTIL_Test, packet_copy_data) { + uint8_t dst[5]; + uint8_t src[4] = {0x12, 0x34, 0x56, 0x78}; + ssize_t res = packet_copy_data(dst, src, sizeof(src)); + EXPECT_EQ(res, 0); + EXPECT_EQ(dst[0], 0x12); + EXPECT_EQ(dst[1], 0x34); + EXPECT_EQ(dst[2], 0x56); + EXPECT_EQ(dst[3], 0x78); +} + +TEST(PACKET_IO_UTIL_Test, pio_packet_hash) { + struct pio_packet *p = (struct pio_packet *)malloc(MAX_SIZE_OF_PIO_PACKET); + memset(p, 0, MAX_SIZE_OF_PIO_PACKET); + uint64_t res = pio_packet_hash(p); + EXPECT_EQ(res, 0); + FREE(p); +} + +TEST(PACKET_IO_UTIL_Test, pio_packet_queue_init) { + +} + +TEST(PACKET_IO_UTIL_Test, pio_packet_enqueue) { + +} + +TEST(PACKET_IO_UTIL_Test, pio_packet_dequeue) { + +} + +TEST(PACKET_IO_UTIL_Test, release_pio_packet_queue) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_instance_create) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_instance_destroy) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_open) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_close) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_receive) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_send) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_pkt_free) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_buff_ctrlzone) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_buff_mtod) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_buff_buflen) { + +} + +TEST(PACKET_IO_PIO_MARSIO_Test, pio_marsio_device_buff_datalen) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_instance_create) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_instance_destroy) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_open) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_close) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_receive) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_pkt_free) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_buff_ctrlzone) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_buff_mtod) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_buff_buflen) { + +} + +TEST(PACKET_IO_PIO_PCAP_FILE_Test, pio_pcap_file_device_buff_datalen) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_instance_create) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_instance_destroy) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_open) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_close) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_receive) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_send) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_pkt_free) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_buff_ctrlzone) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_buff_mtod) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_buff_buflen) { + +} + +TEST(PACKET_IO_PIO_PCAP_LIVE_Test, pio_pcap_live_device_buff_datalen) { + } int main(int argc, char ** argv) diff --git a/src/packet_io/test/test-64.pcapng b/src/packet_io/test/test-64.pcapng new file mode 100644 index 0000000..eb87132 Binary files /dev/null and b/src/packet_io/test/test-64.pcapng differ