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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <rte_log.h>
#include <mr_common.h>
#include <mr_core.h>
#include <rte_malloc.h>
#include <mr_runtime.h>
#include <sk_stack.h>
#include <mrapp.h>
#include <marsio.h>
#include "mr_mask.h"
/* ��־λ���Ƿ��Ѿ������˳�ʼ��*/
int mrapp_inited = 0;
/* ��־λ�����߳��Ƿ��Ѿ������˳�ʼ�� */
int __thread mrapp_thread_inited = 0;
/* ���������Ľṹ */
struct mrapp_instance * instance = NULL;
/* ������Ϣ */
struct mrapp_config mrapp_config;
#define MR_LIB_MAX_EAL_ARGC 128
/* �Command���� */
static void __write_arg(char * eal_argv[], unsigned int * eal_argc,
unsigned int max_argc, const char * value)
{
assert(max_argc >= *eal_argc);
char * mem = (char *)malloc(MR_STRING_MAX * sizeof(char));
assert(mem != NULL);
snprintf(mem, MR_STRING_MAX * sizeof(char), "%s", value);
eal_argv[(*eal_argc)++] = mem;
return;
}
#define WRITE_ARG(x) \
do {__write_arg(eal_argv, &eal_argc, MR_LIB_MAX_EAL_ARGC, x);} \
while(0)
extern int mrapp_init_open_raw_device(struct mrapp_instance * instance);
extern void mrapp_destory_close_raw_device(struct mrapp_instance * instance);
/* EAL������ʼ�� */
static void mrapp_eal_init(struct mrapp_config * config)
{
if (config->logstream == NULL) config->logstream = stderr;
rte_openlog_stream(config->logstream);
char * eal_argv[MR_LIB_MAX_EAL_ARGC];
unsigned int eal_argc = 0;
WRITE_ARG("mrlib");
WRITE_ARG("-c");
WRITE_ARG("0x1");
WRITE_ARG("--proc-type=secondary");
int ret = rte_eal_init(eal_argc, eal_argv);
MR_CHECK(ret >= 0, "Cannot init EAL Enviorment, Failed");
return;
}
#define MRAPP_MONIT_FILE_PATH "/tmp/.mrmonit.app.%s"
struct mrapp_instance * mrapp_instance_create(const char * appsym, struct mrapp_config * config)
{
struct mrapp_instance * _instance = rte_zmalloc(NULL, sizeof(struct mrapp_instance), 0);
MR_CHECK(_instance != NULL, "Cannot alloc memory for mrapp instance");
snprintf(_instance->appsym, sizeof(instance->appsym), "%s", appsym);
snprintf(_instance->monit_file_path, sizeof(_instance->monit_file_path),
MRAPP_MONIT_FILE_PATH, _instance->appsym);
_instance->config = *config;
return _instance;
}
void mrapp_instance_destory(struct mrapp_instance * instance)
{
rte_free(instance);
return;
}
static int mrapp_config_check(const char * appsym, struct mrapp_config * config)
{
// �û�û��ָ���߳������Ӻ����������
if (config->nr_thread == 0) config->nr_thread = mask_popcnt(config->coremask);
// û��ָ���ա����������շ��������߳���һ��
config->nr_raw_rxstream = config->nr_raw_rxstream == 0 ?
config->nr_thread : config->nr_raw_rxstream;
config->nr_raw_txstream = config->nr_raw_txstream == 0 ?
config->nr_thread : config->nr_raw_txstream;
config->nr_stack_rxstream = config->nr_stack_rxstream == 0 ?
config->nr_thread : config->nr_stack_rxstream;
config->nr_stack_txstream = config->nr_stack_txstream == 0 ?
config->nr_thread : config->nr_stack_txstream;
// �������
if(config->nr_thread <= 0)
{
MR_LOG(ERR, MRLIB, "Invalid thread number or coremask(thread number<=0). \n");
return -EINVAL;
}
if(config->nr_raw_rxstream > config->nr_thread)
{
MR_LOG(ERR, MRLIB, "Invalid raw rxstream number(>thread number). \n");
return -EINVAL;
}
if(config->nr_raw_txstream > config->nr_thread)
{
MR_LOG(ERR, MRLIB, "Invalid raw txstream number(>thread number). \n");
return -EINVAL;
}
if (config->nr_stack_rxstream > config->nr_thread)
{
MR_LOG(ERR, MRLIB, "Invalid stack rxstream number(>thread number). \n");
return -EINVAL;
}
if (config->nr_stack_txstream > config->nr_thread)
{
MR_LOG(ERR, MRLIB, "Invalid stack txstream number(>thread number). \n");
return -EINVAL;
}
// ��ӡ����
MR_LOG(INFO, MRLIB, "Marsio Config: \n");
MR_LOG(INFO, MRLIB, " Dataplane Thread Count: %d\n", config->nr_thread);
MR_LOG(INFO, MRLIB, " Raw rxstream: %d\n", config->nr_raw_rxstream);
MR_LOG(INFO, MRLIB, " Raw txstream: %d\n", config->nr_raw_txstream);
MR_LOG(INFO, MRLIB, " Stack rxstream: %d\n", config->nr_stack_rxstream);
MR_LOG(INFO, MRLIB, " Stack txstream: %d\n", config->nr_stack_txstream);
return 0;
}
void * mrapp_loop_thread(void * args)
{
struct mrapp_instance * _instance = (struct mrapp_instance *)args;
pthread_detach(pthread_self());
while (1)
{
mrapp_monit_loop(_instance);
sleep(1);
}
}
static int mrapp_init(const char * appsym, struct mrapp_config * config)
{
// �����
int ret = mrapp_config_check(appsym, config);
if(ret < 0)
{
MR_LOG(ERR, MRLIB, "Marsio config check failed. \n");
return ret;
}
// Ӧ��ע��
ret = mr_app_register(appsym, config->coremask, config->autoexit);
if (ret < 0)
{
MR_LOG(ERR, MRLIB, "Cannot register application %s. \n", appsym);
return -1;
}
MR_LOG(INFO, MRLIB, "Application %s register success. \n", appsym);
// ������̵�Privָ�벻Ϊ�գ�˵����ǰ���й����ָ�������
if ((instance = mr_app_priv_get()) != NULL)
mrapp_instance_destory(instance);
instance = mrapp_instance_create(appsym, config);
assert(instance != NULL);
// ��ȫ��Core���
struct mr_global_ctx * g_ctx = mr_global_ctx_get();
mr_core_default_instance_set(g_ctx->ctx_core);
instance->core_instance = g_ctx->ctx_core;
// ע��Э��ջ���
struct sk_app_instance * sk_app_instance = sk_app_instance_create(
g_ctx->ctx_stack, instance->appsym);
if (sk_app_instance == NULL)
{
MR_LOG(ERR, MRLIB, "Cannot create stack app instance for app %s. \n",
instance->appsym);
return -1;
}
sk_default_instance_set(g_ctx->ctx_stack, sk_app_instance);
instance->sk_app_instance = sk_app_instance;
ret = mrapp_init_open_raw_device(instance);
if (ret < 0)
{
MR_LOG(ERR, MRLIB, "Open raw devices failed. \n");
return ret;
}
pthread_t pid_loop;
pthread_create(&pid_loop, NULL, mrapp_loop_thread, instance);
return 0;
}
int __option_set_raw_device(struct mrapp_config * config, const char * devsym)
{
if (config->nr_raw_device >= MR_DEVICE_MAX)
return -ENOMEM;
struct __devinfo * p_devinfo = &config->raw_open_device[config->nr_raw_device];
snprintf(p_devinfo->devsym, sizeof(p_devinfo->devsym), "%s", devsym);
p_devinfo->dev_id = config->nr_raw_device;
config->nr_raw_device++;
return p_devinfo->dev_id;
}
int __option_set_stack_device(struct mrapp_config * config, const char * devsym)
{
if (config->nr_stack_device >= MR_DEVICE_MAX)
return -ENOMEM;
struct __devinfo * p_devinfo = &config->stack_open_device[config->nr_stack_device];
snprintf(p_devinfo->devsym, sizeof(p_devinfo->devsym), "%s", devsym);
p_devinfo->dev_id = config->nr_raw_device;
config->nr_stack_device++;
return p_devinfo->dev_id;
}
int marsio_option_set(const char * appsym, marsio_opt_type_t opt_type, void * opt, size_t sz_opt)
{
#define __CHECK_USER_PARAM(expect_type) do { \
if(sz_opt < sizeof(expect_type)) { return -EINVAL; } \
} while(0)
struct mrapp_config * config = &mrapp_config;
int ret = 0;
switch(opt_type)
{
case MARSIO_OPT_THREAD_NUM:
__CHECK_USER_PARAM(unsigned int);
config->nr_thread = *(unsigned int *)opt;
ret = 0; break;
case MARSIO_OPT_THREAD_MASK:
__CHECK_USER_PARAM(uint64_t);
config->coremask = *(uint64_t *)opt;
ret = 0; break;
case MARSIO_OPT_AUTOEXIT:
__CHECK_USER_PARAM(unsigned int);
config->autoexit = *(unsigned int *)opt;
ret = 0; break;
case MARSIO_OPT_FILE_LOG:
__CHECK_USER_PARAM(FILE *);
config->logstream = (FILE *)opt;
ret = 0; break;
case MARSIO_OPT_RAW_RX_STREAM_NUM:
__CHECK_USER_PARAM(unsigned int);
config->nr_raw_rxstream = *(unsigned int *)opt;
ret = 0; break;
case MARSIO_OPT_RAW_TX_STREAM_NUM:
__CHECK_USER_PARAM(unsigned int);
config->nr_raw_txstream = *(unsigned int *)opt;
ret = 0; break;
case MARSIO_OPT_STACK_RX_STREAM_NUM:
__CHECK_USER_PARAM(unsigned int);
config->nr_stack_rxstream = *(unsigned int *)opt;
ret = 0; break;
case MARSIO_OPT_STACK_TX_STREAM_NUM:
__CHECK_USER_PARAM(unsigned int);
config->nr_stack_txstream = *(unsigned int *)opt;
ret = 0; break;
case MARSIO_OPT_RAW_DEVICE:
__CHECK_USER_PARAM(char *);
ret = __option_set_raw_device(config, (const char *)opt);
break;
case MARSIO_OPT_STACK_DEVICE:
__CHECK_USER_PARAM(char *);
ret = __option_set_stack_device(config, (const char *)opt);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
int marsio_option_get(const char * appsym, int opt_type, void * out_opt, size_t out_opt_buffer)
{
return 0;
}
int marsio_init(const char * appsym)
{
mrapp_eal_init(&mrapp_config);
return mrapp_init(appsym, &mrapp_config);
}
int marsio_thread_init()
{
mr_thread_register();
return 0;
}
int marsio_destory()
{
mrapp_destory_close_raw_device(instance);
return 0;
}
void marsio_on_exit_register(void(*exit_fn)(void * arg), void * arg)
{
mr_on_exit_register(exit_fn, arg);
}
|