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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
#include "mrapp.h"
#include <assert.h>
#include <marsio.h>
#include <pthread.h>
#include <rte_ether.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char appsym[64] = "mrfwd";
static char dev_1_symbol[64] = "meth0";
cpu_set_t cpu_set_io;
unsigned int nr_thread;
struct mr_instance * mr_instance = NULL;
struct mr_vdev * dev_1_handler = NULL;
struct mr_sendpath * to_dev_1_sendpath = NULL;
#define BURST_MAX 64
unsigned int nr_burst = 32;
/* options */
unsigned int opt_dump_packet_metadata = 1;
unsigned int opt_inject_by_deep_copy = 0;
int str_uint_range_parse(const char * str, unsigned int * out, const size_t size);
static int mr_parse_corelist(const char * corelist, int * cores);
void dump_packet_metadata(marsio_buff_t * buff)
{
#if 0
sid_t sids[MR_SID_LIST_MAXLEN];
int nr_sids = marsio_buff_get_sid_list(buff, sids, MR_SID_LIST_MAXLEN);
fprintf(stderr, "packet buf = %p, is_vlan_tci_set = %u, vlan_tci = %u\n", buff, is_vlan_tci_set, vlan_tci);
#endif
}
void * l2fwd_loop(void * arg)
{
uintptr_t sid = (uintptr_t)arg;
marsio_buff_t * rx_buff[BURST_MAX];
marsio_buff_t * tx_buff[BURST_MAX];
int ret = 0;
marsio_thread_init(mr_instance);
for (;;)
{
ret = marsio_recv_burst(dev_1_handler, sid, rx_buff, (int)nr_burst);
if (ret == 0)
{
marsio_poll_wait(mr_instance, &dev_1_handler, 1, sid, -1);
}
for (int i = 0; i < ret; i++)
{
if (marsio_dp_trace_measurements_can_emit(mr_instance, rx_buff[i], DP_TRACE_MEASUREMENT_TYPE_TELEMETRY))
{
marsio_dp_trace_measurement_emit_str(mr_instance, rx_buff[i], DP_TRACE_MEASUREMENT_TYPE_TELEMETRY,
"I2fwd-nf", "test data path trace telemetry");
marsio_dp_trace_measurement_emit_str(mr_instance, rx_buff[i], DP_TRACE_MEASUREMENT_TYPE_TELEMETRY,
"I2fwd-nf", "test data path trace telemetry 2");
}
if (marsio_dp_trace_measurements_can_emit(mr_instance, rx_buff[i], DP_TRACE_MEASUREMENT_TYPE_TRACE))
{
marsio_dp_trace_measurement_emit_fmt(mr_instance, rx_buff[i], DP_TRACE_MEASUREMENT_TYPE_TRACE,
"I2fwd-nf", "test format recod %s", "hello");
marsio_dp_trace_measurement_emit_fmt(mr_instance, rx_buff[i], DP_TRACE_MEASUREMENT_TYPE_TRACE,
"I2fwd-nf", "test format recod %s", "hello 2");
}
if (opt_dump_packet_metadata)
{
dump_packet_metadata(rx_buff[i]);
}
if (opt_inject_by_deep_copy)
{
marsio_buff_t * orin_buff = rx_buff[i];
marsio_buff_t * deep_copy_buff = NULL;
marsio_buff_malloc_global(mr_instance, &deep_copy_buff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY);
/* copy the packet body */
unsigned int orin_pkt_len = marsio_buff_datalen(orin_buff);
const char * orin_pkt_ptr = marsio_buff_mtod(orin_buff);
char * deep_copy_ptr = marsio_buff_append(deep_copy_buff, orin_pkt_len);
memcpy(deep_copy_ptr, orin_pkt_ptr, orin_pkt_len);
/* get the route ctx */
char route_ctx[64];
marsio_buff_get_metadata(orin_buff, MR_BUFF_ROUTE_CTX, route_ctx, sizeof(route_ctx));
/* get the sids */
sid_t sids[MR_SID_LIST_MAXLEN];
int nr_sids = marsio_buff_get_sid_list(orin_buff, sids, MR_SID_LIST_MAXLEN);
#if 0
struct rte_ether_hdr * ether_hdr = (struct rte_ether_hdr *)orin_pkt_ptr;
if(ether_hdr->ether_type != 0xAAAA)
{
assert(nr_sids != 0);
}
#endif
/* set route ctx and sids for deep copy buff */
marsio_buff_set_metadata(deep_copy_buff, MR_BUFF_ROUTE_CTX, route_ctx, sizeof(route_ctx));
marsio_buff_set_sid_list(deep_copy_buff, sids, nr_sids);
if (marsio_dp_trace_measurements_can_emit(mr_instance, deep_copy_buff, DP_TRACE_MEASUREMENT_TYPE_TRACE))
{
marsio_dp_trace_measurement_emit_str(mr_instance, deep_copy_buff, DP_TRACE_MEASUREMENT_TYPE_TRACE,
"I2fwd-nf", "test for sending message directly");
}
tx_buff[i] = deep_copy_buff;
}
}
if (opt_inject_by_deep_copy)
{
marsio_buff_free(mr_instance, rx_buff, ret, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY);
marsio_send_burst(to_dev_1_sendpath, sid, tx_buff, (int)ret);
}
else
{
marsio_send_burst(to_dev_1_sendpath, sid, rx_buff, (int)ret);
}
}
}
int help()
{
fprintf(stderr, "parameter error.\n");
exit(EXIT_SUCCESS);
}
void signalHandler(int signum)
{
printf("Received CTRL+C signal (SIGINT)\n");
exit(0);
}
int main(int argc, char * argv[])
{
if (signal(SIGINT, signalHandler) == SIG_ERR)
{
perror("Error setting up signal handler");
return 1;
}
int opt = 0;
while ((opt = getopt(argc, argv, "s:d:a:c:l:b:h?")) != -1)
{
char * endptr = NULL;
switch (opt)
{
case '?':
case 'h': {
help();
break;
}
case 's': {
snprintf(dev_1_symbol, sizeof(dev_1_symbol), "%s", optarg);
break;
}
case 'a': {
snprintf(appsym, sizeof(appsym), "%s", optarg);
break;
}
case 'c': {
uint64_t cpu_mask = strtoull(optarg, &endptr, 0);
if (cpu_mask == 0 && endptr == optarg)
help();
CPU_ZERO(&cpu_set_io);
for (unsigned long bit_iter = 0; bit_iter < sizeof(cpu_mask) * 8; bit_iter++)
{
if ((cpu_mask & (1ULL << bit_iter)))
{
CPU_SET(bit_iter, &cpu_set_io);
}
}
break;
}
case 'l': {
int lcore_indexes[RTE_MAX_LCORE];
if (mr_parse_corelist(optarg, lcore_indexes) < 0)
{
fprintf(stderr, "%s\n", "invalid core list syntax");
help();
}
CPU_ZERO(&cpu_set_io);
for (unsigned int i = 0; i < RTE_MAX_LCORE; i++)
{
if (lcore_indexes[i] == -1)
{
continue;
}
CPU_SET(i, &cpu_set_io);
}
break;
}
case 'b': {
nr_burst = strtoull(optarg, &endptr, 0);
if (nr_burst == 0 && endptr == optarg)
help();
break;
}
default:
help();
break;
}
}
if (CPU_COUNT(&cpu_set_io) == 0)
{
fprintf(stderr, "please set the io cores parameter.\n");
help();
}
mr_instance = marsio_create();
if (mr_instance == NULL)
{
fprintf(stderr, "Marsio instance create failed. ");
abort();
}
unsigned int opt_value = 1;
marsio_option_set(mr_instance, MARSIO_OPT_EXIT_WHEN_ERR, &opt_value, sizeof(opt_value));
marsio_option_set(mr_instance, MARSIO_OPT_THREAD_MASK_IN_CPUSET, &cpu_set_io, sizeof(cpu_set_io));
marsio_init(mr_instance, appsym);
nr_thread = CPU_COUNT(&cpu_set_io);
dev_1_handler = marsio_open_device(mr_instance, dev_1_symbol, nr_thread, nr_thread);
to_dev_1_sendpath = marsio_sendpath_create_by_vdev(dev_1_handler);
fprintf(stdout, "Thread Count = %d\n", nr_thread);
pthread_t tmp_pid[nr_thread];
for (int i = 0; i < nr_thread; i++)
{
pthread_create(&tmp_pid[i], NULL, l2fwd_loop, (void *)(uintptr_t)i);
}
for (int i = 0; i < nr_thread; i++)
{
pthread_join(tmp_pid[i], NULL);
}
marsio_destory(mr_instance);
fprintf(stdout, "L2FWD is terminated. ");
return 0;
}
// copy from lib/eal/common/eal_common_options.c
static int check_core_list(int * lcores, unsigned int count)
{
char lcorestr[RTE_MAX_LCORE * 10];
bool overflow = false;
int len = 0, ret;
unsigned int i;
for (i = 0; i < count; i++)
{
if (lcores[i] < RTE_MAX_LCORE)
continue;
fprintf(stdout, "lcore %d >= RTE_MAX_LCORE (%d)", lcores[i], RTE_MAX_LCORE);
overflow = true;
}
if (!overflow)
return 0;
/*
* We've encountered a core that's greater than RTE_MAX_LCORE,
* suggest using --lcores option to map lcores onto physical cores
* greater than RTE_MAX_LCORE.
*/
for (i = 0; i < count; i++)
{
ret = snprintf(&lcorestr[len], sizeof(lcorestr) - len, "%d@%d,", i, lcores[i]);
if (ret > 0)
len = len + ret;
}
if (len > 0)
lcorestr[len - 1] = 0;
fprintf(stdout,
"To use high physical core ids, "
"please use --lcores to map them to lcore ids below RTE_MAX_LCORE, "
"e.g. --lcores %s",
lcorestr);
return -1;
}
static int mr_parse_corelist(const char * corelist, int * cores)
{
unsigned int count = 0, i;
int lcores[RTE_MAX_LCORE];
char * end = NULL;
int min, max;
int idx;
for (idx = 0; idx < RTE_MAX_LCORE; idx++)
cores[idx] = -1;
/* Remove all blank characters ahead */
while (isblank(*corelist))
corelist++;
/* Get list of cores */
min = -1;
do
{
while (isblank(*corelist))
corelist++;
if (*corelist == '\0')
return -1;
errno = 0;
idx = strtol(corelist, &end, 10);
if (errno || end == NULL)
return -1;
if (idx < 0)
return -1;
while (isblank(*end))
end++;
if (*end == '-')
{
min = idx;
}
else if ((*end == ',') || (*end == '\0'))
{
max = idx;
if (min == -1)
min = idx;
for (idx = min; idx <= max; idx++)
{
bool dup = false;
/* Check if this idx is already present */
for (i = 0; i < count; i++)
{
if (lcores[i] == idx)
dup = true;
}
if (dup)
continue;
if (count >= RTE_MAX_LCORE)
{
fprintf(stdout, "Too many lcores provided. Cannot exceed RTE_MAX_LCORE (%d)", RTE_MAX_LCORE);
return -1;
}
lcores[count++] = idx;
}
min = -1;
}
else
return -1;
corelist = end + 1;
} while (*end != '\0');
if (count == 0)
return -1;
if (check_core_list(lcores, count))
return -1;
/*
* Now that we've got a list of cores no longer than RTE_MAX_LCORE,
* and no lcore in that list is greater than RTE_MAX_LCORE, populate
* the cores array.
*/
do
{
count--;
cores[lcores[count]] = count;
} while (count != 0);
return 0;
}
#if 0
// Remove the following code later
int core_list_parse(const char * str)
{
unsigned int io_cores[128] = {};
int nr_io_cores = 0;
nr_io_cores = str_uint_range_parse(str, io_cores, sizeof(io_cores) / sizeof(io_cores[0]));
if (nr_io_cores < 0)
{
return -1;
}
CPU_ZERO(&cpu_set_io);
for (unsigned int i = 0; i < nr_io_cores; i++)
{
CPU_SET(io_cores[i], &cpu_set_io);
}
return 0;
}
int str_uint_range_parse(const char * str, unsigned int * out, const size_t size)
{
// The strtok() is not reentrant function. It is not recommended.
int ret = 0;
char * line = strdup(str);
char * saveptr;
int num_cnt = 0;
char * currnum = strtok_r(line, " ,", &saveptr);
while (currnum != NULL)
{
if (num_cnt >= size)
{
ret = -1;
goto end;
}
out[num_cnt++] = atoi(currnum);
currnum = strtok_r(NULL, " ,", &saveptr);
}
ret = num_cnt;
end:
free(line);
return ret;
}
#endif
|