1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#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
|