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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
|
#include "sapp_api.h"
#include "sapp_private_api.h"
#include "support/MESA_feedback.h"
/*
2015-04-15
ƽ̨ʵʱ������������ݰ�, ��sizeָ����С, �������100MB,
ÿ���̷߳�Ϊ�����ļ�, ѭ��д��,
�������:
pid.thread_seq.[0-1].pcap,
0�ļ�����100MB��ֹͣ, ת����ʼд1�ļ�, 1�ļ�Ҳ����100MB��, ����������д0�ļ�.
ͨ���ܿ��ؿ��ƴ˹����Ƿ���, ���鿴��ǰĿ¼�м���core, ��������д, ��ֹӲ����!
debug_pkt_dump_switch=1
debug_pkt_dump_max_num=5 //����core�ļ�
debug_pkt_dump_max_size_per_thread=100 // ��λMB
����һ������, ��debug_pkt_dump,
sapp��ʼ��ʱ���߳���, ����THREAD_NUM���ܵ�, Ȼ��fork����, �ӽ��̱�Ϊdebug_pkt_dump,
�����̼�������sapp,
��mesa_default_pkt_cb������, ���յ��İ�ͨ���ܵ�����debug_pkt_dump����.
debug_pkt_dump����, Ҳͬ������ͬ���߳�����������, д�ļ�.
*/
#ifdef __cplusplus
extern "C" {
#endif
int gdev_init(void);
extern int packet_io_status_init(void);
extern MESA_send_handle g_send_handle[MAX_THREAD_NUM];
char g_send_dev_name[DEV_NAME_STR_LEN]; /* ���������� */
unsigned char g_send_dev_mac[MAC_ADDR_LEN];
unsigned char g_send_gateway_mac[MAC_ADDR_LEN]; /* ͨ���ֹ����û��ѯ·�ɱ���ARP���õ� */
unsigned char g_send_gateway_mac_table[64][16][18];
static int G_SND_ROUTE_INFO_NUM = 0;
unsigned char g_send_gdev_ip_table[64][32];
static int G_SND_GDEV_INFO_NUM = 0;
char g_up_dev_name[DEV_NAME_STR_LEN]; /* ����ģʽĬ�ϲ������� */
char g_down_dev_name[DEV_NAME_STR_LEN];
//static char g_cap_filter[CAP_FILTER_STR_LEN];
//static int g_cap_buf_queue_num = 10000; /* IO�̺߳ʹ����߳�֮��Ļ�����г��� */
//int g_topology_config_raw_mode = 1;/* main.conf �����ļ���ԭʼֵ */
//int g_topology_mode = NET_CONN_PARALLEL; /* ͨ��topology_mode_convert()ת�����ƽ̨�ڲ�ֵ */
//int g_packet_io_cap_mode = CAP_MODEL_PCAP_ONLINE;
///int g_packet_io_cap_level = CAP_LEVEL_MAC; /* ������ʼλ��,Ĭ��MAC�� */
//int g_packet_io_thread_num = 1;
int g_app_send_rst_type;
//extern int g_timestamp_record_sw;
int g_encapsulate_with_ddp = 0; /* ʹ��DDPЭ���װ��ԭʼ�� */
int g_encapsulate_with_L2E = 0; /* ʹ��DDPЭ���װ��x27ԭʼIP�� */
/*
2015-12-10 lijia add, ����eth��, ���账��MAC��ַ, ���贴����ʱCOPY�����ṹ�� ,
ԭʼ����IP�㿪ʼ.
*/
int g_skip_ethernet_layer_sw = 0;
void *g_packet_dl_send_handle[MAX_THREAD_NUM];/* ��̬IO��ķ������ */
//static int g_packet_io_dir; /* ���ڼ�¼��ǰ��������, �����ʱʹ�� */
int G_SKIP_NOT_IP_LAYER = 0; /* ������IPЭ��IJ�, ����˫ջ�����������, RST������ */
static int mesa_default_pkt_cb(const raw_pkt_t *p_raw_pkt, unsigned char dir, int thread_num);
PACKET_IO_CB_T G_DEFAULT_PKT_CB = mesa_default_pkt_cb;
dl_io_fun_list_t dl_io_fun_list;
static int use_custom_pkt_cb = 0; /* �Ƿ������µ�callback���� */
long long g_timedelay_threshold = 10000000; /* ��λCPU������, ��ʱ�� */
int g_use_MESA_sleep_sw = 0;
int g_raw_pkt_broken_check = 0; /* ���ԭʼ���Ƿ��� */
static UINT8 *g_send_buf_pool[MAX_THREAD_NUM];
#define MAX_RAW_PKT_TARGET_NUM (128)
typedef struct{
unsigned int target_id; /* Ŀ��ID */
send_raw_args_t send_args[__CAP_MODEL_MAX];
}send_raw_pkt_info_t;
static send_raw_pkt_info_t G_SND_RAW_PKT_INFO[MAX_RAW_PKT_TARGET_NUM];
static int G_SND_RAW_PKT_INFO_NUM = 0;
void packet_io_exit(void);
int packet_io_lib_load(int cap_mode);
void del_last_rn(char *data, int max_len);
int parse_send_route_conf(const char *profile_path);
int parse_send_gdev_ip_conf(const char *profile_path);
extern void cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt);
extern char *MESA_MD5_sum_bin(unsigned char *raw_data, unsigned int raw_data_len, char result[16]);
extern void timestamp_region_update(int tid, long long cpu_cycle);
void pptp_exit(void);
extern void ipv4_frag_per_thread_exit(int thread_seq);
extern void ipv6_frag_per_thread_exit(int thread_seq);
extern void pptp_per_thread_exit(int thread_seq);
extern void __timestamp_print_max_tuple4(const raw_pkt_t *raw_pkt, long timedelay, int tid);
extern int packet_io_device_alias_init(void);
extern void sapp_fs2_set_latency(int thead_seq, long long time_cost);
static const unsigned char phony_feedback_eth_hdr_ip4[ETHERNET_HDR_LEN] =
{0x50, 0x48, 0x4F, 0x4E, 0x59, 0x5F, 0x4D, 0x41, 0x43, 0x41, 0x44, 0x44, 0x08, 0x00}; /* PHONY_MACADD + ipv4 */
static const unsigned char phony_feedback_eth_hdr_ip6[ETHERNET_HDR_LEN] =
{0x50, 0x48, 0x4F, 0x4E, 0x59, 0x5F, 0x4D, 0x41, 0x43, 0x41, 0x44, 0x44, 0x86, 0xDD}; /* PHONY_MACADD + ipv6*/
int packet_io_set_ipv6_module_enable(int op_switch)
{
g_ipv6_decapsulation_enabled = op_switch;
return 0;
}
int packet_io_set_ipv6_raw_socket_enable(int op_switch)
{
g_ipv6_send_packet_enabled = op_switch;
return 0;
}
int packet_io_register_cb(PACKET_IO_CB_T fun)
{
int ret = 0;
use_custom_pkt_cb = 1;
if(dl_io_fun_list.dl_io_register_cb){
ret = dl_io_fun_list.dl_io_register_cb(fun);
}else{
printf("Warning, packet io library not support 'dl_io_register_cb'!\n");
}
return ret;
}
int packet_io_set_cap_level(int cap_level)
{
int ret = 0;
g_packet_io_cap_level = cap_level;
if(dl_io_fun_list.dl_io_set_cap_level){
ret = dl_io_fun_list.dl_io_set_cap_level(cap_level);
}else{
printf("Warning, packet io library not support 'dl_io_set_cap_level'!\n");
}
return ret;
}
int packet_io_set_work_thread_num(int thread_num)
{
int ret = 0;
if(dl_io_fun_list.dl_io_set_work_thread_num){
ret = dl_io_fun_list.dl_io_set_work_thread_num(thread_num);
}else{
printf("Warning, packet io library not support 'dl_io_set_work_thread_num'!\n");
}
//g_packet_io_thread_num = thread_num;
return ret;
}
int packet_io_set_cap_mode(int cap_mode)
{
int ret = 0;
if(dl_io_fun_list.dl_io_set_cap_mode){
ret = dl_io_fun_list.dl_io_set_cap_mode(cap_mode);
}else{
printf("Warning, packet io library not support 'dl_io_set_cap_mode'!\n");
}
return ret;
}
/*
Ϊ�˷����û����ã������ļ��е�ֵ�Ƚϼ���0��1��2��3�ȣ�
�ڴ��е�ֵ����Ƚϸ��ӣ���Ҫ�˺���ת��һ��.
*/
static int topology_mode_convert(int config_value)
{
int private_value;
switch(config_value)
{
case 0:
private_value = NET_CONN_PARA_NOSEND;
break;
case 1:
private_value = NET_CONN_PARALLEL;
break;
case 2:
private_value = NET_CONN_SERIAL_2CARD;
break;
case 3:
private_value = NET_CONN_SERIAL_GDEV;
break;
default:
private_value = -1;
break;
}
return private_value;
}
int packet_io_set_topology_mode(int user_topology_mode)
{
int private_top_mode, ret = -1;
private_top_mode = topology_mode_convert(user_topology_mode);
if(private_top_mode < 0){
return -1;
}
//g_topology_config_raw_mode = user_topology_mode;
//g_topology_mode = private_top_mode;
if(dl_io_fun_list.dl_io_set_topology_mode){
ret = dl_io_fun_list.dl_io_set_topology_mode(private_top_mode);
}else{
printf("Warning, packet io library not support 'dl_io_set_topology_mode'!\n");
}
return ret;
}
int packet_io_set_capdev_parallel(const char *cap_dev)
{
int ret = 0;
if(dl_io_fun_list.dl_io_set_capdev_parallel){
ret = dl_io_fun_list.dl_io_set_capdev_parallel(cap_dev);
}else{
printf("Warning, packet io library not support 'dl_io_set_capdev_parallel'!\n");
}
strncpy(g_up_dev_name, cap_dev, DEV_NAME_STR_LEN);
return ret;
}
int packet_io_set_capdev_serial(const char *up_dev, const char *down_dev)
{
int ret = 0;
if(dl_io_fun_list.dl_io_set_capdev_serial){
ret = dl_io_fun_list.dl_io_set_capdev_serial(up_dev, down_dev);
}else{
printf("Warning, packet io library not support 'dl_io_set_capdev_serial'!\n");
}
strncpy(g_up_dev_name, up_dev, DEV_NAME_STR_LEN);
strncpy(g_down_dev_name, down_dev, DEV_NAME_STR_LEN);
return ret;
}
int packet_io_set_send_dev(const char *send_dev)
{
int ret;
ret = snprintf(g_send_dev_name, DEV_NAME_STR_LEN, "%s", send_dev);
if(ret >= DEV_NAME_STR_LEN){
printf("send device name is too long!\n");
return -1;
}
return ret;
}
int packet_io_set_gateway_mac(const char *gateway_mac)
{
if(MESA_mac_pton(gateway_mac, ':', (char *)g_send_gateway_mac) < 0){
printf("error, gateway mac:%s is not correct, for example:00:11:22:33:44:55\n", gateway_mac);
return -1;
}
return 0;
}
int packet_io_set_capture_filter(const char *filter_rule)
{
int ret = 0;
if(dl_io_fun_list.dl_io_set_capture_filter){
ret = dl_io_fun_list.dl_io_set_capture_filter(filter_rule);
}else{
printf("Warning, packet io library not support 'dl_io_set_capture_filter'!\n");
}
return ret;
}
long packet_io_get_app_drop_num(int thread_num)
{
long ret = 0;
if(dl_io_fun_list.dl_io_get_app_drop_num){
ret = dl_io_fun_list.dl_io_get_app_drop_num(thread_num);
}else{
//printf("Warning, packet io library not support 'dl_io_get_app_drop_num'!\n");
ret = 0;
}
return ret;
}
long packet_io_get_app_drop_num_tot(void)
{
int i;
long ret = 0;
for(i = 0; i < g_packet_io_thread_num; i++){
ret += packet_io_get_app_drop_num(i);
}
return 0;
}
long packet_io_get_lib_drop_num(void)
{
long ret = 0;
if(dl_io_fun_list.dl_io_get_lib_drop_num){
ret = dl_io_fun_list.dl_io_get_lib_drop_num();
}else{
//printf("Warning, packet io library not support 'dl_io_get_lib_drop_num'!\n");
ret = 0;
}
return ret;
}
int packet_io_set_cap_buf_queue(int queue_num_max)
{
int ret = 0;
if(dl_io_fun_list.dl_io_set_cap_buf_queue){
ret = dl_io_fun_list.dl_io_set_cap_buf_queue(queue_num_max);
}else{
printf("Warning, packet io library not support 'dl_io_set_cap_buf_queue'!\n");
}
return ret;
}
static int packet_io_process_ddp_pkt(const MESA_feedback_raw_pkt_t *ddp_fix_hdr,
int ddp_payload_len, raw_pkt_t *p_raw_pkt, unsigned char dir, int thread_num)
{
const char *ddp_payload;
//int pkt_index = 1; //�����ݲ�֧��DDP�ۺ�ԭʼ��
if(MESA_FEEDBACK_HDR_MAGIC != ddp_fix_hdr->property_hdr.magic){
return PASS;
}
if(0 != ddp_fix_hdr->property_hdr.stream){
return PASS;
}
if((MESA_FEEDBACK_DTYPE_IPv4 != ddp_fix_hdr->property_hdr.data_type)
&& (MESA_FEEDBACK_DTYPE_IPv6 != ddp_fix_hdr->property_hdr.data_type)){
return PASS;
}
ddp_payload = (const char *)((char *)ddp_fix_hdr + sizeof(MESA_feedback_raw_pkt_t));
#if 0 /* �������Զ����0, ͨ��IPͷ��ȡ�İ�������ʵ�ʲ���, ����Ҳ��֧��VLAN, PPPOE�����ݰ�, �����ݲ�֧��DDP�ۺ�ԭʼ��, һ��DDP��ֻȡһ��ԭʼ�� */
while(ddp_payload_len > 0)
{
p_raw_pkt->raw_pkt_len = get_pkt_len_from_eth_hdr((const struct mesa_ethernet_hdr *)ddp_payload);
if(p_raw_pkt->raw_pkt_len < 0){
sapp_runtime_log(30, "ddp raw pkt parse error, pkt index:%d\n", pkt_index);
break;
}
#else
{
p_raw_pkt->raw_pkt_len = ddp_payload_len;
#endif
p_raw_pkt->raw_pkt_data = (char *)ddp_payload;
#if CYCLE_PKT_DUMP
/* 2017-10-09 lijia add, for tcpdump_mesa capture ddp inner packet */
cycle_pkt_dump(thread_num, p_raw_pkt);
#endif
eth_entry(NULL, p_raw_pkt->raw_pkt_data, thread_num, dir, p_raw_pkt, 0);
#if 0
ddp_payload += p_raw_pkt->raw_pkt_len;
ddp_payload_len -= p_raw_pkt->raw_pkt_len;
if(ddp_payload_len < 0){
sapp_runtime_log(30, "ddp raw pkt parse error, pkt index:%d\n", pkt_index);
break;
}
pkt_index++;
#endif
}
return PASS;
}
static int packet_io_strip_ddp_hdr(const raw_pkt_t *p_raw_pkt, unsigned char dir, int thread_num)
{
int ret;
const MESA_feedback_raw_pkt_t *ddp_raw_pkt_hdr = NULL;
int ddp_payload_len = p_raw_pkt->raw_pkt_len; /* DDP���ݳ���, ���������ڲ����ddpͷ������ */
switch(p_raw_pkt->low_layer_type){
case CAP_LEVEL_MAC:
{
const struct mesa_ethernet_hdr *ethh = (const struct mesa_ethernet_hdr *)p_raw_pkt->raw_pkt_data;
if(ETHERTYPE_IP == ntohs(ethh->ether_type)){
const struct mesa_ip4_hdr *ip4h = (const struct mesa_ip4_hdr *)((char *)ethh + sizeof(struct mesa_ethernet_hdr));
ddp_raw_pkt_hdr = (const MESA_feedback_raw_pkt_t *)((char *)ip4h + ip4h->ip_hl * 4 + sizeof(struct mesa_udp_hdr));
ddp_payload_len -= sizeof(struct mesa_ethernet_hdr) + sizeof(struct mesa_ip4_hdr) + sizeof(struct mesa_udp_hdr) + sizeof(MESA_feedback_raw_pkt_t);
}else{
/* �ݲ�֧��������ʽ��DDPԭʼ���ش� */
sapp_runtime_log(30, "ddp raw pkt parse error, unknown ether_type:0x%x\n", ntohs(ethh->ether_type));
return PASS;
}
}
break;
case CAP_LEVEL_IPV4:
{
const struct mesa_ip4_hdr *ip4h = (const struct mesa_ip4_hdr *)p_raw_pkt->raw_pkt_data;
ddp_raw_pkt_hdr = (const MESA_feedback_raw_pkt_t *)((char *)ip4h + ip4h->ip_hl * 4 + sizeof(struct mesa_udp_hdr));
ddp_payload_len -= sizeof(struct mesa_ip4_hdr) + sizeof(struct mesa_udp_hdr) + sizeof(MESA_feedback_raw_pkt_t);
}
break;
default:
return PASS;
break;
}
ret = packet_io_process_ddp_pkt(ddp_raw_pkt_hdr, ddp_payload_len, (raw_pkt_t *)p_raw_pkt, dir, thread_num);
return ret;
}
#if 1 /* for xx7 �ӿ� */
typedef struct {
uint32_t magic_num;
uint8_t version;
uint8_t msg_type;
uint16_t flag;
uint32_t cont_len; /* ������ͷ����12�ֽ�! */
}L2E_msg_header_t; /*sizeof = 12B */
typedef struct {
uint16_t data_len; /* ������ͷ����4�ֽ� */
uint16_t data_type;
uint8_t data_value[0];
}L2E_data_small_block_hdr_t;
struct __msg_small_body_t{
uint32_t cap_ip; // ǰ�˽ڵ�IP
uint32_t data_id; // �����н����Ψһ���
uint16_t unit_num; //С���ݿ�ĸ���
L2E_data_small_block_hdr_t small_unit[0]; //С���ݿ�����
}__attribute__((packed));
typedef struct __msg_small_body_t L2E_msg_small_body_hdr_t;
#endif
/*
3x7��Ŀ�ش���ԭʼ��ͷ��.
*/
static int packet_io_strip_L2E_hdr(raw_pkt_t *p_raw_pkt, unsigned char dir, int thread_num)
{
int ret;
#define L2E_HDR_TOTAL_LEN (78)
#define L2E_PKT_HDR_MAGIC 0xD0BADABA
const struct mesa_ethernet_hdr *ethh = (const struct mesa_ethernet_hdr *)p_raw_pkt->raw_pkt_data;
//const unsigned int *l2e_hdr_magic;
unsigned char *L2E_payload;
int L2E_len;
const L2E_msg_header_t *L2E_msg_hdr;
const L2E_data_small_block_hdr_t *small_block_hdr;
const L2E_msg_small_body_hdr_t *small_block_body;
int i;
/* һЩ������ */
if(ntohs(ethh->ether_type) != ETHERTYPE_IP){
return PASS;
}
const struct mesa_ip4_hdr *ip4h = (const struct mesa_ip4_hdr *)((char *)p_raw_pkt->raw_pkt_data + sizeof(struct mesa_ethernet_hdr));
if(ip4h->ip_p != IPPROTO_UDP){
return PASS;
}
if(ntohs(ip4h->ip_len) < sizeof(struct mesa_ip4_hdr)/* �����ipͷ */ + sizeof(struct mesa_udp_hdr)/* ���udpͷ */ + L2E_HDR_TOTAL_LEN + sizeof(struct mesa_ip4_hdr)/* �ڲ�IP��ͷ */){
return PASS;
}
L2E_payload = (unsigned char *)p_raw_pkt->raw_pkt_data + sizeof(struct mesa_ethernet_hdr) + sizeof(struct mesa_ip4_hdr) + sizeof(struct mesa_udp_hdr);
L2E_len = p_raw_pkt->raw_pkt_len - sizeof(struct mesa_ethernet_hdr) - sizeof(struct mesa_ip4_hdr) - sizeof(struct mesa_udp_hdr);
while(L2E_len > 0){
L2E_msg_hdr = (L2E_msg_header_t *)L2E_payload;
if(L2E_msg_hdr->magic_num != L2E_PKT_HDR_MAGIC){
sapp_runtime_log(20, "L2E hdr error, magic is:%x\n", L2E_msg_hdr->magic_num);
return PASS;
}
L2E_payload += sizeof(L2E_msg_header_t);
L2E_len -= sizeof(L2E_msg_header_t);
if(L2E_len < (int)L2E_msg_hdr->cont_len ){
sapp_runtime_log(20, "L2E hdr error, left payload len is %d, smaller than hdr declare len:%d!\n", L2E_len, L2E_msg_hdr->cont_len);
return PASS;
}
if(L2E_msg_hdr->msg_type != 2){ /* 1:meta data; 2:small block; 3:big block, ֻ����С�������е�IP��, ��������������! */
L2E_payload += L2E_msg_hdr->cont_len;
L2E_len -= L2E_msg_hdr->cont_len;
continue;
}
small_block_body = (L2E_msg_small_body_hdr_t *)L2E_payload;
L2E_payload += sizeof(L2E_msg_small_body_hdr_t);
L2E_len -= sizeof(L2E_msg_small_body_hdr_t);
for(i = 0; i < small_block_body->unit_num; i++){
small_block_hdr = (L2E_data_small_block_hdr_t *)L2E_payload;
if(small_block_hdr->data_type != 0x0002){ /* 0x0002: ԭʼIP�� */
L2E_payload += small_block_hdr->data_len;
L2E_len -= small_block_hdr->data_len;
continue;
}
/* ����raw_pktָ�� */
p_raw_pkt->raw_pkt_data = L2E_payload + sizeof(L2E_data_small_block_hdr_t);
p_raw_pkt->raw_pkt_len = small_block_hdr->data_len - sizeof(L2E_data_small_block_hdr_t);
p_raw_pkt->low_layer_type = ADDR_TYPE_IPV4;
#if CYCLE_PKT_DUMP
/* for tcpdump_mesa capture ddp inner packet */
cycle_pkt_dump(thread_num, p_raw_pkt);
#endif
ret = ipv4_entry(NULL, p_raw_pkt->raw_pkt_data, thread_num, dir, p_raw_pkt, 0);
L2E_payload += small_block_hdr->data_len;
L2E_len -= small_block_hdr->data_len;
}
}
return ret;
}
extern void sapp_fs2_set_latency(int thead_seq, long long time_cost);
extern char (*g_platform_action_cb_fun)(int net_conn_mode, char plug_action);
static int mesa_default_pkt_cb(const raw_pkt_t *p_raw_pkt, unsigned char dir, int thread_num)
{
int ret = PASS;
//long long before_call = 0, after_call;
long long timecost;
struct timespec start, end;
if(unlikely(g_timestamp_record_sw))
{
clock_gettime(CLOCK_MONOTONIC, &start);
//before_call = sapp_get_cpu_cycle();
}
#if DEBUG
char raw_pkt_md5_before[16];
char raw_pkt_md5_after[16];
if(unlikely(g_raw_pkt_broken_check != 0)){
MESA_MD5_sum_bin((unsigned char *)p_raw_pkt->raw_pkt_data, p_raw_pkt->raw_pkt_len, raw_pkt_md5_before);
}
#endif
if(unlikely(g_encapsulate_with_ddp)){
/* ����ddp���װ���ܺ�, ��packet_io_process_ddp_pkt()�����в���DDP�������� */
ret = packet_io_strip_ddp_hdr(p_raw_pkt, dir, thread_num);
} else if(unlikely(g_encapsulate_with_L2E)){
ret = packet_io_strip_L2E_hdr((raw_pkt_t *)p_raw_pkt, dir, thread_num);
}else{
#if CYCLE_PKT_DUMP
cycle_pkt_dump(thread_num, p_raw_pkt);
#endif
switch(p_raw_pkt->low_layer_type){
case CAP_LEVEL_MAC:
ret = eth_entry(NULL,p_raw_pkt->raw_pkt_data,thread_num,dir, p_raw_pkt, 0);
break;
case CAP_LEVEL_IPV4:
ret = ipv4_entry(NULL,(void *)p_raw_pkt->raw_pkt_data,thread_num,dir, p_raw_pkt, 0);
break;
case CAP_LEVEL_IPV6:
ret = ipv6_entry(NULL,(void *)p_raw_pkt->raw_pkt_data,thread_num,dir, p_raw_pkt, 0);
break;
default:
return PASS;
break;
}
}
//after_call = sapp_get_cpu_cycle();
//timecost = after_call - before_call;
if(unlikely(g_timestamp_record_sw))
{
clock_gettime(CLOCK_MONOTONIC, &end);
timecost = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_nsec - start.tv_nsec);
//timestamp_region_update(thread_num, timecost);
sapp_fs2_set_latency(thread_num, timecost);
}
#if DEBUG
if(unlikely(g_timestamp_record_sw)){
if(timecost > g_timedelay_threshold){
__timestamp_print_max_tuple4(p_raw_pkt, timecost, thread_num);
}
}
if(unlikely(g_raw_pkt_broken_check != 0)){
MESA_MD5_sum_bin((unsigned char *)p_raw_pkt->raw_pkt_data, p_raw_pkt->raw_pkt_len, raw_pkt_md5_after);
if(memcmp(raw_pkt_md5_before, raw_pkt_md5_after, 16) != 0){
printf("\033[41mraw packet is broken involuntarily! If you don't care, please disable 'raw_pkt_broken_check' in main.conf!\033[0m\n");
abort();
}
}
#endif
if(g_platform_action_cb_fun != NULL){
ret = g_platform_action_cb_fun(sapp_global_val->config.packet_io.depolyment_mode_bin, ret);
}
if((PASS == ret) && (g_topology_mode & __NET_CONN_SERIAL)){
sapp_global_val->mthread_volatile[thread_num]->sys_stat.count[SAPP_STAT_ETH_EGRESS]++;
sapp_global_val->mthread_volatile[thread_num]->sys_stat.length[SAPP_STAT_ETH_EGRESS]+= p_raw_pkt->raw_pkt_len;
}
return ret;
}
void packet_io_exit(void)
{
/* ����ģʽ��, �������߳�һЩʱ��, ����δ�������, ����MESA_tcp�첽�������ݵȵ�. */
sapp_set_current_state(SAPP_STATE_READY_TO_EXIT);
sched_yield();
sleep(1);
sched_yield();
sleep(1);
exit(1);
return;
}
int packet_io_init(int argc, char *argv[])
{
int i;
#if (0 == SAPP_AS_TARGET_SO)
if(0 == use_custom_pkt_cb){
packet_io_register_cb(mesa_default_pkt_cb);
}
if(NULL == dl_io_fun_list.dl_io_init){
printf("Error, packet io library must support 'dl_io_init' !\n");
exit(0);
}
if(dl_io_fun_list.dl_io_init(argc, argv) < 0){
return -1;
}
#endif
for(i = 0; i < MAX_THREAD_NUM; i++){
g_send_buf_pool[i] = (UINT8 *)malloc(SENDPACKET_BUF_LEN);
}
if(sendpacket_init_new(g_packet_io_thread_num) < 0){
printf("Error, sendpacket_init_new error !\n");
return -1;
}
#if 0 /* 20161117 lijia move to send_handle_init() */
for(i = 0; i < g_packet_io_thread_num; i++){
g_packet_dl_send_handle[i] = dl_io_fun_list.dl_io_get_send_handle(i);
}
#endif
if(CAP_MODEL_PCAP_DUMPFILE == g_packet_io_cap_mode){
dl_io_fun_list.dl_io_register_exit_cb(packet_io_exit);
}
if(g_send_dev_name[0] != '\0'){
if(MESA_get_dev_mac(g_send_dev_name, g_send_dev_mac) < 0){
printf("Warning, can't get %s mac addr!\n", g_send_dev_name);
}
}else{
//printf("Warning, not assign send device!\n");
;
}
parse_send_gdev_ip_conf("conf/send_gdev.conf");
parse_send_route_conf("conf/send_route.conf");
packet_io_device_alias_init();
packet_io_status_init();
return 0;
}
extern int g_StreamTcpAllFunNum;
extern int g_Ipv6FunNum;
extern int app_function_rationality_check(void);
/* not return until error, or use 'CAP_MODEL_PCAP_DUMPFILE' mode */
void packet_io_run(void)
{
pthread_t pid;
#if (0 == SAPP_AS_TARGET_SO)
if(NULL == dl_io_fun_list.dl_io_run){
printf("Error, packet io library must support 'dl_io_run' !\n");
sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: Error, packet io library must support 'dl_io_run' !\n", __FILE__, __LINE__);
assert(0);
}
#endif
app_function_rationality_check();
pthread_create(&pid, NULL, sapp_time_event_thread ,NULL);
#if (0 == SAPP_AS_TARGET_SO)
dl_io_fun_list.dl_io_run();
#endif
}
extern volatile int update_packet_io_status_sw;
void packet_io_clean_thread_context(int thread_seq)
{
if(1 == update_packet_io_status_sw){
sysinfo_output(); /* ���һ��дͳ����Ϣ, �رձ�־λ, ��Ϊ�����������, ��д��û������ */
update_packet_io_status_sw = 0;
}
free_thread_stream(thread_seq);
ipv4_frag_per_thread_exit(thread_seq);
ipv6_frag_per_thread_exit(thread_seq);
pptp_per_thread_exit(thread_seq);
}
MESA_send_handle *packet_io_get_send_handle(int thread_id)
{
return &g_send_handle[thread_id];
}
unsigned char *packet_io_get_sendbuf(int type, int thread_num)
{
if(g_topology_mode & __NET_CONN_PARALLEL){
return g_send_buf_pool[thread_num];
}
return dl_io_fun_list.dl_io_get_sendbuf(g_packet_dl_send_handle[thread_num], thread_num);
}
void packet_io_free_sendbuf(int type, int thread_num)
{
if(g_topology_mode & __NET_CONN_PARALLEL){
return; /* use static global variable, do nothing */
}
dl_io_fun_list.dl_io_free_sendbuf(g_packet_dl_send_handle[thread_num], thread_num);
return;
}
static int packet_io_calc_ethernet_offset_to_ip(const unsigned char *sendbuf)
{
int offset;
const struct mesa_ethernet_hdr *outer_ehdr;
///const struct mesa_ethernet_hdr *inner_ehdr;
if(sapp_global_single.send_fake_pkt_mode == SEND_PKT_MODE_GDEV){
sendbuf += VXLAN_HDR_RESERVED_LEN; /* vxlanģʽ��Ԥ����vxlan�����mac,ip,udpͷ��, ��ת���ڲ�macͷ */
}
switch(g_packet_io_cap_level){
case CAP_LEVEL_IPV4:
offset = 0;
break;
case CAP_LEVEL_MAC:
outer_ehdr = (struct mesa_ethernet_hdr *)sendbuf;
if(ETHERTYPE_PANGU_MAC_IN_MAC == ntohs(outer_ehdr->ether_type)){
offset = MAC_IN_MAC_HDR_LEN;
}else{
offset = sizeof(struct mesa_ethernet_hdr);
}
break;
default:
offset = -1;
sapp_runtime_log(RLOG_LV_FATAL, "MESA_kill_tcp: Invalid cap_mode:%d\n", g_packet_io_cap_level);
return -1;
}
return offset;
}
static int packet_io_send_by_sys_routev4(MESA_send_handle *send_handle,int datalen,int dir,
char *feedback_buf, int *feedback_buf_len)
{
int ret, offset = 0;
struct sockaddr_in sock_addr_v4;
char *actual_data_ptr;
struct mesa_ip4_hdr *ip4_hdr;
sapp_gval_mthread_sys_stat_t *local_sys_stat = &sapp_global_val->mthread_volatile[send_handle->threadnum]->sys_stat;
offset = packet_io_calc_ethernet_offset_to_ip(send_handle->send_buf);
if(offset < 0){
return -1;
}
/* ʹ��ϵͳ·��ʱ, MAC��ַ��Э��ջ���, ָ��ͳ���Ҫ��ȥETHͷ�� */
#if 0
actual_data_ptr = (char *)send_handle->send_buf + sizeof(mesa_ethernet_hdr);
datalen -= sizeof(mesa_ethernet_hdr);
#else
actual_data_ptr = (char *)send_handle->send_buf + offset;
datalen -= offset;
#endif
ip4_hdr = (struct mesa_ip4_hdr *)actual_data_ptr;
memset(&sock_addr_v4, 0, sizeof(struct sockaddr_in));
sock_addr_v4.sin_family = AF_INET;
/* build_ipv4ʱ�Ѿ���dir������õ�ַ, �˴�ֱ��ʹ��ip_dst���� */
sock_addr_v4.sin_addr.s_addr = ip4_hdr->ip_dst.s_addr;
//if(sapp_global_single.send_fake_pkt_mode != 0){
// return packet_io_send_fake_pkt_by_gdev(send_handle, ADDR_TYPE_IPV4,
// actual_data_ptr+VXLAN_HDR_RESERVED_LEN, datalen, dir,
// feedback_buf, feedback_buf_len);
//}
retry:
ret = sendto(send_handle->raw_ipv4_fd, actual_data_ptr, datalen, 0,
(struct sockaddr *)&sock_addr_v4, sizeof(struct sockaddr));
if(ret < 0){
if(EINTR == errno){
goto retry; /* ���ź��жϵ�IO������Ҫ���� */
}else{
//g_SysInputInfo[send_handle->threadnum][SEND_PKT_ERR]++;
//g_SysInputInfo[send_handle->threadnum][SEND_PKT_ERR_LEN] += datalen;
local_sys_stat->count[SAPP_STAT_SND_ERROR]++;
sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: sendto error, %s\n", __FILE__,__LINE__,strerror(errno));
return ret;
}
}
/* �Ƿ���Ҫ��Inject�������������� */
if(feedback_buf != KILL_TCP_PHONY_POINTER){
if(*feedback_buf_len < ret + (int)sizeof(struct mesa_ethernet_hdr)){
return -2;
}
memcpy(feedback_buf, phony_feedback_eth_hdr_ip4, ETHERNET_HDR_LEN);
memcpy(feedback_buf + sizeof(struct mesa_ethernet_hdr), actual_data_ptr, datalen);
*feedback_buf_len = datalen + sizeof(struct mesa_ethernet_hdr);
}
return ret;
}
static int packet_io_send_by_sys_routev6(MESA_send_handle *send_handle,int datalen,int dir,
char *feedback_buf, int *feedback_buf_len)
{
int ret;
struct sockaddr_in6 sock_addr_v6;
char *actual_data_ptr;
struct mesa_ip6_hdr *ip6_hdr;
int offset;
sapp_gval_mthread_sys_stat_t *local_sys_stat = &sapp_global_val->mthread_volatile[send_handle->threadnum]->sys_stat;
if(0 == g_ipv6_send_packet_enabled){
printf("IPv6 module is not support! Please check 'sapp.toml->ipv6_send_packet_enabled'.\n");
sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: IPv6 module is not support! Please check 'sapp.toml->ipv6_send_packet_enabled'.\n", __FILE__, __LINE__);
return -1;
}
offset = packet_io_calc_ethernet_offset_to_ip(send_handle->send_buf);
if(offset < 0){
return -1;
}
/* ʹ��ϵͳ·��ʱ, MAC��ַ��Э��ջ���, ָ��ͳ���Ҫ��ȥETHͷ�� */
#if 0
actual_data_ptr = (char *)send_handle->send_buf + sizeof(struct mesa_ethernet_hdr);
datalen -= sizeof(struct mesa_ethernet_hdr);
#else
/* 2018-09-13 lijia modify, adjust MAC_IN_MAC header */
actual_data_ptr = (char *)send_handle->send_buf + offset;
datalen -= offset;
#endif
ip6_hdr = (struct mesa_ip6_hdr *)actual_data_ptr;
memset(&sock_addr_v6, 0, sizeof(struct sockaddr_in6));
sock_addr_v6.sin6_family = AF_INET6;
memcpy(sock_addr_v6.sin6_addr.s6_addr, ip6_hdr->ip6_dst.s6_addr, IPV6_ADDR_LEN);
retry:
ret = sendto(send_handle->raw_ipv6_fd, actual_data_ptr, datalen, 0,
(struct sockaddr *)&sock_addr_v6, sizeof(struct sockaddr_in6));
if(ret < 0){
if(EINTR == errno){
goto retry; /* ���ź��жϵ�IO������Ҫ���� */
}else{
//g_SysInputInfo[send_handle->threadnum][SEND_PKT_ERR]++;
//g_SysInputInfo[send_handle->threadnum][SEND_PKT_ERR_LEN] += datalen;
local_sys_stat->count[SAPP_STAT_SND_ERROR]++;
sapp_runtime_log(RLOG_LV_FATAL,"%s:%d: packet_io_send_by_sys_routev6() sendto error, %s\n", __FILE__,__LINE__,strerror(errno));
return ret;
}
}
if(feedback_buf != KILL_TCP_PHONY_POINTER){
if(*feedback_buf_len < ret + (int)sizeof(struct mesa_ethernet_hdr)){
return -2;
}
memcpy(feedback_buf, phony_feedback_eth_hdr_ip6, ETHERNET_HDR_LEN);
memcpy(feedback_buf + sizeof(struct mesa_ethernet_hdr), actual_data_ptr, datalen);
*feedback_buf_len = datalen + sizeof(struct mesa_ethernet_hdr);
}
return ret;
}
static int packet_io_get_sendroute_mac(char dev_id, char link_id, unsigned char *out_mac_addr)
{
char *p_mac_addr = (char *)&g_send_gateway_mac_table[dev_id][link_id][0];
if(G_SND_ROUTE_INFO_NUM <= 0 || strlen(p_mac_addr) <= 0){
return -1;
}else{
if(MESA_mac_pton(p_mac_addr, ':', (char *)out_mac_addr) < 0){
printf("error, src mac:%s is not correct, for example:00:11:22:33:44:55\n", p_mac_addr);
return -1;
}
}
return 0;
}
static int packet_io_send_by_manual_conf(MESA_send_handle *send_handle,int datalen,
int dir, UINT16 ether_type)
{
int ret;
/* to do:
��ȡ����ip, mac ��Ϣ,
�����ֹ�����,
����ݷ���/proc/net/route, /proc/net/arp�Զ���ȡ!
get_gateway_info_by_manual();
get_gateway_info_aotu();
*/
#if 0 /* for test, use 10.0.6.201 mac addr */
static char mac_201_gateway[6] = {0xdc, 0xd2, 0xfc, 0x66, 0xec, 0xb2};
static char mac_201_em2[6] = {0x74, 0x86,0x7A,0xD0,0x12,0xFD};
memcpy(g_send_gateway_mac, mac_201_gateway, 6);
memcpy(g_send_dev_mac, mac_201_em2, 6);
#endif
const struct streaminfo_private *pstrem_pr = (const struct streaminfo_private *)send_handle->user_arg;
const struct streaminfo *pstream = (const struct streaminfo *)send_handle->user_arg;
const struct streaminfo *tmpstream = pstream;
struct vxlan_info mim_mem_hdr;
int opt_len = sizeof(mim_mem_hdr);
ret = MESA_get_stream_opt(tmpstream, MSO_STREAM_VXLAN_INFO, &mim_mem_hdr, &opt_len);
if(ret < 0){
sapp_runtime_log(20, "packet_io_send_by_manual_conf(): get vxlan info error!\n");
return -1;
}
unsigned char send_route_mac[MAC_ADDR_LEN] = "";
unsigned char *p_dst_mac;
if(sapp_global_single.send_fake_pkt_mode == SEND_PKT_MODE_STACK_2_LAYER_MUTI_ROUTE )
{
ret = packet_io_get_sendroute_mac(mim_mem_hdr.dev_id, mim_mem_hdr.link_id, send_route_mac);
if(ret < 0){
sapp_runtime_log(20, "packet_io_send_by_manual_conf(): get sendroute_mac info using %d:%d error!\n", mim_mem_hdr.dev_id, mim_mem_hdr.link_id);
return -1;
}
p_dst_mac = send_route_mac;
}
else
{
p_dst_mac = g_send_gateway_mac;
}
sendpacket_build_ethernet(p_dst_mac, g_send_dev_mac, ether_type, NULL, 0, send_handle->send_buf);
send_again:
ret = sendto(send_handle->raw_eth_fd, send_handle->send_buf, datalen, 0,
(struct sockaddr *)&send_handle->saddr_raw_eth, sizeof(struct sockaddr));
if(ret < 0){
if((EAGAIN == errno) || (EINTR == errno) ){
goto send_again;
}else{
return -1;
}
}
return ret;
}
int packet_io_send_fake_pkt(MESA_send_handle *send_handle,int datalen,int send_type,
int low_layer_type, int dir,int thread_num,
char *feedback_buf, int *feedback_buf_len)
{
int ret;
UINT16 ether_type;
if(g_topology_mode & __NET_CONN_PARALLEL)
{
if(sapp_global_single.send_fake_pkt_mode == SEND_PKT_MODE_GDEV)
{
ret = packet_io_send_fake_pkt_by_gdev(send_handle, (enum addr_type_t)low_layer_type,
(char *)send_handle->send_buf, datalen, dir, feedback_buf, feedback_buf_len);
}
else if(sapp_global_single.send_fake_pkt_mode == SEND_PKT_MODE_STACK_2_LAYER_1_ROUTE || sapp_global_single.send_fake_pkt_mode == SEND_PKT_MODE_STACK_2_LAYER_MUTI_ROUTE)
{
ether_type = net_layer_to_ethernet_protocol(low_layer_type);
ret = packet_io_send_by_manual_conf(send_handle, datalen, dir, ether_type);
}
else
{
switch (low_layer_type)
{
case __ADDR_TYPE_IP_PAIR_V4:
case ADDR_TYPE_IPV4:
ret = packet_io_send_by_sys_routev4(send_handle, datalen, dir, feedback_buf, feedback_buf_len);
break;
case __ADDR_TYPE_IP_PAIR_V6:
case ADDR_TYPE_IPV6:
ret = packet_io_send_by_sys_routev6(send_handle, datalen, dir, feedback_buf, feedback_buf_len);
break;
default:
/* ����ģʽ�µķ�IPЭ��, ����ʹ��ϵͳ·�ɷ���, ����ʹ��raw_eth_fdͨ��ָ���������� */
ether_type = net_layer_to_ethernet_protocol(low_layer_type);
ret = packet_io_send_by_manual_conf(send_handle, datalen, dir, ether_type);
break;
}
}
}
else
{
ret = dl_io_fun_list.dl_io_low_level_send(send_handle->low_level_send_handle,
(UINT8 *)send_handle->send_buf,
datalen,
low_layer_type,
dir,
thread_num);
}
if(ret >= 0){
sapp_global_val->mthread_volatile[thread_num]->sys_stat.count[SAPP_STAT_ETH_EGRESS]++;
sapp_global_val->mthread_volatile[thread_num]->sys_stat.length[SAPP_STAT_ETH_EGRESS] += datalen;
}
return ret;
}
/*
send_type:��������, RST, ��־, �ش����ݵ�;
low_layer_type:��MAC��֮��, ��ײ�Э������, ��IPv4, ipv6, VLAN��.
datalen :�ϲ�APP�ܳ�, ������MAC���14�ֽ�.
*/
int packet_io_send(MESA_send_handle *send_handle,int datalen,int send_type,
int low_layer_type, int dir,int thread_num,
char *feedback_buf, int *feedback_buf_len)
{
int ret = -1;
switch(send_type){
case SEND_TYPE_LINK_INJECT:
ret = packet_io_send_fake_pkt(send_handle, datalen, send_type, low_layer_type,
dir, thread_num, feedback_buf, feedback_buf_len);
break;
case SEND_TYPE_LOG:
/* to do */
ret = -1;
break;
case SEND_TYPE_REDIRECT:
/* to do, ˮ�ԭʼ����ת�� */
ret = -1;
break;
default:
break;
}
return ret;
}
send_raw_pkt_info_t *get_raw_pkt_conf_by_id(unsigned int target_id)
{
int i;
for(i = 0; i < G_SND_RAW_PKT_INFO_NUM; i++){
if(target_id == G_SND_RAW_PKT_INFO[i].target_id){
return &(G_SND_RAW_PKT_INFO[i]);
}
}
//printf("Send raw pkt error! Not found target_id:%u\n", target_id);
return NULL;
}
int parse_send_gdev_ip_conf(const char *profile_path)
{
FILE *fp;
char conf_buf[1024] = "";
int dev_id = 0;
char ip_addr[32] = "";
int ret = 0;
fp = fopen(profile_path, "r");
if(NULL == fp){
printf("open %s error!\n", profile_path);
return -1;
}
memset(g_send_gdev_ip_table, 0, sizeof(g_send_gdev_ip_table));
G_SND_GDEV_INFO_NUM = 0;
while(fgets(conf_buf, 1024, fp)){
if('#' == conf_buf[0] || ' ' == conf_buf[0] || '\r' == conf_buf[0] || '\n' == conf_buf[0]){
continue;
}
//del_last_rn(conf_buf, 1024);
ret = sscanf(conf_buf, "%d\t%s", &dev_id, ip_addr);
if(ret != 2 || dev_id >= 64 || strlen(ip_addr) >= 32)
{
printf("read %s error, line:%s!\n", profile_path, conf_buf);
G_SND_GDEV_INFO_NUM = 0;
break;
}
memcpy(g_send_gdev_ip_table[dev_id], ip_addr, strlen(ip_addr));
G_SND_GDEV_INFO_NUM++;
memset(conf_buf, 0, sizeof(conf_buf));
}
fclose(fp);
return 0;
}
int parse_send_route_conf(const char *profile_path)
{
FILE *fp;
char conf_buf[1024] = "";
//const char *delim = "\t ";
int dev_id = 0, link_id = 0;
char mac_addr[18] = "";
int ret = 0;
fp = fopen(profile_path, "r");
if(NULL == fp){
printf("open %s error!\n", profile_path);
return -1;
}
memset(g_send_gateway_mac_table, 0, sizeof(g_send_gateway_mac_table));
G_SND_ROUTE_INFO_NUM = 0;
while(fgets(conf_buf, 1024, fp)){
if('#' == conf_buf[0] || ' ' == conf_buf[0] || '\r' == conf_buf[0] || '\n' == conf_buf[0]){
continue;
}
//del_last_rn(conf_buf, 1024);
ret = sscanf(conf_buf, "%d\t%d\t%s", &dev_id, &link_id, mac_addr);
if(ret != 3 || dev_id >= 64 || link_id >= 16 || strlen(mac_addr) >= 18)
{
printf("read %s error, line:%s!\n", profile_path, conf_buf);
G_SND_ROUTE_INFO_NUM = 0;
break;
}
memcpy(g_send_gateway_mac_table[dev_id][link_id], mac_addr, strlen(mac_addr));
G_SND_ROUTE_INFO_NUM++;
memset(conf_buf, 0, sizeof(conf_buf));
}
fclose(fp);
return 0;
}
int packet_io_send_raw(int thread_num, char *data, int datalen,unsigned int target_id)
{
MESA_send_handle *snd_handle;
//send_raw_pkt_info_t *send_info;
int ret;
snd_handle = packet_io_get_send_handle(thread_num);
if(NULL == snd_handle){
return -1;
}
//send_info = get_raw_pkt_conf_by_id(target_id);
//if(NULL == send_info){
//return -1;
//}
ret = dl_io_fun_list.dl_io_raw_pkt_send(snd_handle->low_level_send_handle,
(unsigned char *)data,
datalen,
g_packet_device_alias[target_id].dl_io_param,
thread_num);
return ret;
}
#ifdef __cplusplus
}
#endif
|