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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
/*
**********************************************************************************************
* File: packet_io_internal.cpp
* Description:
* Authors: Liu WenTan <[email protected]>
* Date: 2022-07-15
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include <string.h>
#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) + 1);
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);
}
|