299 lines
9.9 KiB
C
299 lines
9.9 KiB
C
#include "plugin_manager_gtest_mock.h"
|
|
#include "lua_plugin_manage.h"
|
|
|
|
#include <toml.h>
|
|
#include <utarray.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <getopt.h>
|
|
#include <time.h>
|
|
|
|
#define PLUGIN_CONFIG_PATH "./conf/plugin_manage.toml"
|
|
#define LUA_CONFIG_PATH "./conf/lua_plugin_manage.toml"
|
|
|
|
// #define DEBUG_PLUGIN_SCHEMA
|
|
|
|
#define PACKET_COUNT 3
|
|
#define SESSION_COUNT 10
|
|
|
|
struct arg_config
|
|
{
|
|
int session_count;
|
|
int packet_count;
|
|
int wait_time;
|
|
int thread_num;
|
|
unsigned char lua_mod;
|
|
unsigned char session_mod;
|
|
unsigned char print_mod;
|
|
unsigned char time_mod;
|
|
char *config_path;
|
|
};
|
|
|
|
struct arg_config global_arg_config = {SESSION_COUNT, PACKET_COUNT, 10, 1, 1, 1, 1, 1, LUA_CONFIG_PATH};
|
|
|
|
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_num);
|
|
static void parse_args(int argc, char *argv[]);
|
|
// #ifdef DEBUG_PLUGIN_SCHEMA
|
|
static void debug_plugin_manage_specifics(struct lua_config_specific *specifics, int num);
|
|
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema);
|
|
// #endif
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
parse_args(argc, argv);
|
|
if (global_arg_config.print_mod)
|
|
{
|
|
printf("***** ***** ***** ***** ***** *****\n");
|
|
printf("load example config:\n");
|
|
printf("session count is %d\n", global_arg_config.session_count);
|
|
printf("packet count is %d\n", global_arg_config.packet_count);
|
|
printf("thread num is %d\n", global_arg_config.thread_num);
|
|
printf("load config from path %s\n", global_arg_config.config_path);
|
|
printf("***** ***** ***** ***** ***** *****\n\n");
|
|
}
|
|
|
|
clock_t start_clock, end_clock;
|
|
|
|
struct stellar st;
|
|
memset(&st, 0, sizeof(st));
|
|
st.thread_num = global_arg_config.thread_num;
|
|
|
|
/* ***** ***** 进行初始化 ***** ***** */
|
|
struct plugin_manager_schema *plug_mgr = plugin_manager_init(&st, PLUGIN_CONFIG_PATH);
|
|
assert(plug_mgr);
|
|
/* 初始化lua插件 */
|
|
|
|
printf("now wait time to compute memory ... ...\n");
|
|
sleep(global_arg_config.wait_time);
|
|
|
|
struct lua_plugin_manage_schema *lua_schema = NULL;
|
|
if (global_arg_config.lua_mod)
|
|
{
|
|
int num = 0;
|
|
struct lua_config_specific *specific = config_load(LUA_CONFIG_PATH, &num);
|
|
assert(specific);
|
|
if (global_arg_config.print_mod)
|
|
debug_plugin_manage_specifics(specific, num);
|
|
|
|
lua_schema = lua_plugin_manage_init(&st, num, specific);
|
|
st.lua_plug_mgr = lua_schema;
|
|
assert(lua_schema);
|
|
if (specific)
|
|
free(specific);
|
|
|
|
/* 测试单个插件加载函数 */
|
|
struct lua_config_specific add_specific = {NULL, NULL, NULL};
|
|
add_specific.config_specific_file = "./plugin/example_plugin_load.lua";
|
|
add_specific.config_specific_load_func = "plugin_load";
|
|
add_specific.config_specific_unload_func = "plugin_unload";
|
|
lua_plugin_manage_load_one_specific(st.lua_plug_mgr, &add_specific);
|
|
|
|
if (global_arg_config.print_mod)
|
|
debug_lua_plugin_manage_schema(lua_schema);
|
|
|
|
printf("now wait time to compute memory ... ...\n");
|
|
sleep(global_arg_config.wait_time);
|
|
}
|
|
|
|
// #ifdef DEBUG_PLUGIN_SCHEMA
|
|
if (global_arg_config.print_mod)
|
|
debug_plugin_manage_schema(plug_mgr);
|
|
// #endif
|
|
/* ***** ***** 初始化完成 ***** ***** */
|
|
|
|
/* ***** ***** 会话相关测试 ***** ***** */
|
|
if (global_arg_config.session_mod)
|
|
{
|
|
unsigned char ip_proto = 6;
|
|
struct packet pkt = {&st, TCP, ip_proto};
|
|
struct session *sess = (struct session *)calloc(global_arg_config.session_count, sizeof(struct session));
|
|
memset(sess, 0, global_arg_config.session_count * sizeof(struct session));
|
|
|
|
/* 创建session */
|
|
for (int i = 0; i < global_arg_config.session_count; i++)
|
|
{
|
|
sess[i].plug_mgr_rt = plugin_manager_session_runtime_new(plug_mgr, &sess[i]);
|
|
sess[i].type = SESSION_TYPE_TCP;
|
|
}
|
|
|
|
clock_t total_clock = 0;
|
|
for (int j = 0; j < global_arg_config.packet_count; j++)
|
|
{
|
|
plugin_manager_on_packet_ingress(plug_mgr, &pkt);
|
|
|
|
for (int i = 0; i < global_arg_config.session_count; i++)
|
|
{
|
|
sess[i].sess_pkt_cnt += 1;
|
|
if (global_arg_config.time_mod)
|
|
start_clock = clock();
|
|
plugin_manager_on_session_ingress(&sess[i], &pkt);
|
|
plugin_manager_on_session_egress(&sess[i], &pkt);
|
|
if (global_arg_config.time_mod)
|
|
{
|
|
end_clock = clock();
|
|
total_clock += (end_clock - start_clock);
|
|
}
|
|
}
|
|
|
|
if (global_arg_config.time_mod)
|
|
{
|
|
printf("for session count %d, total time is %ld\n", global_arg_config.session_count, total_clock);
|
|
printf("trans to second, time is %f\n", ((double)total_clock / CLOCKS_PER_SEC));
|
|
}
|
|
|
|
plugin_manager_on_packet_egress(plug_mgr, &pkt);
|
|
}
|
|
|
|
/* 释放session */
|
|
for (int i = 0; i < global_arg_config.session_count; i++)
|
|
{
|
|
plugin_manager_on_session_closing(&sess[i]);
|
|
plugin_manager_session_runtime_free(sess[i].plug_mgr_rt);
|
|
}
|
|
}
|
|
/* ***** ***** 会话相关完成 ***** ***** */
|
|
|
|
/* ***** ***** 释放 ***** ***** */
|
|
lua_plugin_manage_exit(lua_schema);
|
|
plugin_manager_exit(plug_mgr);
|
|
/* ***** ***** 释放 ***** ***** */
|
|
return 0;
|
|
}
|
|
|
|
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_count)
|
|
{
|
|
if (__glibc_unlikely(!config_file_name))
|
|
return NULL;
|
|
int specific_num = 0;
|
|
char errbuff[256] = {0};
|
|
|
|
if (access(config_file_name, F_OK))
|
|
return NULL;
|
|
FILE *fp = fopen(config_file_name, "r");
|
|
if (!fp)
|
|
return NULL;
|
|
toml_table_t *conf = toml_parse_file(fp, errbuff, sizeof(errbuff));
|
|
if (fp)
|
|
fclose(fp);
|
|
if (!conf)
|
|
{
|
|
printf("parse config file failed, filename %s, err %s\n", config_file_name, errbuff);
|
|
return NULL;
|
|
}
|
|
|
|
toml_array_t *plugin_array = toml_array_in(conf, "plugin");
|
|
if (!plugin_array)
|
|
return NULL;
|
|
|
|
specific_num = toml_array_nelem(plugin_array);
|
|
struct lua_config_specific *new_spec = (struct lua_config_specific *)calloc(specific_num, sizeof(struct lua_config_specific));
|
|
if (!new_spec)
|
|
return NULL;
|
|
struct lua_config_specific *specific = NULL;
|
|
|
|
for (int i = 0; i < specific_num; ++i)
|
|
{
|
|
toml_table_t *plugin = toml_table_at(plugin_array, i);
|
|
const char *raw_filepath = toml_raw_in(plugin, "path");
|
|
const char *raw_load_func_name = toml_raw_in(plugin, "init");
|
|
const char *raw_unload_func_name = toml_raw_in(plugin, "exit");
|
|
specific = &new_spec[i];
|
|
|
|
if (toml_rtos(raw_filepath, &specific->config_specific_file) ||
|
|
toml_rtos(raw_load_func_name, &specific->config_specific_load_func) ||
|
|
toml_rtos(raw_unload_func_name, &specific->config_specific_unload_func))
|
|
{
|
|
toml_free(conf);
|
|
free(specific);
|
|
return NULL;
|
|
}
|
|
}
|
|
*specific_count = specific_num;
|
|
|
|
return new_spec;
|
|
}
|
|
|
|
static void parse_args(int argc, char *argv[])
|
|
{
|
|
int opt;
|
|
static const char *shortopts = "s:p:w:c:t:lnde";
|
|
static struct option longopts[] = {
|
|
{"session_count", required_argument, NULL, 's'},
|
|
{"packet_count", required_argument, NULL, 'p'},
|
|
{"wait_time", required_argument, NULL, 'w'},
|
|
{"config_path", required_argument, NULL, 'c'},
|
|
{"thread_num", required_argument, NULL, 't'},
|
|
{"disable_lua", no_argument, NULL, 'l'},
|
|
{"disable_session", no_argument, NULL, 'n'},
|
|
{"print_debug", no_argument, NULL, 'd'},
|
|
{"disable_time", no_argument, NULL, 'e'},
|
|
{0, 0, 0, 0},
|
|
};
|
|
|
|
while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
|
|
{
|
|
switch (opt)
|
|
{
|
|
case 's':
|
|
global_arg_config.session_count = atoi(optarg);
|
|
break;
|
|
case 'p':
|
|
global_arg_config.packet_count = atoi(optarg);
|
|
break;
|
|
case 'w':
|
|
global_arg_config.wait_time = atoi(optarg);
|
|
break;
|
|
case 'c':
|
|
global_arg_config.config_path = strdup(optarg);
|
|
break;
|
|
case 't':
|
|
global_arg_config.thread_num = atoi(optarg);
|
|
break;
|
|
case 'l':
|
|
global_arg_config.lua_mod = 0;
|
|
break;
|
|
case 'n':
|
|
global_arg_config.session_mod = 0;
|
|
break;
|
|
case 'd':
|
|
global_arg_config.print_mod = 1;
|
|
break;
|
|
case 'e':
|
|
global_arg_config.time_mod = 0;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// #ifdef DEBUG_PLUGIN_SCHEMA
|
|
static void debug_plugin_manage_specifics(struct lua_config_specific *specifics, int num)
|
|
{
|
|
printf("***** ***** ***** ***** ***** *****\n");
|
|
printf("load config specifics count is %d\n", num);
|
|
for (int i = 0; i < num; ++i)
|
|
{
|
|
printf("[%d]file path: %s\n", i, specifics[i].config_specific_file);
|
|
printf("[%d]load func: %s\n", i, specifics[i].config_specific_load_func);
|
|
printf("[%d]unload func: %s\n", i, specifics[i].config_specific_unload_func);
|
|
}
|
|
printf("***** ***** ***** ***** ***** *****\n\n");
|
|
}
|
|
|
|
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema)
|
|
{
|
|
struct registered_session_plugin_schema *plugin = NULL;
|
|
for (int i = 0; i < (int)utarray_len(schema->registered_session_plugin_array); ++i)
|
|
{
|
|
plugin = (struct registered_session_plugin_schema *)utarray_eltptr(schema->registered_session_plugin_array, (unsigned int)i);
|
|
printf("plugin[%d]: new func %p, free func %p, env %p\n", i, plugin->on_ctx_new, plugin->on_ctx_free, plugin->plugin_env);
|
|
}
|
|
printf("\n");
|
|
return;
|
|
}
|
|
// #endif
|