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
|
/*
��WY��Ŀ��������, �Ƿ���GDEV�����Ǹ��ݲ��������״̬��̬����, �������ٹ̶�һֱ����,
���Խ�GDEV-KEEPALIVE���ܴ�ƽ̨�а���,
���ص�IP��UDP�������ԭ�б����.
Ŀǰû��arp_entry, ethernet_entry, ARP�ظ�������ʱ������ƽ̨�ڲ�.
*/
#include <pthread.h>
#ifndef __FAVOR_BSD
#define __FAVOR_BSD 1
#endif
#include "sapp_api.h"
#include "sapp_private_api.h"
#include "sapp_declaration.h"
#include "stream_inc/gdev_keepalive.h"
#include "field_stat2.h"
#if IOMODE_MARSIO
static struct mr_instance * gdev_plug_extern_marsio4_instance; /* dlopen dynamic link sapp->marsio4 instance */
extern int marsio_send_burst_with_options_for_tcpdumpmesa(struct mr_sendpath * sendpath, queue_id_t sid, marsio_buff_t * mbufs[], int nr_mbufs, uint16_t options);
extern int (*ptr_marsio_buff_malloc_global)(struct mr_instance * instance, marsio_buff_t *marsio_buff[], unsigned int nr_mbufs, int socket_id, int thread_id);
extern char * (*ptr_marsio_buff_append)(marsio_buff_t *m, uint16_t len);
extern void (*ptr_marsio_buff_free)(struct mr_instance * instance, marsio_buff_t *marsio_buff[],unsigned int nr_mbufs, int socket_id, int thread_id);
#endif
#define MAX_SUPPORT_GDEV_NUM (64) /* ÿ���߳������֧��inline device������, ���ݷ������Ժ��߳����IJ�ͬ, ��Сֵ��MAX_SUPPORT_GDEV_NUM, ���ֵ��MAX_SUPPORT_GDEV_NUM * sapp_thread_count */
#define MAX_VXLAN_SERVICE_NUM (256) /* Ŀǰ���֧��255�ֲ�ͬҵ��, �����±������255 */
#define MAX_NO_PKT_TIMEOUT (120) /* ������ô��ʱ��һֱû�����, �Ѵ��豸�� */
static void *gdev_kp_log_handle;
enum prog_work_mode_t{
PROG_MODE_MASTER = 1,
PROG_MODE_SLAVE = 2,
};
enum gdev_keepalive_fs2_column_t{
FS2_COLUMN_REQUEST_NUM = 0,
FS2_COLUMN_REPLY_NUM,
__FS2_COLUMN_MAX,
};
/*
ÿ���߳�, ÿ��Inline-DEV-IP, ÿ��ҵ��ֱ�ͳ��.
*/
typedef struct {
int gdev_keepalive_type; /* for example: GDEV_KEEPALIVE_TYPE_BFD */
//int gdev_keepalive_service_num;
unsigned int keepalive_request_num; /* ��ǰҵ�����������, ÿ��һ��ʱ������ */
unsigned int keepalive_reply_num; /* ��ǰҵ��Ӧ�������, ����service��ͬ, ����ijЩҵ����ⲻ�ظ�Ӧ��, ����request����һ������reply */
int field_stat_line_id; /* fs2 �������id */
time_t last_time; /* ��ǰҵ�����һ���յ��������ʱ�� */
}gdev_keepalive_status_per_service_t;
typedef struct{
int inuse_flag; /* ��λ�ñ�ռ�� */
unsigned int gdev_ip_network_order;
unsigned int keepalive_request_num; /* ��ǰ�豸�������������, ÿ��һ��ʱ������ */
unsigned int keepalive_reply_num; /* ��ǰ�豸����Ӧ�������, ����service��ͬ, ����ijЩҵ����ⲻ�ظ�Ӧ��, ����request����һ������reply */
int fs2_line_id;
time_t last_time; /* ��ǰ�豸���һ���յ��������ʱ�� */
gdev_keepalive_status_per_service_t service_array[MAX_VXLAN_SERVICE_NUM]; /* ��BFDЭ���������service, ICMPĬ�϶���0��ҵ�� */
}gdev_keepalive_status_per_dev_t;
struct _gdev_keepalive_status_per_thread{
gdev_keepalive_status_per_dev_t gdev_array[MAX_SUPPORT_GDEV_NUM];
}__attribute__ ((aligned (64)));
typedef struct _gdev_keepalive_status_per_thread gdev_keepalive_status_per_thread_t;
/*
˵��: gdev�ı���״̬����һ����������, ��Ϊ���Ȳ�֪������,
��Ϊʲô����hash? ����? ��̬����?
����ģʽ�Ƕ���߳�д, һ���̶߳�ȡ���������, ����Ƕ�̬�����ݽṹ, �ⲻ��Ҫ����,
ʹ���������ȷ��������ڴ�, ����־λinuse_flag��ʾ�Ƿ���ʹ��, ��֤��д����ͻ.
*/
static gdev_keepalive_status_per_thread_t GDEV_KEEPALIVE_STAT[MAX_THREAD_NUM];
static volatile int g_dev_keepalive_flag = 1; /* ȫ�ֱ����־, ���в���������������Ҫ������������ */
static volatile struct gdev_keepalive_service_ctrl g_dev_keepalive_service_ctrl_array[MAX_VXLAN_SERVICE_NUM];/* ����ij��ҵ����Ƿ� */
//extern int g_packet_io_thread_num;
//extern time_t g_CurrentTime;
static int g_dev_keepalive_default_action = 1; /* ����g_dev_keepalive_service_ctrl_array��û�е�ҵ���, �DZ���Dz����� */
static unsigned int sendto_gdev_card_ip; /* ��Gdev����������IP��ַ, ������, network-order */
extern int sendpacket_build_icmpv4_echo(u_int8_t type, u_int8_t code, u_int16_t sum,
u_int16_t id, u_int16_t seq, u_int8_t *payload, u_int32_t payload_s, unsigned char *buf);
extern char *timet_to_str(time_t timet, char *time_str, int time_str_len);
static int vxlan_sport_service_map_init(void);
extern void del_last_rn(char *data, int max_len);
static unsigned char g_vxlan_sport_to_service_id[65536]; /* �����±�ΪԴ�˿�, ֵΪservice-id, �������ֵ��65535 */
static int gdev_kp_action_judge(int service_id)
{
if(0 == g_dev_keepalive_flag){
return 0;
}
return g_dev_keepalive_service_ctrl_array[service_id].keepalive_switch;
}
/*
prometheus fs2 line���Ʋ��ܰ�����Щ�ַ�:
const char* reserverd="|:\n\r. \t<>[]#!@";
����ip��ַ�ķָ�����Ҫ�ij�'_'
*/
static const char *fs2_inet_ntop(unsigned int gip_net, char *buf, int buf_len)
{
unsigned char ip_byte1 = (gip_net >> 24) & 0xFF;
unsigned char ip_byte2 = (gip_net >> 16) & 0xFF;
unsigned char ip_byte3 = (gip_net >> 8) & 0xFF;
unsigned char ip_byte4 = gip_net & 0xFF;
snprintf(buf, buf_len, "%u_%u_%u_%u", ip_byte1, ip_byte2, ip_byte3, ip_byte4);
return buf;
}
static void gdev_keepalive_stat_update(int tid, unsigned int gip_net, int type, int service_id, int action)
{
gdev_keepalive_status_per_thread_t *cur_thread_stat = &GDEV_KEEPALIVE_STAT[tid];
int bingo = 0, index;
for(index = 0; index < MAX_SUPPORT_GDEV_NUM; index++){
if((1 == cur_thread_stat->gdev_array[index].inuse_flag)
&& (gip_net == cur_thread_stat->gdev_array[index].gdev_ip_network_order)){
bingo = 1;
break;
}
}
/* not found this inline device */
if(0 == bingo){
/* search a empty place */
for(index = 0; index < MAX_SUPPORT_GDEV_NUM; index++){
if(0 == cur_thread_stat->gdev_array[index].inuse_flag){
cur_thread_stat->gdev_array[index].inuse_flag = 1;
break;
}
}
if(index >= MAX_SUPPORT_GDEV_NUM){
MESA_handle_runtime_log(gdev_kp_log_handle, RLOG_LV_FATAL, "[gdev_keepalive]",
"Too many inline devices, max support: %d!\n", MAX_SUPPORT_GDEV_NUM);
return;
}
}
if(service_id < 0 || service_id >= MAX_VXLAN_SERVICE_NUM){
MESA_handle_runtime_log(gdev_kp_log_handle, RLOG_LV_INFO, "[gdev_keepalive]",
"invalid service num: %d!\n", service_id);
return;
}
/* update this device */
cur_thread_stat->gdev_array[index].inuse_flag = 1;
cur_thread_stat->gdev_array[index].gdev_ip_network_order = gip_net;
cur_thread_stat->gdev_array[index].last_time = g_CurrentTime;
cur_thread_stat->gdev_array[index].keepalive_request_num++;
cur_thread_stat->gdev_array[index].keepalive_reply_num++;
/* update by service */
cur_thread_stat->gdev_array[index].service_array[service_id].gdev_keepalive_type = type;
cur_thread_stat->gdev_array[index].service_array[service_id].keepalive_request_num++;
if(action){
cur_thread_stat->gdev_array[index].service_array[service_id].keepalive_reply_num++;
}
cur_thread_stat->gdev_array[index].service_array[service_id].last_time = g_CurrentTime;
return;
}
/* �����豸�������BFDЭ�� */
static int gdev_keepalive_bfd_plug(const raw_pkt_t *raw_pkt, int thread_id, unsigned char dir, unsigned char *send_buf)
{
char payload_buf[1472];
struct mesa_ethernet_hdr *raw_eth_hdr = (struct mesa_ethernet_hdr *)raw_pkt->raw_pkt_data;
struct mesa_ip4_hdr *ip_hdr = (struct mesa_ip4_hdr *)((char *)raw_eth_hdr+ SENDPACKET_ETH_H);
struct mesa_udp_hdr *udp_hdr = (struct mesa_udp_hdr *)((char *)raw_eth_hdr + SENDPACKET_ETH_H + ip_hdr->ip_hl*4);
bfd_header_t *bfd_net_hdr = (bfd_header_t *)((char *)raw_eth_hdr + SENDPACKET_ETH_H + ip_hdr->ip_hl*4 + SENDPACKET_UDP_H);
bfd_header_t *bfd_send_hdr = (bfd_header_t *)payload_buf;
char bfd_buf[4];
int payload_len = ntohs(udp_hdr->uh_ulen) - SENDPACKET_UDP_H;
int service_id = 0;
int action;
service_id = vxlan_sport_map_to_service_id(ntohs(udp_hdr->uh_sport));
action = gdev_kp_action_judge(service_id);
/* ��ʹaction=0, stat_updateҲҪ����, ��Ҫͳ�Ʋ��ظ������ҵ����Ϣ */
gdev_keepalive_stat_update(thread_id, ip_hdr->ip_src.s_addr, GDEV_KEEPALIVE_TYPE_BFD, service_id, action);
if(0 == action){
return -1;
}
/* ��ȡ����service_id, ����Ӧ�ûظ�����Ӧ��, go on !! */
memcpy(payload_buf, bfd_net_hdr, payload_len);
// passive bfdd, DOWN->INIT, UP->UP
if (bfd_send_hdr->state == BFD_MSG_FLAGS_DOWN)
{
//DOWN->INIT
bfd_send_hdr->state = BFD_MSG_FLAGS_INIT;
// set keep_alive ip as discriminator
// memcpy(bfd_send_hdr->your_discriminator, &ip_hdr->ip_dst.s_addr, 4);
}
// swap discriminator
memcpy(bfd_buf, bfd_send_hdr->my_discriminator, 4);
memcpy(bfd_send_hdr->my_discriminator, bfd_send_hdr->your_discriminator, 4);
memcpy(bfd_send_hdr->your_discriminator, bfd_buf, 4);
sendpacket_build_ethernet(raw_eth_hdr->ether_shost, raw_eth_hdr->ether_dhost, ETH_P_IP, NULL, 0, send_buf);
sendpacket_build_udp_dual_stack(ntohs(udp_hdr->uh_sport), /* BFDЭ�鲻����Դ��Ŀ�Ķ˿� */
ntohs(udp_hdr->uh_dport),
(char *)bfd_send_hdr,
payload_len,
payload_len,
send_buf + SENDPACKET_ETH_H + SENDPACKET_IP_H);
sendpacket_build_ipv4(SENDPACKET_UDP_H + payload_len,
0,
ntohs(ip_hdr->ip_id),
ntohs(ip_hdr->ip_off),
ip_hdr->ip_ttl,
IPPROTO_UDP,
ip_hdr->ip_dst.s_addr,
ip_hdr->ip_src.s_addr, /* ����Դ��Ŀ��IP��ַ */
NULL, 0,
send_buf + SENDPACKET_ETH_H);
sendpacket_do_checksum(send_buf + SENDPACKET_ETH_H,
IPPROTO_IP,
SENDPACKET_IP_H);
sendpacket_do_checksum(send_buf + SENDPACKET_ETH_H,
IPPROTO_UDP,
payload_len + SENDPACKET_UDP_H);
return SENDPACKET_IP_H + SENDPACKET_UDP_H+sizeof(bfd_header_t) + sizeof(struct mesa_ethernet_hdr);
}
/* �����豸�������BFDЭ�� */
static int gdev_keepalive_icmp_plug(const raw_pkt_t *raw_pkt, int thread_id, unsigned char dir, unsigned char *send_buf)
{
struct mesa_ethernet_hdr *raw_eth_hdr = (struct mesa_ethernet_hdr *)raw_pkt->raw_pkt_data;
struct mesa_ip4_hdr *ip_hdr = (struct mesa_ip4_hdr *)((char *)raw_eth_hdr + SENDPACKET_ETH_H);
struct mesa_icmp_echo_hdr *icmp_hdr = (struct mesa_icmp_echo_hdr *)((char *)raw_eth_hdr + SENDPACKET_ETH_H + ip_hdr->ip_hl*4);
unsigned char *icmp_payload;
int icmp_payload_len;
gdev_keepalive_stat_update(thread_id, ip_hdr->ip_src.s_addr, GDEV_KEEPALIVE_TYPE_ICMP, 0, g_dev_keepalive_default_action);
icmp_payload = (unsigned char *)icmp_hdr + sizeof(struct mesa_icmp_echo_hdr);
icmp_payload_len = ntohs(ip_hdr->ip_len) - ip_hdr->ip_hl*4 - sizeof(struct mesa_icmp_echo_hdr);
sendpacket_build_ethernet(raw_eth_hdr->ether_shost, raw_eth_hdr->ether_dhost, ETH_P_IP, NULL, 0, send_buf);
sendpacket_build_icmpv4_echo(ICMP_ECHOREPLY, 0, 0,
ntohs(icmp_hdr->icd_id),
ntohs(icmp_hdr->icd_seq),
icmp_payload,
icmp_payload_len,
send_buf + SENDPACKET_ETH_H + SENDPACKET_IP_H);
sendpacket_build_ipv4(icmp_payload_len + sizeof(struct mesa_icmp_echo_hdr),
0, 0x1234, 0, 64, IPPROTO_ICMP,
ip_hdr->ip_dst.s_addr,
ip_hdr->ip_src.s_addr,
NULL, 0,
send_buf + SENDPACKET_ETH_H);
memcpy(send_buf + SENDPACKET_ETH_H + SENDPACKET_IP_H + sizeof(struct mesa_icmp_echo_hdr),
icmp_payload,
icmp_payload_len);
sendpacket_do_checksum(send_buf + SENDPACKET_ETH_H,
IPPROTO_IP,
SENDPACKET_IP_H);
sendpacket_do_checksum(send_buf + SENDPACKET_ETH_H,
IPPROTO_ICMP,
icmp_payload_len + sizeof(struct mesa_icmp_echo_hdr));
return ntohs(ip_hdr->ip_len) + sizeof(struct mesa_ethernet_hdr);
}
static int gdev_keepalive_plug(const raw_pkt_t *raw_pkt, int thread_id, unsigned char dir, int keepalive_type)
{
int ret = -1, send_len;
MESA_send_handle *snd_handle = NULL;
#if IOMODE_MARSIO
const struct mesa_ethernet_hdr *raw_eth_hdr = (struct mesa_ethernet_hdr *)raw_pkt->raw_pkt_data;
#endif
snd_handle = packet_io_get_send_handle(thread_id);
if(NULL == snd_handle){
return -1;
}
snd_handle->send_buf = packet_io_get_sendbuf(SEND_TYPE_LINK_INJECT, thread_id);
if(NULL == snd_handle->send_buf){
return 0;
}
switch(keepalive_type){
case GDEV_KEEPALIVE_TYPE_BFD:
send_len = gdev_keepalive_bfd_plug(raw_pkt, thread_id, dir, snd_handle->send_buf);
if(send_len < 0){
goto err;
}
break;
case GDEV_KEEPALIVE_TYPE_ICMP:
send_len = gdev_keepalive_icmp_plug(raw_pkt, thread_id, dir, snd_handle->send_buf);
if(send_len < 0){
goto err;
}
break;
default:
return -1;
}
ret = packet_io_send(snd_handle, send_len, SEND_TYPE_LINK_INJECT,
__ADDR_TYPE_IP_PAIR_V4, dir, thread_id,
(char *)KILL_TCP_PHONY_POINTER, (int *)KILL_TCP_PHONY_POINTER, raw_pkt);
packet_io_free_sendbuf(SEND_TYPE_LINK_INJECT, thread_id);
return ret;
err:
packet_io_free_sendbuf(SEND_TYPE_LINK_INJECT, thread_id);
return -1;
}
/* �ڴ���UDP����, ����ʶ����Щ������BFD����� */
static inline int gdev_bfd_pkt_identify(const struct streaminfo *a_udp)
{
if(g_topology_mode != NET_CONN_SERIAL_GDEV){ /* ��G����ģʽ */
return 0;
}
if((sendto_gdev_card_ip != a_udp->addr.tuple4_v4->daddr)
&& (sendto_gdev_card_ip != a_udp->addr.tuple4_v4->saddr)){
return 0;
}
if(BFD_DEFAULT_PORT != ntohs(a_udp->addr.tuple4_v4->dest)){
return 0;
}
if(ADDR_TYPE_IPV4 != a_udp->addr.addrtype){ /* Ŀǰ������IPv4 */
return 0;
}
if(a_udp->pudpdetail->datalen != (int)sizeof(bfd_header_t)){
return 0;
}
return 1;
}
static long _gdev_keepalive_rcv_pps;
static long _gdev_keepalive_snd_pps;
static void __gdev_keepalive_pkt_calc_pps(void)
{
static long _last_rcv_num, _last_snd_num;
long rcv_sum = 0;
long snd_sum = 0;
int tid, dev_num;
for(tid = 0; tid < g_packet_io_thread_num; tid++){
for(dev_num = 0; dev_num < MAX_SUPPORT_GDEV_NUM; dev_num++){
if(GDEV_KEEPALIVE_STAT[tid].gdev_array[dev_num].inuse_flag != 0){
rcv_sum += GDEV_KEEPALIVE_STAT[tid].gdev_array[dev_num].keepalive_request_num;
snd_sum += GDEV_KEEPALIVE_STAT[tid].gdev_array[dev_num].keepalive_reply_num;
}
}
}
_gdev_keepalive_rcv_pps = rcv_sum - _last_rcv_num;
_gdev_keepalive_snd_pps = snd_sum - _last_snd_num;
_last_rcv_num = rcv_sum;
_last_snd_num = snd_sum;
return;
}
static void gdev_keepalive_update_local_log(void)
{
FILE *gdev_status_fp;
int dev_index, thread_seq, service_index;
int platform_thread_num;
char cur_time_str[64];
char tmp_ip_str[INET6_ADDRSTRLEN];
const char *tmp_keealive_type;
const gdev_keepalive_status_per_thread_t *this_thread_stat;
const gdev_keepalive_status_per_dev_t *this_dev_stat;
gdev_status_fp = fopen(ABBR_INLINE_KEEPALIVE_LOG_DATA_FILE, "w+");
if(NULL == gdev_status_fp){
return;
}
platform_thread_num = get_thread_count();
timet_to_str(time(NULL), cur_time_str, 64);
fprintf(gdev_status_fp, "%-20s %-16s %-11s %-5s %-5s %-16s %-16s\n",
"Last-update-time", "Gdev-ip", "Service-Num", "Ctrl", "Type", "Rcv-pkt", "Snd-pkt");
for(thread_seq = 0; thread_seq < platform_thread_num; thread_seq++)
{
this_thread_stat = &GDEV_KEEPALIVE_STAT[thread_seq];
for(dev_index = 0; dev_index < MAX_SUPPORT_GDEV_NUM; dev_index++){
if(0 == this_thread_stat->gdev_array[dev_index].inuse_flag){
continue;
}
this_dev_stat = &this_thread_stat->gdev_array[dev_index];
inet_ntop(AF_INET, &this_dev_stat->gdev_ip_network_order, tmp_ip_str, sizeof(tmp_ip_str));
for(service_index = 0; service_index < MAX_VXLAN_SERVICE_NUM; service_index++){
if(this_dev_stat->service_array[service_index].keepalive_request_num > 0){
if(GDEV_KEEPALIVE_TYPE_ICMP == this_dev_stat->service_array[service_index].gdev_keepalive_type){
tmp_keealive_type = "ICMP";
}else if(GDEV_KEEPALIVE_TYPE_BFD == this_dev_stat->service_array[service_index].gdev_keepalive_type){
tmp_keealive_type = "BFD";
}else{
tmp_keealive_type = "Unknown";
}
fprintf(gdev_status_fp, "%-20s %-16s %-11d %-5d %-5s %-16u %-16u\n",
cur_time_str,
tmp_ip_str,
service_index,
gdev_kp_action_judge(service_index),
tmp_keealive_type,
this_dev_stat->service_array[service_index].keepalive_request_num,
this_dev_stat->service_array[service_index].keepalive_reply_num);
}
}
}
}
fclose(gdev_status_fp);
}
static void gdev_keepalive_clear_stat(void)
{
int platform_thread_num, dev_index, thread_seq, service_index;
gdev_keepalive_status_per_thread_t *this_thread_stat;
gdev_keepalive_status_per_dev_t *this_dev_stat;
platform_thread_num = get_thread_count();
for(thread_seq = 0; thread_seq < platform_thread_num; thread_seq++){
this_thread_stat = &GDEV_KEEPALIVE_STAT[thread_seq];
for(dev_index = 0; dev_index < MAX_SUPPORT_GDEV_NUM; dev_index++){
if(0 == this_thread_stat->gdev_array[dev_index].inuse_flag){
continue;
}
this_dev_stat = &this_thread_stat->gdev_array[dev_index];
this_dev_stat->keepalive_request_num = 0;
this_dev_stat->keepalive_reply_num = 0;
#if 0 /* �Ȳ�֧����̭, ��Ϊ��ǰ�豸��line_id�Ѿ�ע�ᵽfs2, ����ʹ�õĻ�, name������ */
if(this_dev_stat->last_time + MAX_NO_PKT_TIMEOUT < g_CurrentTime){
char dev_ip_str[INET6_ADDRSTRLEN];
this_dev_stat->inuse_flag = 0;
inet_ntop(AF_INET, &this_dev_stat->gdev_ip_network_order, dev_ip_str, sizeof(dev_ip_str));
MESA_handle_runtime_log(gdev_kp_log_handle, RLOG_LV_INFO, "[gdev_keepalive]", " %s timeout, delete it.\n", dev_ip_str);
}
#endif
for(service_index = 0; service_index < MAX_VXLAN_SERVICE_NUM; service_index++){
if(this_dev_stat->service_array[service_index].keepalive_request_num > 0){
this_dev_stat->service_array[service_index].keepalive_request_num = 0;
this_dev_stat->service_array[service_index].keepalive_reply_num = 0;
}
}
}
}
}
static void *gdev_keepalive_log_thread(void *arg)
{
time_t last_op_time = time(NULL);
MESA_mkdir_p(sapp_global_val->config.data_file_path.data_files_root_dir, 0755);
while(sapp_get_current_state() < SAPP_STATE_PROCESSING){
sapp_usleep(100);
}
while(sapp_get_current_state() == SAPP_STATE_PROCESSING){
while(last_op_time == time(NULL)){
sapp_usleep(100);
}
last_op_time = time(NULL);
__gdev_keepalive_pkt_calc_pps();
gdev_keepalive_update_local_log();
//gdev_keepalive_clear_stat(); /* ÿ����ռ���, ֻ����һ��ʱ������� */
}
return NULL;
}
static int gdev_kp_update_service_ctrl(const struct gdev_keepalive_service_ctrl *service_ctrl)
{
if((service_ctrl->service_num < 0) || (service_ctrl->service_num >= MAX_VXLAN_SERVICE_NUM)){
return -1;
}
g_dev_keepalive_service_ctrl_array[service_ctrl->service_num].keepalive_switch = service_ctrl->keepalive_switch;
return 0;
}
/* vxlan�������, vpn-id, Դ�˿ں�ҵ��ID֮��Ĺ�ϵ, �����ļ���ʽ:
service-id VPN-id Sport-range
1 101 50000-50016
2 102 50064-50080
......
���ڸ�֪�ϲ�Ӧ��ҵ���, �ʹ�������ҵ���Ƿ���Ҫ����.
*/
static int vxlan_sport_service_map_update(int service_id, unsigned short begin_sport_host, unsigned short end_sport_host)
{
unsigned short i;
for(i = begin_sport_host; i <= end_sport_host;i++){
g_vxlan_sport_to_service_id[i] = service_id;
g_dev_keepalive_service_ctrl_array[service_id].keepalive_switch = 1;
}
return 0;
}
static int vxlan_sport_service_map_init(void)
{
char line_buf[1024];
FILE *fp;
int tmp_service_id, tmp_int;
const char *delim = "\t- ";
char *save_ptr;
char *section;
unsigned short begin_sport, end_sport;
fp = fopen(ABBR_VXLAN_SPORT_MAP_CONF_FILE, "r");
if(NULL == fp){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "open vxlan sport map file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
return -1;
}
while(fgets(line_buf, 1024, fp) != NULL){
if('#' == line_buf[0]){
continue;
}
del_last_rn(line_buf, 1024);
/* service-id */
section = strtok_r(line_buf, delim, &save_ptr);
if(NULL == section){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "parse file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
goto done;
}
tmp_service_id = atoi(section);
/* VPN-id, ��ʱ����ת��, ֱ�Ӽ�100����ҵ��� */
section = strtok_r(NULL, delim, &save_ptr);
if(NULL == section){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "parse file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
goto done;
}
/* begin-port */
section = strtok_r(NULL, delim, &save_ptr);
if(NULL == section){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "parse file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
goto done;
}
tmp_int = atoi(section);
if(tmp_int <= 0 || tmp_int > 65535){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "parse file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
goto done;
}
begin_sport = (unsigned short)tmp_int;
/* end-port */
section = strtok_r(NULL, delim, &save_ptr);
if(NULL == section){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "parse file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
goto done;
}
tmp_int = atoi(section);
if(tmp_int <= 0 || tmp_int > 65535){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "parse file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
goto done;
}
end_sport = (unsigned short)tmp_int;
if(end_sport < begin_sport){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "parse file %s error!\n", ABBR_VXLAN_SPORT_MAP_CONF_FILE);
goto done;
}
while(strtok_r(NULL, delim, &save_ptr));
vxlan_sport_service_map_update(tmp_service_id, begin_sport, end_sport);
}
done:
fclose(fp);
return 0;
}
#ifdef __cplusplus
extern "C" {
#endif
/* �������vxlanģʽ��, ���ݰ��Ƿ�������� */
int vxlan_packet_is_myself(const raw_pkt_t *rawpkt)
{
struct mesa_ip4_hdr *ip4h = (struct mesa_ip4_hdr *)((char *)rawpkt->raw_pkt_data + sizeof(struct mesa_ethernet_hdr));
if(sendto_gdev_card_ip == ip4h->ip_dst.s_addr){
return 1;
}
return 0;
}
unsigned char vxlan_sport_map_to_service_id(unsigned short sport_host_order)
{
return g_vxlan_sport_to_service_id[sport_host_order];
}
unsigned char vxlan_id_map_to_service_id(int vxlan_id)
{
return (unsigned char)(vxlan_id - 100);
}
int gdev_keepalive_set_opt(const SAPP_TLV_T *tlv_value)
{
int ret = 0;
static time_t last_log_time = 0;
if((NULL == tlv_value) || (tlv_value->length == 0)){
return -1;
}
switch(tlv_value->type){
case GDEV_KEEPALIVE_OPT_SERVICE_CTRL:
{
if(tlv_value->length != sizeof(struct gdev_keepalive_service_ctrl)){
return -1;
}
const struct gdev_keepalive_service_ctrl *actual_val = (const struct gdev_keepalive_service_ctrl *)tlv_value->ptr_value;
ret = gdev_kp_update_service_ctrl(actual_val);
if(ret < 0){
if(last_log_time < g_CurrentTime){
last_log_time = g_CurrentTime;
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "set gdev-keepalive error! service:%d, action:%d",
actual_val->service_num, actual_val->keepalive_switch);
}
}else{
if(last_log_time < g_CurrentTime){
last_log_time = g_CurrentTime;
MESA_handle_runtime_log(gdev_kp_log_handle, 10, "[gdev_keepalive]", "set gdev-keepalive, service:%d, action:%d",
actual_val->service_num, actual_val->keepalive_switch);
}
}
}
break;
case GDEV_KEEPALIVE_OPT_GLOBAL_SWITCH:
{
if(tlv_value->length != sizeof(int)){
return -1;
}
g_dev_keepalive_flag = tlv_value->int_value;
if(last_log_time < g_CurrentTime){
MESA_handle_runtime_log(gdev_kp_log_handle, 20, "[gdev_keepalive]", "set global keepalive to %d", tlv_value->int_value);
last_log_time = g_CurrentTime;
}
}
break;
default:
ret = -2;
break;
}
return ret;
}
int gdev_keepalive_get_opt(SAPP_TLV_T *tlv_value)
{
int ret = 0;
if((NULL == tlv_value) || (tlv_value->length == 0)){
return -1;
}
switch(tlv_value->type){
case GDEV_KEEPALIVE_OPT_GLOBAL_SWITCH:
{
tlv_value->int_value = g_dev_keepalive_flag;
}
break;
case GDEV_KEEPALIVE_OPT_RCV_PKT_PPS:
{
tlv_value->long_value = _gdev_keepalive_rcv_pps;
}
break;
case GDEV_KEEPALIVE_OPT_SND_PKT_PPS:
{
tlv_value->long_value = _gdev_keepalive_snd_pps;
}
break;
default:
ret = -1;
break;
}
return ret;
}
char gdev_keepalive_udp_entry(const struct streaminfo *a_udp, void **pme, int thread_seq, const void *ip_hdr)
{
int entry_ret = APP_STATE_GIVEME;
int kp_ret;
const struct streaminfo_private *a_udp_pr;
if(OP_STATE_PENDING == a_udp->opstate){
if(0 == gdev_bfd_pkt_identify(a_udp)){
return APP_STATE_DROPME;
}
}else if(OP_STATE_CLOSE == a_udp->opstate){
return APP_STATE_DROPME;
}
a_udp_pr = (const struct streaminfo_private *)a_udp;
kp_ret = gdev_keepalive_plug(a_udp_pr->raw_pkt, a_udp->threadnum, a_udp->routedir, GDEV_KEEPALIVE_TYPE_BFD);
if(kp_ret < 0){
/* ������, socket�ȴ����ܷ���APP_STATE_DROPME, ����DROP ME����Ҳ�ò������������, ���Dz������ͨ��gdev_keepalive_set_opt()�رձ����, ����Ҫ����DROPPKT, ���ܰ��������ע */
entry_ret = APP_STATE_GIVEME | APP_STATE_DROPPKT;
}else{
entry_ret = APP_STATE_GIVEME | APP_STATE_DROPPKT; /* �����, ������ǰ�� */
}
return entry_ret;
}
char gdev_keepalive_ip_entry(const struct streaminfo *pstream,unsigned char routedir,int thread_seq, const struct mesa_ip4_hdr *ipv4_hdr)
{
const struct mesa_icmp_echo_hdr *icmp_hdr;
const struct streaminfo_private *pstream_pr;
int entry_ret = APP_STATE_GIVEME;
int kp_ret;
if(ipv4_hdr->ip_p != IPPROTO_ICMP){
return APP_STATE_DROPME;
}
if(ipv4_hdr->ip_dst.s_addr != sendto_gdev_card_ip){
return APP_STATE_DROPME;
}
icmp_hdr = (struct mesa_icmp_echo_hdr *)((char *)ipv4_hdr + ipv4_hdr->ip_hl*4);
if(icmp_hdr->icmp_type != ICMP_ECHO){
return APP_STATE_DROPME;
}
pstream_pr = (const struct streaminfo_private *)pstream;
kp_ret = gdev_keepalive_plug(pstream_pr->raw_pkt, thread_seq, pstream->routedir, GDEV_KEEPALIVE_TYPE_ICMP);
if(kp_ret < 0){
entry_ret = APP_STATE_DROPME | APP_STATE_DROPPKT;
}else{
entry_ret = APP_STATE_GIVEME | APP_STATE_DROPPKT; /* �����, ������ǰ�� */
}
return entry_ret;
}
int gdev_keepalive_plug_init(void)
{
char tmp_ip_buf[16];
char deploy_mode_string[32];
int opt_len;
int i;
gdev_kp_log_handle = sapp_global_val->individual_fixed.log_handle;
/* ���߳�ģʽ��α��������, 64B������� */
assert(sizeof(gdev_keepalive_status_per_thread_t) % 64 == 0);
opt_len = sizeof(deploy_mode_string);
if(sapp_get_platform_opt(SPO_DEPLOYMENT_MODE_STR, (void *)deploy_mode_string, &opt_len) < 0){
MESA_handle_runtime_log(gdev_kp_log_handle, 30, "[gdev_keepalive]",
"can't get sapp deployment mode!\n");
return -1;
}
if(strncasecmp(deploy_mode_string, "inline", strlen("inline")) != 0){ /* ��G����ģʽ */
MESA_handle_runtime_log(gdev_kp_log_handle, 30, "[gdev_keepalive]", "not inline mode, can't load gdev_keepalive.so\n");
return -1;
}
memset(g_vxlan_sport_to_service_id, 0, sizeof(g_vxlan_sport_to_service_id));
memset(tmp_ip_buf, 0, 16);
MESA_load_profile_string_nodef((char *)ABBR_INLINE_DEV_CONF_FILE, (char *)"Module", (char *)"sendto_gdev_ip", tmp_ip_buf, 16);
if(tmp_ip_buf[0] == '\0'){
MESA_handle_runtime_log(gdev_kp_log_handle, 30, "[gdev_keepalive]","can't get 'sendto_gdev_ip'");
return -1;
}
inet_pton(AF_INET, tmp_ip_buf, &sendto_gdev_card_ip);
MESA_load_profile_int_def((char *)ABBR_INLINE_DEV_CONF_FILE, (char *)"Module", (char *)"default_keepalive_action", &g_dev_keepalive_default_action, 1);
for(i = 0; i < MAX_VXLAN_SERVICE_NUM; i++){
g_dev_keepalive_service_ctrl_array[i].service_num = i;
g_dev_keepalive_service_ctrl_array[i].keepalive_switch = g_dev_keepalive_default_action;
}
vxlan_sport_service_map_init();
pthread_create(&sapp_global_val->individual_fixed.gdev_keepalive_log_thread_id, NULL, gdev_keepalive_log_thread, NULL);
return 0;
}
void gdev_keepalive_plug_destroy(void)
{
if(DEPLOYMENT_MODE_INLINE != sapp_global_val->config.packet_io.deployment_mode_bin){
return;
}
if(sapp_global_val->individual_fixed.gdev_keepalive_log_thread_id){
pthread_join(sapp_global_val->individual_fixed.gdev_keepalive_log_thread_id, NULL);
sapp_global_val->individual_fixed.gdev_keepalive_log_thread_id = 0;
}
memset(GDEV_KEEPALIVE_STAT, 0, sizeof(GDEV_KEEPALIVE_STAT));
memset((void *)g_dev_keepalive_service_ctrl_array, 0, sizeof(g_dev_keepalive_service_ctrl_array));
g_dev_keepalive_flag = 1;
g_dev_keepalive_default_action = 1;
}
#ifdef __cplusplus
}
#endif
|