diff options
| author | dump2file <[email protected]> | 2019-01-24 15:00:17 +0600 |
|---|---|---|
| committer | dump2file <[email protected]> | 2019-01-24 15:00:17 +0600 |
| commit | d7a6f7ff4cb7bc4f353deba13fe5b9f46e8afc9f (patch) | |
| tree | bedae201397d075ab97982a5c0e2a12a414e60ba | |
| parent | 63221dc65ca07d5ac0a4d77557e9150c0de85a7e (diff) | |
close #9 marsio模式下sysinfo增加mbuf使用统计输入,合并fs2输出单包处理延迟histogram功能
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| -rw-r--r-- | dealpkt/stream_manage.c | 5168 | ||||
| -rw-r--r-- | entry/sapp_init.c | 785 | ||||
| -rw-r--r-- | include/stream_internal.h | 685 | ||||
| -rw-r--r-- | inner_plug/sapp_assistant.cpp | 1042 | ||||
| -rw-r--r-- | packet_io/packet_io.c | 2000 | ||||
| -rw-r--r-- | packet_io/packet_io_marsio.c | 12 | ||||
| -rw-r--r-- | packet_io/packet_io_status.cpp | 46 |
8 files changed, 4926 insertions, 4817 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e0f99f..6ace2a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,11 @@ include(Version) set(CMAKE_MACOSX_RPATH 0) +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG") +endif() + #set(CMAKE_INSTALL_PREFIX /home/ceiec/sapp/) set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/run/) diff --git a/dealpkt/stream_manage.c b/dealpkt/stream_manage.c index c210889..788a6d7 100644 --- a/dealpkt/stream_manage.c +++ b/dealpkt/stream_manage.c @@ -1,2575 +1,2593 @@ -
-/*
-*********************************************
-1. 20130803 ������
-1) ԭ����������hash����������Ĺ���ͬһkeyֵ�ϣ�
- ��Ϊ������Ԫ��Ĵ�С��������hash����ͬһ�������ϣ�
-2) ͨ���� findandsethashindex ����ʱԤ��Ҫ������������������ٲ���ʱ�����²��
-
-
-2 . adjust by lqy 20130917 streamaddlist
-1) ��������̭֮����Ҫ���¼���Ҫ�����λ��
-2) ÿ����̭��������ĿΪ��������Ŀ��2%
-
-3 . adjust by lqy 20140810
-1) ������̭ʱ�������100��
-
-4. adjust by lqy 20141203
-1)lruʱ����������һ���Ƿ�ʱ����ʱ������̭
-
-5.adjust by lqy 20141212
-1)Ϊÿ��������ȫ��Ψһ����stream_index ����ͨ���������
-2)�Ƿ�֧��ȫ���ӿ��ɿ����趨
-3)ÿ�����Ӿ�����lru
-4) fix hash��ͻʱ�������������
-*/
-#include "stream_internal.h"
-#include "stream_manage.h"
-#include "sysinfo.h"
-#include "packet_io.h"
-#include "packet_io_internal.h"
-#include "project_requirement.h"
-#include "project_internal.h"
-#include "dictator.h"
-#include <MESA/MESA_handle_logger.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <assert.h>
-#include <string.h>
-#include <arpa/inet.h>
-#include <pthread.h>
-#include <sys/select.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int Stream_Analyse_Process_Platform_version_VERSION_20181116; /* nm sapp | grep version */
-int sapp_args_v = 20181116; /* ./sapp -v */
-
-/*
- ����İ汾������packet_io_libУ��, ��ֹƽ̨��dl.so��ƥ�䵼�������쳣, ��������BUGδ����.
-*/
-#if IOMODE_PCAP
-int sapp_packet_io_v = 20170120;
-#elif IOMODE_PAG
-int sapp_packet_io_v = 20151106;
-#elif IOMODE_PFRING
-int sapp_packet_io_v = 20160505;
-#elif IOMODE_DPDK
-int sapp_packet_io_v = 20160929;
-#elif IOMODE_IPFILE
-int sapp_packet_io_v = 20160426;
-#elif IOMODE_TOPSEC
-int sapp_packet_io_v = 20151014;
-#elif IOMODE_MARSIO
-int sapp_packet_io_v = 20180716;
-#elif IOMODE_SMITH
-int sapp_packet_io_v = 20161027;
-#elif IOMODE_DPDK_VXLAN
-int sapp_packet_io_v = 20161101;
-#elif IOMODE_PAG_MARSIO
-int sapp_packet_io_v = 20170407;
-#endif
-
-int g_sapp_log_level = 20;
-void *g_sapp_log_handle;
-
-#define STREAM_ONCE_DEL_NUM 1000
-void packet_io_status_stream_hash_update(int thread_seq, unsigned long hash_list_num);
-void packet_io_status_stream_update(int tid, UCHAR stream_type, UCHAR stream_state, int action);
-static inline void update_stream_status(UCHAR threadnum, UCHAR type, UCHAR stream_state, int action);
-int project_requirement_global_init(void);
-static inline void __do_streamleavlist(struct streamindex *pindex,struct stream_list *plist);
-
-int tcp_stream_table_size=0;
-int udp_stream_table_size=0;
-//add by lqy
-unsigned short udp_reset_time=0;
-unsigned short link_default_nopkt_time=0;
-int tcpstate_num[MAX_TCP_STATE]={8000,2000,4000};
-int udpstate_num[MAX_UDP_STATE]={2000,2000,2000};
-
-int tcp_default_unorder=5;
-int tcp_creatlink_model=TCP_CTEAT_LINK_BYSYN;
-int tcp_flood_detect_model=TCP_FLOOD_DETECT_OFF;
-int tcp_support_all=TCP_SUPPORT_ENTRYALL_OFF;
-int tcp_dataflood_pktlen=5;
-extern int g_packet_io_thread_num;
-struct global_stream **G_MESA_GLOBAL_STREAM;
-
-//static struct stream_status stream_info[MAX_THREAD_NUM];
-
-static int init_stream_detail(struct global_stream *g_stream)
-{
- int i=0;
- memset(g_stream,0,sizeof(struct global_stream));
- for(i=0;i<MAX_TCP_STATE;i++)
- {
- g_stream->tcpList[i].max_cnt=tcpstate_num[i];
-
- //add by lqy����ÿ�λ��������ĿΪ100
- g_stream->tcpList[i].sift_out_cnt=g_stream->tcpList[i].max_cnt/50+1;
-#if 0
- if(g_stream->tcpList[i].sift_out_cnt>100)
- g_stream->tcpList[i].sift_out_cnt=100;
-#else
- g_stream->tcpList[i].sift_out_cnt=1; /* lijia modify, ���ٵ�����ʱ̫��, ÿ��ֻ��̭һ�� */
-#endif
- g_stream->tcpList[i].cnt=0;
- }
- for(i=0;i<MAX_UDP_STATE;i++)
- {
- g_stream->udpList[i].max_cnt=udpstate_num[i];
- //add by lqy����ÿ�λ��������ĿΪ100
- g_stream->udpList[i].sift_out_cnt=g_stream->udpList[i].max_cnt/50+1;
-#if 0
- if(g_stream->udpList[i].sift_out_cnt>100)
- g_stream->udpList[i].sift_out_cnt=100;
-#else
- g_stream->udpList[i].sift_out_cnt=1; /* lijia modify, ���ٵ�����ʱ̫��, ÿ��ֻ��̭һ�� */
-#endif
- g_stream->udpList[i].cnt=0;
- }
- g_stream->freeList.max_cnt=tcp_stream_table_size+udp_stream_table_size;
- g_stream->freeList.cnt=0;
-
- //Ԥ�ȷ���hashָ��
-#if USE_RBTREE_INSTEAD_LIST
- g_stream->tcp_rb_root_table = (struct rb_root *)malloc (tcp_stream_table_size * sizeof (struct rb_root));
- memset(g_stream->tcp_rb_root_table, 0, tcp_stream_table_size * sizeof (struct rb_root));
-
- g_stream->udp_rb_root_table = (struct rb_root *)malloc (tcp_stream_table_size * sizeof (struct rb_root));
- memset(g_stream->udp_rb_root_table, 0, tcp_stream_table_size * sizeof (struct rb_root));
-#else
- g_stream->tcp_stream_table = (struct streamindex **)malloc (tcp_stream_table_size * sizeof (char *));
- memset (g_stream->tcp_stream_table, 0, tcp_stream_table_size* sizeof (char *));
- g_stream->udp_stream_table = (struct streamindex **)malloc (udp_stream_table_size * sizeof (char *));
- memset (g_stream->udp_stream_table, 0, udp_stream_table_size* sizeof (char *));
- //��¼hash��״̬
- g_stream->tcp_stream_talbe_hash_count = (unsigned short *)malloc (tcp_stream_table_size* sizeof (short));
- memset (g_stream->tcp_stream_talbe_hash_count, 0, tcp_stream_table_size* sizeof (short));
- g_stream->udp_stream_talbe_hash_count = (unsigned short *)malloc (udp_stream_table_size* sizeof (short));
- memset (g_stream->udp_stream_talbe_hash_count, 0, udp_stream_table_size* sizeof (short));
-#endif
-
- g_stream->freeList.head=(struct streamindex *) malloc ((g_stream->freeList.max_cnt) * sizeof (struct streamindex));
- memset(g_stream->freeList.head, 0,(g_stream->freeList.max_cnt) * sizeof (struct streamindex));
- for (i = 0; i < g_stream->freeList.max_cnt-1; i++)
- {
- g_stream->freeList.head[i].next= &(g_stream->freeList.head[i + 1]);
- g_stream->freeList.head[i+1].prev=&(g_stream->freeList.head[i]);
- //g_stream->freeList.index=i; // Ϊÿ����������һ��ȫ��������
- //adjust by lqy 20150113
- g_stream->freeList.head[i].stream.stream_public.stream_index=i;
- }
- g_stream->freeList.head[g_stream->freeList.max_cnt-1].next=NULL;
- //adjust by lqy 20150113
- g_stream->freeList.head[g_stream->freeList.max_cnt-1].stream.stream_public.stream_index=i;
- g_stream->freeList.cnt= g_stream->freeList.max_cnt;
- g_stream->freeList.tail= &(g_stream->freeList.head[g_stream->freeList.max_cnt-1]);
-
- return 0;
-}
-
-
-int tcp_set_flood_detect_model(int model)
-{
- tcp_flood_detect_model=model;
- return 0;
-}
-
-int tcp_set_creatlink_model(int model)
-{
- tcp_creatlink_model=model;
- return 0;
-}
-//�����Ƿ���tcpallģʽ
-int tcp_set_support_tcpall_model(int model)
-{
- tcp_support_all=model;
- return 0;
-}
-int tcp_set_default_unorder_num(unsigned short max_unorder)
-{
- tcp_default_unorder=max_unorder;
- return 0;
-}
-
-int tcp_set_max_unorder(int max_unorder) //2014-12-25 lijia add, ���ݾ�ƽ̨, xj_fd
-{
- tcp_default_unorder=max_unorder;
- return 0;
-}
-
-int tcp_set_stream_num(int tcpsyn,int tcpdata,int tcpnouse)
-{
-
- tcpstate_num[TCP_SYN_STATE]=tcpsyn;
- tcpstate_num[TCP_DATA_STATE]=tcpdata;
- tcpstate_num[TCP_NOUSE_STATE]=tcpnouse;
- return 0;
-}
-void stream_set_default_nopkt_time(unsigned short timeout)
-{
- link_default_nopkt_time=timeout;
-}
-
-static inline UINT64 stream_get_detail_last_mtime(const struct streamindex *pindex)
-{
- UINT64 last_mtime;
-
- if(STREAM_TYPE_TCP == pindex->stream.stream_public.type){
- last_mtime = pindex->stream.stream_public.ptcpdetail->lastmtime;
- }else{
- last_mtime = pindex->stream.stream_public.pudpdetail->lastmtime;
- }
-
- return last_mtime;
-}
-
-static void stream_timeout_shift_lrulist(struct streamindex *pindex, unsigned short new_timeout)
-{
- unsigned short global_default_timeout;
- struct streamindex *tmp_index;
- struct stream_list *lru_list_root;
- const struct streaminfo *pstream = &pindex->stream.stream_public;
-
-//long search_times = 0;
-
- if(STREAM_TYPE_TCP == pindex->stream.stream_public.type){
- global_default_timeout = link_default_nopkt_time;
- lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->tcpList[pstream->stream_state]);
- }else{
- global_default_timeout = udp_reset_time;
- lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->udpList[pstream->stream_state]);
- }
-
- if(lru_list_root->head == lru_list_root->tail){ /* ��ǰ��ʣ���һ����, �����ƶ� */
- return;
- }
-
- assert(lru_list_root->head);
- assert(lru_list_root->tail);
- __do_streamleavlist(pindex, lru_list_root); /* ��ԭλ��ժ�� */
- assert(lru_list_root->head);
- assert(lru_list_root->tail);
-
- if(global_default_timeout > 0){
- /*
- ���������ȫ�ֳ�ʱʱ��, �����Ľڵ�Ӧ�ô�tail->head�������,
- ��Ϊ�����Ľڵ㳬ʱʱ�䳤, �����ǵ�ǰ��������, �϶�������Ĭ�ϵ���, �Լ�����������ù���ʱ����������ʱ.
- */
- /*
- �����ǰ�����õ��³�ʱʱ��, ��LRUĩβ����ʣ��ʱ��(�ް�����ʱ��)����,
- ���仰˵, ��������̭������ʱ��Ҳ��LRUĩβ����Ҫ�ͺ�,
- ������Ҫ��tail(ʱ����µ�)�����ƶ� .
- */
- /* after-insert : head->pindex->tmp_index->tail, ����tmp_index֮ǰ */
- tmp_index = lru_list_root->tail; /* �����timeoutֵ��, �Ӳ��ױ���̭��, ��tail�ڵ㿪ʼ�Ƚ� */
- while(tmp_index
- && (new_timeout < (tmp_index->stream.timeout -(g_CurrentTime -stream_get_detail_last_mtime(tmp_index))))){ /* �µij�ʱʱ�� < tmp����ʣ������ʱ�� */
-//printf("##### serach udp timeout place, %ld.....\n", search_times++);
- tmp_index = tmp_index->prev;
- }
-
- /* ������� */
- if(NULL == tmp_index){ /* ��tail��ʼһֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�head */
- lru_list_root->head->prev = pindex;
- pindex->next = lru_list_root->head;
- lru_list_root->head = pindex;
- pindex->prev = NULL;
- }else{
- if(tmp_index->prev){
- tmp_index->prev->next = pindex;
- }else{
- lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */
- }
- pindex->prev = tmp_index->prev;
- tmp_index->prev = pindex;
- pindex->next = tmp_index;
- }
- }else{
- /*
- ���û��������ȫ�ֳ�ʱʱ��, ����ΪĬ�ϳ�ʱʱ��ʱ����, ����������֮���ǿ����̭������.
- ��ô�����Ľڵ�Ӧ�ô�head->tail�������,
- ��Ϊ�����Ľڵ㳬ʱʱ���Ҳ��65535��, �϶������е������糬ʱ.
- */
- tmp_index = lru_list_root->head; /* �����timeout��Ĭ��0�����ױ���̭ */
- while(tmp_index
- && (tmp_index->stream.timeout != 0) /* ֮ǰij����Ҳ���������˳�ʱʱ�� */
- && (new_timeout > (tmp_index->stream.timeout - (g_CurrentTime -stream_get_detail_last_mtime(tmp_index))))){ /* �µij�ʱʱ�� > tmp����ʣ������ʱ�� */
-//printf("##### serach udp timeout place, %ld.....\n", search_times++);
- tmp_index = tmp_index->next;
- }
-
- /* ������� */
- if(NULL == tmp_index){ /* ��head��ʼһֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�tail */
- lru_list_root->tail->next = pindex;
- pindex->prev = lru_list_root->tail;
- lru_list_root->tail = pindex;
- pindex->next = NULL;
- }else{
- if(tmp_index->prev){
- tmp_index->prev->next = pindex;
- }else{
- lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */
- }
- pindex->prev = tmp_index->prev;
- tmp_index->prev = pindex;
- pindex->next = tmp_index;
- }
- }
-
- lru_list_root->cnt++; /* ��__do_streamleavlist()�м�1, �˴��ָ����� */
- return;
-}
-
-/*
- NOTE:
- �����õij�ʱʱ������ȫ�ֱ�timeoutֵ��!!
- ��Ϊȫ����������ֻ��һ��LRU_time����,
- ������ֵ�ܴ�, ���Խ���ǰ����LRUĩβ�����ʵ�λ��,
- �������ǰֵ��ȫ�ֱ�С, ������Ҫ��LRU������tail����ѯ�ܶ���������ҵ��ʵ�λ��,
- ���µ�����ʱ̫��.
-*/
-int stream_set_single_stream_timeout(const struct streaminfo *pstream,unsigned short timeout)
-{
- int ret=0;
- struct streaminfo_private *pstream_pr=(struct streaminfo_private *)pstream;
- struct streamindex *pindex;
-
- if(OP_STATE_CLOSE == pstream->opstate){/* ��ǰ���ѽ���, ���賬ʱҲû�������� */
- return -1;
- }
-
- if((0 == pstream_pr->timeout) /* 0Ϊ��δ���ù� */
- || (timeout >= pstream_pr->timeout)) /* �����õij�ʱֵ������ڵ��ڵ�ǰֵ */
- {
- /* TODO, �������ƶ���LRU��ʱ�����ʵ���λ��, ��head��ʼ��ʣ��ij�ʱʱ������
- NOTE:
- ��Ϊ����ȫ�ֶ�û������ʱ, ����Ե����������˳�ʱ, ��ô����Ӧ���ƶ���LRUĩβ,
- ����del_stream_by_time()����ֻ���ж���ĩβ�����Ƿ�ʱ, �����ж���������.
-
- ����: ������Ϊ�˱��������١�����ʱ�䳤����, ������������, ���ܳ�����������%p;
- */
- pindex = sapp_get_struct_header(pstream_pr, struct streamindex, stream);
-
- if(STREAM_TYPE_UDP==pstream->type)
- {
- if(timeout < udp_reset_time){ /* �����ȫ��Ĭ��ֵ��, �Ҳ��ܱ�֮ǰ���õ�С */
- return -1;
- }
- if(sapp_global_mthread[pstream->threadnum].udp_stream_special_timeout_num >= udpstate_num[UDP_TWO_STATE]/5){
- return -2;
- }
- if(pstream_pr->timeout == udp_reset_time){
- /* �������������ó�ʱʱ��, ��ǰ����һ�����ó�ʱʱ���¼���� */
- sapp_global_mthread[pstream->threadnum].udp_stream_special_timeout_num++;
- }
-#if 0
- lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->udpList[pstream->stream_state]);
- if(lru_list_root->head == lru_list_root->tail){ /* ��ǰ��ʣ���һ����, �����ƶ� */
- ret = 0;
- goto done;
- }
-
- /* before-insert : head->tmp_index->tail */
-
- assert(lru_list_root->head);
- assert(lru_list_root->tail);
- __do_streamleavlist(pindex, lru_list_root); /* ��ԭλ��ժ�� */
- assert(lru_list_root->head);
- assert(lru_list_root->tail);
- /*
- �����ǰ�����õ��³�ʱʱ��, ��LRUĩβ����ʣ��ʱ��(�ް�����ʱ��)����,
- ���仰˵, ��������̭������ʱ��Ҳ��LRUĩβ����Ҫ�ͺ�,
- ������Ҫ��tail(ʱ����µ�)�����ƶ� .
- */
- /* after-insert : head->pindex->tmp_index->tail, ����tmp_index֮ǰ */
-long udp_times = 0;
- tmp_index = lru_list_root->tail; /* �����timeoutֵ��, �Ӳ��ױ���̭��, ��tail�ڵ㿪ʼ�Ƚ� */
- while(tmp_index
- && (tmp_index->stream.timeout != 0) /* timeoutĬ��Ϊ0����, ��û�賬ʱʱ��, ����Ϊ������ */
- && (timeout < (g_CurrentTime - tmp_index->stream.stream_public.pudpdetail->lastmtime))){
-printf("##### serach udp timeout place, %ld.....\n", udp_times++);
- tmp_index = tmp_index->prev;
- }
-
- /* ������� */
- if(NULL == tmp_index){ /* һֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�head */
- lru_list_root->head->prev = pindex;
- pindex->next = lru_list_root->head;
- lru_list_root->head = pindex;
- pindex->prev = NULL;
- }else{
- if(tmp_index->prev){
- tmp_index->prev->next = pindex;
- }else{
- lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */
- }
- pindex->prev = tmp_index->prev;
- tmp_index->prev = pindex;
- pindex->next = tmp_index;
- }
- lru_list_root->cnt++; /* ��__do_streamleavlist()�м�1, �˴��ָ����� */
-#endif
- }
- else if (STREAM_TYPE_TCP==pstream->type)
- {
- if(timeout < link_default_nopkt_time){ /* �����ȫ��Ĭ��ֵ��, �Ҳ��ܱ�֮ǰ���õ�С */
- return -1;
- }
- if(sapp_global_mthread[pstream->threadnum].tcp_stream_special_timeout_num >= tcpstate_num[TCP_DATA_STATE]/10){
- return -2;
- }
- if(pstream_pr->timeout == link_default_nopkt_time){
- /* �������������ó�ʱʱ��, ��ǰ����һ�����ó�ʱʱ���¼���� */
- sapp_global_mthread[pstream->threadnum].tcp_stream_special_timeout_num++;
- }
-#if 0
- lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->tcpList[pstream->stream_state]);
- if(lru_list_root->head == lru_list_root->tail){ /* ��ǰ��ʣ���һ����, �����ƶ� */
- ret = 0;
- goto done;
- }
-
- assert(lru_list_root->head);
- assert(lru_list_root->tail);
- __do_streamleavlist(pindex, lru_list_root); /* ��ԭλ��ժ��, �˴�����ֱ��ʹ��streamleavlist, ���ò��ִ��� */
- assert(lru_list_root->head);
- assert(lru_list_root->tail);
- /*
- �����ǰ�����õ��³�ʱʱ��, ��LRUĩβ����ʣ��ʱ��(�ް�����ʱ��)����,
- ���仰˵, ��������̭������ʱ��Ҳ��LRUĩβ����Ҫ�ͺ�,
- ������Ҫ��tail(ʱ����µ�)�����ƶ� .
- */
-long tcp_times = 0;
- tmp_index = lru_list_root->tail;
- while(tmp_index
- && (tmp_index->stream.timeout != 0) /* timeoutĬ��Ϊ0����, ��û�賬ʱʱ��, ����Ϊ������ */
- && (timeout < (g_CurrentTime - tmp_index->stream.stream_public.ptcpdetail->lastmtime))){
-printf("##### serach tcp timeout place, %ld.....\n", tcp_times);
- tmp_index = tmp_index->prev;
- }
-
- /* ������� */
- if(NULL == tmp_index){ /* һֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�head */
- lru_list_root->head->prev = pindex;
- pindex->next = lru_list_root->head;
- lru_list_root->head = pindex;
- pindex->prev = NULL;
- }else{
- if(tmp_index->prev){
- tmp_index->prev->next = pindex;
- }else{
- lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */
- }
- pindex->prev = tmp_index->prev;
- tmp_index->prev = pindex;
- pindex->next = tmp_index;
- }
- lru_list_root->cnt++; /* ��__do_streamleavlist()�м�1, �˴��ָ����� */
-#endif
- }else{
- goto err; /* ��֧��TCP��UDP�� */
- }
-
- }
- else
- {
- goto err;
- }
-
- stream_timeout_shift_lrulist(pindex, timeout);
-
- pstream_pr->timeout=timeout;
- return ret;
-
-err:
- return -1;
-}
-#if 0
-void udp_set_stream_resettime(unsigned short timeout)
-{
- udp_reset_time=timeout;
-}
-#endif
-
-void udp_set_stream_timeout(unsigned short timeout)//2014-12-25 lijia add, ���ݾ�ƽ̨, xj_fd
-{
- udp_reset_time=(unsigned short)timeout;
-}
-
-void udp_set_stream_resettime(unsigned short timeout)
-{
- udp_reset_time=(unsigned short)timeout;
-}
-
-int udp_set_stream_num(int udp1,int udp2,int udp3)
-{
- udpstate_num[UDP_ONE_STATE]=udp1;
- udpstate_num[UDP_TWO_STATE]=udp2;
- udpstate_num[UDP_MORE_STATE]=udp3;
-
- return 0;
-}
-
-int init_stream_manage(int threadcount)
-{
- int i;
-
- wy_init_hash();
- for(i=0;i<MAX_TCP_STATE;i++)
- tcp_stream_table_size+=tcpstate_num[i];
- for(i=0;i<MAX_UDP_STATE;i++)
- udp_stream_table_size+=udpstate_num[i];
-
- G_MESA_GLOBAL_STREAM = (struct global_stream **)malloc(threadcount * sizeof (char *));
- memset (G_MESA_GLOBAL_STREAM, 0, threadcount * sizeof (char *));
- for(i=0;i<threadcount;i++)
- {
- G_MESA_GLOBAL_STREAM[i]=(struct global_stream *)malloc(sizeof(struct global_stream));
- init_stream_detail(G_MESA_GLOBAL_STREAM[i]);
- }
-
- //add 20140626 ��ʼ��procject�ӿ�
- project_requirement_global_init();
-
- return 0;
-}
-
-static struct streaminfo *del_stream(struct streamindex *pindex)
-{
- struct streaminfo_private *pstream_pr=&(pindex->stream);
- struct streaminfo *pstream=&(pstream_pr->stream_public);
-
- if(STREAM_TYPE_UDP==pstream->type)
- {
- udp_free_stream(pindex);
- }
- else if (STREAM_TYPE_TCP==pstream->type)
- {
- ((struct tcpdetail_private*)(pstream->pdetail))->link_state=STREAM_LINK_LRU_OUT;
- tcp_free_stream(pindex, NULL,NULL,NULL);
- }
-
- return NULL;
-}
-
-static inline void __do_streamleavlist(struct streamindex *pindex,struct stream_list *plist)
-{
- if(pindex->prev != NULL)
- {
- pindex->prev->next = pindex->next;
- }
- else
- {
- plist->head=pindex->next;
- if(plist->head!=NULL)
- plist->head->prev = NULL;
- }
- if(pindex->next != NULL)
- {
- pindex->next->prev = pindex->prev;
- }
- else
- {
- plist->tail = pindex->prev;
- if(plist->tail!=NULL)
- plist->tail->next = NULL;
- }
- pindex->next= NULL; /* NOTE: �˴�Ӧ�ÿ�, �������ܻ���addlist���� */
- pindex->prev = NULL; /* NOTE: �˴�Ӧ�ÿ�, �������ܻ���addlist���� */
- plist->cnt--;
-
- return;
-}
-
-void streamleavlist(struct streamindex *pindex,struct stream_list *plist)
-{
- struct streaminfo *pstream=(struct streaminfo *)(&(pindex->stream));
-
- __do_streamleavlist(pindex, plist);
-
- update_stream_status(pstream->threadnum, pstream->type, pstream->stream_state, -1);
-
-#if DEBUG
- packet_io_status_stream_update(pstream->threadnum, pstream->type, pstream->stream_state, -1);
-#endif
-}
-
-
-void streamaddlist(struct streamindex *pindex,struct stream_list *plist)
-{
- struct streamindex *tmpstream;
- struct streaminfo *pstream=(struct streaminfo *)(&(pindex->stream));
- if((plist->head != NULL) && (plist->cnt >= plist->max_cnt-1))
- {
- //ÿ����̭�ܴ�С��2%
- //istreamdelnum=plist->max_cnt/50+1;
- //adjust by lqy 20140810
- //istreamdelnum=plist->sift_out_cnt;
-
- /*
- 2017-06-07 lijia add,
- ���жϵ�ǰLRUĩβ�����Ƿ��˳�ʱʱ��, �����DZ���IM�Ȱ����١�ʱ�䳤����
- ����: ���ȫ��Ĭ����60��, ����Ϊ����̫��, ��û���ﳬʱʱ��, ��ô��?
-
- ʹ���������, if(pstream->timtout > defalut) && (g_current - lastmtime < pstream->timtout)
-
- */
- tmpstream = plist->head;
-
- while(tmpstream){
- if(STREAM_TYPE_TCP == tmpstream->stream.stream_public.type){
- /* ��ǰ�����г�ʱʱ���Ĭ��ȫ�ֵĴ�, �һ�û�г�ʱ, ������̭! */
- if((tmpstream->stream.timeout > link_default_nopkt_time)
- && (g_CurrentTime - tmpstream->stream.stream_public.ptcpdetail->lastmtime < tmpstream->stream.timeout)){
-/*
-printf("###### %s timeout is %d, remain %lld, can't be free!\n", printaddr(&tmpstream->stream.stream_public.addr, tmpstream->stream.stream_public.threadnum),
- tmpstream->stream.timeout,
- g_CurrentTime - tmpstream->stream.stream_public.ptcpdetail->lastmtime);
-*/
- tmpstream = tmpstream->next;
- }else{
- del_stream(tmpstream);
- break;
- }
- }else{
- if((tmpstream->stream.timeout > udp_reset_time)
- && (g_CurrentTime - tmpstream->stream.stream_public.pudpdetail->lastmtime < tmpstream->stream.timeout)){
- tmpstream = tmpstream->next;
- }else{
- del_stream(tmpstream);
- break;
- }
- }
- }
- }
- pindex->prev = plist->tail;
- pindex->next= NULL;
- if(plist->tail != NULL)
- {
- plist->tail->next = pindex;
- plist->tail = pindex;
- }
- else
- {
- plist->tail = pindex;
- plist->head = pindex;
- }
- plist->cnt++;
- //add by lqy 20120919 just for show
-
-#if 0
- if(pstream->type==STREAM_TYPE_TCP)
- {
- switch(pstream->stream_state)
- {
- case TCP_SYN_STATE:
- stream_info[pstream->threadnum].syn_cnt++;
- break;
- case TCP_DATA_STATE:
- stream_info[pstream->threadnum].ack_cnt++;
- break;
- case TCP_NOUSE_STATE:
- stream_info[pstream->threadnum].tcp_num++;
- break;
- }
-
- }
-#endif
- update_stream_status(pstream->threadnum, pstream->type, pstream->stream_state, 1);
-#if DEBUG
- packet_io_status_stream_update(pstream->threadnum, pstream->type, pstream->stream_state, 1);
-#endif
-}
-
-//struct streamindex *malloc_streamindex(int threadnum)
-struct streamindex *malloc_and_copy_streamindex(int threadnum, struct streamindex *pindex_stack)
-{
- struct streamindex *pindex=NULL;
- struct global_stream *g_stream=G_MESA_GLOBAL_STREAM[threadnum];
-
- pindex =g_stream->freeList.head;
-
- g_stream->freeList.index=pindex->stream.stream_public.stream_index;//add by lqy 20150113
- g_stream->freeList.head=pindex->next;
- g_stream->freeList.cnt--;
-
- memcpy(pindex,pindex_stack,sizeof(struct streamindex));
-
- pindex->stream.stream_public.stream_index=g_stream->freeList.index;
-
- pindex->stream.stream_killed_flag = 0;
- pindex->stream.hash_not_head_times = 0;
- pindex->stream.stream_carry_up_layer_tunnel_type = 0;
- pindex->stream.stream_low_layer_tunnel_type = 0;
-
- return pindex;
-
-}
-void free_streamindex(int threadnum,struct streamindex *pindex)
-{
- struct global_stream *g_stream=G_MESA_GLOBAL_STREAM[threadnum];
- //free(pindex);
- if(g_stream->freeList.tail==NULL)
- {
- assert(0);
- pindex->prev=NULL;
- pindex->next=NULL;
- g_stream->freeList.tail=pindex;
- g_stream->freeList.head=pindex;
- }
- else
- {
- g_stream->freeList.tail->next=pindex;
- pindex->prev=g_stream->freeList.tail;
- pindex->next=NULL;
- g_stream->freeList.tail=pindex;
-
- }
- g_stream->freeList.cnt++;
-
-#if SAPP_INSECTICIDE
- /* ��free���������ṹ��������, ��ֹuse-after-free�����, ע��ֻ�������ڲ���stream�ṹ, pindex�ṹ��ָ��, ��������Ķ� */
- /* 2018-09-20 lijia close! �˴��ر����stream, ��Ϊ�����ܶ�ط����м�����op_state��ֵ, ���ó�0xFE�ᵼ��Ī����������� */
- //memset(&pindex->stream, 0xFE, sizeof(struct streaminfo_private));
-#endif
-}
-
-//add by lqy 20141203
-//�������β�������һ�������Ƿ�ʱ�������ʱ����̭֮
-
-/* 2015-06-10 lijia add arg "current_drive_index", ��ʾ��ǰ���ṹָ��
- return value:
- 0: normal;
- 1: del self by time.
-
- modify by lqy 20150612
- ��ʱɾ��������ͷ����ʼ��һ��ɾ��
-*/
-int del_stream_by_time(struct stream_list *plist, const struct streamindex *current_drive_index)
-{
- struct streamindex *pindex;
- struct streaminfo_private *pstream_pr;
- struct streaminfo *pstream;
- long lastmtime;
-
- pindex=plist->head;
- pstream_pr=(&(pindex->stream));
-
- if(0 == pstream_pr->timeout){ /* δ������ʱ��̭ */
- return 0 ;
- }
-
- pstream=&pstream_pr->stream_public;
-
- if(STREAM_TYPE_UDP==pstream->type)
- {
- lastmtime=((struct udpdetail *)(pstream->pdetail))->lastmtime;
- }
- else if (STREAM_TYPE_TCP==pstream->type)
- {
- lastmtime=((struct tcpdetail *)(pstream->pdetail))->lastmtime;
- }
- else
- {
- sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: del_stream_by_time() error! stream->type is:%d, not TCP or UDP.\n", __FILE__, __LINE__, pstream->type);
- return -1;
- //assert(0);
- }
-
- //����趨�˳�ʱʱ�䣬�����Ѿ���ʱ
- if(unlikely(g_CurrentTime-lastmtime > pstream_pr->timeout))
- {
- if(pindex == current_drive_index){ /* ��ǰ�����ڵ�����ʱ, ˵��֮ǰ�ܳ�ʱ��û�а�����, ��ʱ����free, ���Ƿ���1, �ٵ���reset */
- if (STREAM_TYPE_TCP==pstream->type)
- {
- /* 2016-12-15 lijia add, ��ʱreset֮ǰ, ��Ҫ����link_state */
- ((struct tcpdetail_private *)(pstream->pdetail))->link_state=STREAM_LINK_TIMEOUT;
- }
- return 1;
- }
-#if 0 /* 2016-12-15 lijia close, ������ز�����tcp_free_stream()��, �˴����λ����ָ�����, �ڴ�й©, ��δ֪BUG */
- if(pindex->next!=NULL)
- {
- pindex->next->prev =NULL;
- plist->head= pindex->next;
- }
- else
- {
- plist->head=plist->tail=NULL;
- }
-#endif
- if (STREAM_TYPE_TCP==pstream->type)
- {
- ((struct tcpdetail_private *)(pstream->pdetail))->link_state=STREAM_LINK_TIMEOUT;
- tcp_free_stream(pindex, NULL,NULL,NULL);
- }
- else
- {
- udp_free_stream(pindex);
- }
- }
-
- return 0;
-}
-
-/*
- return value:
- 0: normal;
- 1: del self by timeout, need reset.
-*/
-int lrustream(struct streamindex *pindex)
-{
- struct stream_list *plist=NULL;
- struct streamindex * next,*prev;
- struct streaminfo_private *pstream_pr=&(pindex->stream);
- struct streaminfo *pstream = &pstream_pr->stream_public;
-
- if(STREAM_TYPE_UDP==pstream->type)
- {
- plist=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->udpList[pstream->stream_state]);
- if(1 == del_stream_by_time(plist, pindex)){
- return 1;
- }
- }
- else if (STREAM_TYPE_TCP==pstream->type)
- {
- plist=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->tcpList[pstream->stream_state]);
- /* 2015-06-10 lijia modify, UDPʹ��udp_reset_stream_bytime()��̭ */
- if(1 == del_stream_by_time(plist, pindex)){
- return 1;
- }
- }
-
-// if ( plist->cnt > STREAM_ONCE_DEL_NUM &&
- if(plist->tail && (plist->tail != pindex)) /* �����ǰ�ڵ㲻���������´�, LRU */
- {
- next = pindex->next;
- prev = pindex->prev;
- if(next) next->prev = prev;
- if(prev) prev->next = next;
- plist->tail->next = pindex ;
- pindex->prev = plist->tail;
- pindex->next = NULL;
- plist->tail= pindex;
-
- if(plist->head == pindex)
- {
- plist->head = next;
- plist->head->prev = NULL;
- }
- }
-
- return 0;
-}
-
-#if (0 == USE_LINUX_KERNEL_HASH_ALGO)
-static int mkstreamhash_single(struct streaminfo_private *pstream_pr, int maxsize)
-{
- char addr_hash_buf[MTU_MAX];
- char *hdata;
- struct streaminfo *this_stream = &pstream_pr->stream_public;
- struct layer_addr *saddr = &this_stream->addr;
-
- /* lijia comment, ����hashʱʹ��ͬһ�ַ���, ��"src>=dst" */
-
- /* ��Ԫ������, ����һ������, ��ʱֻ�ܿ�ip�ķ���, �˿���Ϣ��ͨ��ָ����ݻ�ȥ, �̲���ʹ�� */
- if(0 == pstream_pr->layer_dir){
- memcpy(addr_hash_buf, saddr->paddr, this_stream->addr.addrlen);
- reverse_addr((void *)addr_hash_buf, saddr->addrtype);
- hdata = (char *)&addr_hash_buf[0];
- }else{
- hdata = (char *)(saddr->paddr);
- }
-
- return mkaddrhash(hdata, this_stream->addr.addrlen) % maxsize;
-}
-
-
-int mkstreamhash_new(struct streaminfo_private *pstream_pr, int maxsize)
-{
- int hash = 0;
- struct streaminfo *this_stream = &pstream_pr->stream_public;
-
- switch(this_stream->addr.addrtype){
- case __ADDR_TYPE_IP_PAIR_V4:
- case __ADDR_TYPE_IP_PAIR_V6:
- case ADDR_TYPE_VLAN:
- case ADDR_TYPE_MAC:
- case ADDR_TYPE_PPPOE_SES:
- case ADDR_TYPE_L2TP:
- hash = mkstreamhash_single(pstream_pr, maxsize);
- break;
-
- case ADDR_TYPE_IPV4:
- case ADDR_TYPE_IPV6:
- case ADDR_TYPE_TCP:
- case ADDR_TYPE_UDP:
-#if 0
- /*
- TCP��UDPЭ�������, ���ֻ�ñ���Ķ˿ڼ���HASH, ��ͻ��̫��,
- ��Ҫ����fstream�е�IP��ַ, ʹ����Ԫ��һ�����.
- */
- hash = mkstreamhash_dual(this_stream, maxsize);
-#else
- hash = mkstreamhash_new((struct streaminfo_private *)this_stream->pfather, maxsize);
-#endif
- break;
- break;
-
- default:
- printf("Unknown addr type:%d!", this_stream->addr.addrtype);
- sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: mkstreamhash_new() Unknown addr type:%d.\n", __FILE__, __LINE__, this_stream->addr.addrtype);
- //assert(0);
- return -1;
- break;
- }
-
- return hash;
-}
-#endif
-
-static int cmpaddr_positive(void *addr_heap, void *addr_stack, UCHAR addrtype, UCHAR addrlen)
-{
- int ret;
-
- switch(addrtype){
- case ADDR_TYPE_IPV4:
- {
- /* 2015-07-14 lijia modify, ��Ҫ�Ƚ϶˿� */
- struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap;
- struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack;
-
- /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */
- ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->source;
- if(ret != 0){
- return ret;
- }
- ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->saddr;
- if(ret != 0){
- return ret;
- }
- ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->daddr;
- if(ret != 0){
- return ret;
- }
- ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->dest;
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- case ADDR_TYPE_IPV6:
- {
- struct stream_tuple4_v6 *ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap;
- struct stream_tuple4_v6 *ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack;
- /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */
- ret = (int)ip6_addr_heap->source - (int)ip6_addr_stack->source;
- if(ret != 0){
- return ret;
- }
- ret = memcmp(ip6_addr_heap->saddr, ip6_addr_stack->saddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- ret = memcmp(ip6_addr_heap->daddr, ip6_addr_stack->daddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- ret = (int)ip6_addr_heap->dest - (int)ip6_addr_stack->dest;
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V4:
- {
- struct stream_tuple4_v4 *pure_ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap;
- struct stream_tuple4_v4 *pure_ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack;
-
- ret = (int)pure_ip4_addr_heap->saddr - (int)pure_ip4_addr_stack->saddr;
- if(ret != 0){
- return ret;
- }
- ret = (int)pure_ip4_addr_heap->daddr - (int)pure_ip4_addr_stack->daddr;
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V6:
- {
- struct stream_tuple4_v6 *pure_ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap;
- struct stream_tuple4_v6 *pure_ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack;
-
- ret = memcmp(pure_ip6_addr_heap->saddr, pure_ip6_addr_stack->saddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- ret = memcmp(pure_ip6_addr_heap->daddr, pure_ip6_addr_stack->daddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- /* TODO: MPLS��ַֻ�Ƚ�label, TTL�Ȳ������ַHASH */
-
- default:
- /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת��, ֱ�ӱȽϼ��� */
- ret = memcmp(addr_heap, addr_stack, addrlen);
- }
-
- return ret;
-}
-static int cmpaddr_reverse(void *addr_heap, void *addr_stack, UCHAR addrtype, UCHAR addrlen)
-{
- int ret;
-
- switch(addrtype){
- case ADDR_TYPE_IPV4:
- {
- /* 2015-07-14 lijia modify, ��Ҫ�Ƚ϶˿� */
- struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap;
- struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack;
-
- /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */
- ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->dest;
- if(ret != 0){
- return ret;
- }
- ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->daddr;
- if(ret != 0){
- return ret;
- }
- ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->saddr;
- if(ret != 0){
- return ret;
- }
- ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->source;
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- case ADDR_TYPE_IPV6:
- {
- struct stream_tuple4_v6 *ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap;
- struct stream_tuple4_v6 *ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack;
- /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */
- ret = (int)ip6_addr_heap->source - (int)ip6_addr_stack->dest;
- if(ret != 0){
- return ret;
- }
- ret = memcmp(ip6_addr_heap->saddr, ip6_addr_stack->daddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- ret = memcmp(ip6_addr_heap->daddr, ip6_addr_stack->saddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- ret = (int)ip6_addr_heap->dest - (int)ip6_addr_stack->source;
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V4:
- {
- struct stream_tuple4_v4 *pure_ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap;
- struct stream_tuple4_v4 *pure_ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack;
-
- ret = (int)pure_ip4_addr_heap->saddr - (int)pure_ip4_addr_stack->daddr;
- if(ret != 0){
- return ret;
- }
- ret = (int)pure_ip4_addr_heap->daddr - (int)pure_ip4_addr_stack->saddr;
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V6:
- {
- struct stream_tuple4_v6 *pure_ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap;
- struct stream_tuple4_v6 *pure_ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack;
-
- ret = memcmp(pure_ip6_addr_heap->saddr, pure_ip6_addr_stack->daddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- ret = memcmp(pure_ip6_addr_heap->daddr, pure_ip6_addr_stack->saddr, IPV6_ADDR_LEN);
- if(ret != 0){
- return ret;
- }
- }
- break;
-
- default:
- /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת�� */
- ret = memcmp(addr_heap, addr_stack, addrlen);
- }
-
- return ret;
-}
-
-
-#if USE_RBTREE_INSTEAD_LIST
-/*
- pheap_stream_pr : already in stream htable.
- pstack_stream_pr: new element recv from network;
-*/
-static int cmpaddr_new(struct streaminfo_private *pheap_stream_pr,
- struct streaminfo_private *pstack_stream_pr, unsigned short hash_elem_count)
-{
- int ret;
- struct layer_addr *heap_addr, *stack_addr;
-
- heap_addr = &pheap_stream_pr->stream_public.addr;
- stack_addr = &pstack_stream_pr->stream_public.addr;
-
- if(pheap_stream_pr->stream_dir == pstack_stream_pr->layer_dir){
- struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)heap_addr->tuple4_v4;
- struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)stack_addr->tuple4_v4;
-
- /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */
- ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->source;
- if(ret != 0){
- goto done;
- }
- ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->saddr;
- if(ret != 0){
- goto done;
- }
- ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->daddr;
- if(ret != 0){
- goto done;
- }
- ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->dest;
- if(ret != 0){
- goto done;
- }
-
- ret = 0;
- }else{
- struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)heap_addr->tuple4_v4;
- struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)stack_addr->tuple4_v4;
-
- /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */
- ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->dest;
- if(ret != 0){
- goto done;
- }
- ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->daddr;
- if(ret != 0){
- goto done;
- }
- ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->saddr;
- if(ret != 0){
- goto done;
- }
- ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->source;
- if(ret != 0){
- goto done;
- }
-
- ret = 0;
- }
-
-done:
- if(0 == ret){
- //pst1->p_layer_header = pst2->p_layer_header;
- pheap_stream_pr->offset_to_raw_pkt_hdr = pstack_stream_pr->offset_to_raw_pkt_hdr;
- }
-
- return ret;
-}
-#else
-
-/*
- pheap_stream_pr : already in stream htable.
- pstack_stream_pr: new element recv from network;
-*/
-static int cmpaddr_new(struct streaminfo_private *pheap_stream_pr,
- struct streaminfo_private *pstack_stream_pr, unsigned short hash_elem_count)
-{
- int ret;
- struct layer_addr *heap_addr, *stack_addr;
-
- heap_addr = &pheap_stream_pr->stream_public.addr;
- stack_addr = &pstack_stream_pr->stream_public.addr;
-
- if(pheap_stream_pr->stream_dir == pstack_stream_pr->layer_dir){
- /* 2015-07-15 lijia modify,
- ��Ԫ��Ƚ�ʹ�����ʽ, ȡ����HASH��ͻ��,
- ��ͻ�ʸ�ʱʹ��cmpaddr_positive()���ܸ���,
- ����ʹ��memcmp()���ܺ�.
-
- Ŀǰƽ̨��HASH��ͻ��С��20%, ��ʱʹ��memcmp().
- */
- if(likely(hash_elem_count <= 2)){
- ret = memcmp(heap_addr->paddr, stack_addr->paddr, heap_addr->addrlen);
- }else{
- ret = cmpaddr_positive(heap_addr->paddr, stack_addr->paddr,
- heap_addr->addrtype, heap_addr->addrlen);
- }
- }else{
- /* 2015-03-04 lijia add, �����ַ�Ƚ�, ������COPY.*/
- ret = cmpaddr_reverse(heap_addr->paddr, stack_addr->paddr,
- heap_addr->addrtype, heap_addr->addrlen);
- }
-#if 0
-printf("lijia debug: cmpaddr, addr_len:%d:\n", pst1->addr.addrlen);
-int i;
-printf("heap addr \t :");
-for(i = 0; i < pst1->addr.addrlen; i++){
-printf("%02x,", ((UINT8 *)pst1->addr.paddr)[i]);
-}
-printf(" <-->\n");
-
-printf("new stack addr:\t");
- for(i = 0; i < pst2->addr.addrlen; i++){
- printf("%02x,", ((UINT8 *)ptmp)[i]);
- }
-printf("\n====================================================\n\n");
-#endif
-
- if(0 == ret){
- //pst1->p_layer_header = pst2->p_layer_header;
- pheap_stream_pr->offset_to_raw_pkt_hdr = pstack_stream_pr->offset_to_raw_pkt_hdr;
- }
-
- return ret;
-}
-
-#endif
-
-/*�Ƚ��������ṹ���С */
-//adjust 20140324 �Ƚ��������ṹ�Ƿ���ͬ������pfather �ڵ���ȫ��ͬ,
-//�ݹ�Ƚ��������ĸ��������
-
-/* lijia comment:
- pst1: in htable.
- pst2: new element;
-
- return value:
- 0 : found, success;
- 1 : pst1 > pst2;
- -1: pst1 < pst2
-*/
-int checkstreamorder(struct streaminfo_private *pheap_stream_pr,
- struct streaminfo_private *pstack_stream_pr, unsigned short hash_elem_count)
-{
- int ret;
- //int isreverse=0;
- struct streaminfo *pheap_stream;
- struct streaminfo *pstack_stream;
-
- if(pheap_stream_pr==NULL)
- {
- if(unlikely(pstack_stream_pr!=NULL)){
- return -1;
- }else{
- return 0;
- }
- }
- else
- {
- if(unlikely(pstack_stream_pr==NULL)){
- return 1;
- }
- }
-
- pheap_stream = &pheap_stream_pr->stream_public;
- pstack_stream = &pstack_stream_pr->stream_public;
-
- ret = (int)(pheap_stream->type - pstack_stream->type);
- if(ret != 0)
- {
- return ret;
- }
-
- ret = (int)(pheap_stream->addr.addrtype - pstack_stream->addr.addrtype);
- if(ret != 0)
- {
- return ret;
- }
-
- /* 2014-04-01 lijia add, ��������Ƚ�, ֱ������ */
- if((0 == pheap_stream_pr->addr_use_as_hash) && (0 == pstack_stream_pr->addr_use_as_hash)){
- pheap_stream_pr->offset_to_raw_pkt_hdr = pstack_stream_pr->offset_to_raw_pkt_hdr;
- return checkstreamorder((struct streaminfo_private *)(pheap_stream->pfather),
- (struct streaminfo_private *)(pstack_stream->pfather),
- hash_elem_count);
- }
-
- ret = cmpaddr_new(pheap_stream_pr, pstack_stream_pr, hash_elem_count);
- if(0 != ret){
- return ret;
- }else{
- /* 2015-03-11 lijia add, ��Ҫ����ǰ��IP��İ���Ϣ���ݵ�����Ϣ */
- pheap_stream_pr->stream_public.addr.pktipfragtype = pstack_stream_pr->stream_public.addr.pktipfragtype;
- }
-
- ret=checkstreamorder((struct streaminfo_private *)(pheap_stream->pfather),
- (struct streaminfo_private *)(pstack_stream->pfather),
- hash_elem_count);
- if(ret != 0)
- {
- return ret;
- }
-
- return ret;
-}
-
-#if 0
-static struct streamindex * findandsethashindex_20141217(struct streamindex *plinkhead,
- struct streamindex *pinsert, int hash_elem_count)
-{
- struct streamindex *phead=plinkhead;
- struct streamindex *res = NULL;
- struct streaminfo_private *pshead_pr=NULL;
- struct streaminfo_private *psinsert_pr=&(pinsert->stream);
- int result;
- unsigned long hash_list_num = 0;
- UCHAR threadnum = psinsert_pr->stream_public.threadnum;
-
- while(phead!=NULL)
- {
- pshead_pr=&(phead->stream);
-#if USE_LINUX_KERNEL_HASH_ALGO
- /* 2015-12-14 lijia add, ��ͨ���ڶ���HASHֵ�����ж��Ƿ����, ���HASH��ͬ, ��ַ�϶���ͬ */
- if(pshead_pr->hash_slave != psinsert_pr->hash_slave){
- phead=phead->phashnext;
- hash_list_num++;
- continue;
- }
-#endif
- result=checkstreamorder(pshead_pr,psinsert_pr, hash_elem_count);
- if(result==0){
- //�ҵ���ͬ�ڵ�
- res = phead;
- break;
- }
- //prev=phead;
-
- phead=phead->phashnext;
- hash_list_num++;
- }
-
- if(unlikely(hash_list_num > g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX])){
- g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX] = hash_list_num;
- }
-
-#if DEBUG
- packet_io_status_stream_hash_update(threadnum, hash_list_num+1);
-#endif
-
- return res;
-}
-#endif
-
-static struct streamindex * findandsethashindex_lru(struct streamindex *plinkhead,
- struct streamindex *pinsert, unsigned short hash_elem_count)
-{
- struct streamindex *phead=plinkhead;
- struct streamindex *res = NULL;
- struct streaminfo_private *pshead_pr=NULL;
- struct streaminfo_private *psinsert_pr=&(pinsert->stream);
- int result;
- unsigned long hash_list_num = 0;
- UCHAR threadnum = psinsert_pr->stream_public.threadnum;
-
- while(phead!=NULL)
- {
- pshead_pr=&(phead->stream);
-#if USE_LINUX_KERNEL_HASH_ALGO
- /* 2015-12-14 lijia add, ��ͨ���ڶ���HASHֵԤ���ж��Ƿ����, ���HASH��ͬ, ��ַ�϶���ͬ */
- if(pshead_pr->hash_slave != psinsert_pr->hash_slave){
- phead=phead->phashnext;
- hash_list_num++;
- continue;
- }
-#endif
- result=checkstreamorder(pshead_pr,psinsert_pr, hash_elem_count);
- if(result==0){
- res = phead;
- break;
- }
- //prev=phead;
-
- phead=phead->phashnext;
- hash_list_num++;
- }
-
- if(unlikely(hash_list_num > g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX])){
- g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX] = hash_list_num;
- }
-
-#if DEBUG
- packet_io_status_stream_hash_update(threadnum, hash_list_num+1);
-#endif
-
- return res;
-}
-
-#if 0
-/*�ж��������Ƿ�����Ӧ��������Ϣ
- ���û�У��Կ��ܵIJ���λ�ý���Ԥ��*/
-struct streamindex * findandsethashindex(struct streamindex *plinkhead,struct streamindex *pinsert, int hash_elem_count)
-{
- struct streamindex *phead=plinkhead;
- struct streamindex *prev=NULL;
- struct streaminfo_private *pshead=NULL;
- struct streaminfo_private *psinsert=&(pinsert->stream);
- int result;
- while(phead!=NULL)
- {
- pshead=&(phead->stream);
- result=checkstreamorder(pshead,psinsert,hash_elem_count);
- if(result>0)
- {
- //�ڸýڵ�ǰ����
- pinsert->phashprev=phead->phashprev;
- pinsert->phashnext=phead;
- return NULL;
- }
- else if(result==0)
- {
- //�ҵ���ͬ�ڵ�
- return phead;
- }
- prev=phead;
- phead=phead->phashnext;
- }
- if(phead==NULL)
- {
- //��ĩβ����
- if(prev!=NULL)
- {
- pinsert->phashprev=prev;
- pinsert->phashnext=NULL;
- }
- }
- return NULL;
-}
-#endif
-
-#if 0
-struct streamindex * findandsethashindex_binsearch(int elementcount,struct streamindex *plinkhead,
- struct streamindex *pinsert)
-{
- struct streamindex *ptag=plinkhead;
- struct streaminfo_private *pshead=NULL;
- struct streaminfo_private *psinsert=&(pinsert->stream);
- int result=0;
-
- int high=elementcount-1;
- int low=0;
- int mid=0,i=0,itag=0;
- //������
- if(ptag==NULL){
- return NULL;
- }
- //assert(plinkhead->hash_index==pinsert->hash_index);
- while(low<=high)
- {
- mid=low+(high-low)/2;
- if(mid>=itag)
- for(i=0;i<(mid-itag);i++) ptag=ptag->phashnext;
- else
- for(i=0;i<(itag-mid);i++) ptag=ptag->phashprev;
- itag=mid;
-
- //compare
- pshead=&(ptag->stream);
-#if 1 //2014-12-15 lqy modify
- result=checkstreamorder(pshead,psinsert,elementcount);
-#else
- result=checkstreamorder(psinsert, pshead);
-#endif
- if(result==0) //�ҵ���ͬ�ڵ�
- {
- return ptag;
- }
-
- if(result>0)
- high=mid-1;
- else
- low=mid+1;
- }
-
- if(low>high) //����ʧ��
- {
- if(result>0)
- {
- //�ڸýڵ�ǰ����
- pinsert->phashprev=ptag->phashprev;
- pinsert->phashnext=ptag;
- }
- else
- {
- //�ڸýڵ�����
- pinsert->phashprev=ptag;
- pinsert->phashnext=ptag->phashnext;
- }
- }
- /*
- if(pinsert->phashprev!=NULL)
- {
- assert(pinsert->phashprev->hash_index==pinsert->hash_index);
- }
- if(pinsert->phashnext!=NULL)
- {
- assert(pinsert->phashnext->hash_index==pinsert->hash_index);
- }
- */
- return NULL;
-}
-#endif
-
-#if USE_LINUX_KERNEL_HASH_ALGO
-extern int stream_make_hash(struct streaminfo_private *stream_pr, unsigned int maxsize);
-#endif
-
-#if USE_RBTREE_INSTEAD_LIST
-extern void rb_sapp_insert(struct streamindex *new_sapp_node, struct rb_root *root);
-extern struct streamindex *rb_sapp_search(struct streamindex *new_sapp_node, struct rb_root *rbroot, unsigned int hash_index);
-extern void rb_sapp_erase(struct streamindex *new_sapp_node, struct rb_root *root);
-
-
-struct streamindex *findstreamindex(struct streamindex *pindex, const raw_pkt_t *raw_pkt)
-{
- struct streamindex **phashstream=NULL;
- struct streamindex *a_index=NULL;
- struct streaminfo_private *pstream_pr = &(pindex->stream);
- struct streaminfo *ptmp=&pstream_pr->stream_public;
- struct streaminfo *a_stream=NULL;
- UCHAR threadnum = ptmp->threadnum;
- UCHAR stream_type = ptmp->type;
- unsigned int hash_max;
- unsigned int hash_index;
- unsigned short elemcount=0;
- struct global_stream *this_thread_stream = G_MESA_GLOBAL_STREAM[threadnum];
- struct rb_root *rb_root_table;
-
- if (STREAM_TYPE_TCP== stream_type)
- {
- rb_root_table = this_thread_stream->tcp_rb_root_table;
- hash_max=tcp_stream_table_size;
- }
- else if(STREAM_TYPE_UDP==stream_type)
- {
- rb_root_table = this_thread_stream->udp_rb_root_table;
- hash_max=udp_stream_table_size;
- }
-
-#if IOMODE_DPDK
- if(raw_pkt->hd_hash != 0){ /* 2016-05-05 lijia add, �ײ�Ӳ���ṩHASH */
- hash_index = raw_pkt->hd_hash % hash_max;;
- }
- else /* ��������HASHֵ */
-#endif
-
- {
-#if USE_LINUX_KERNEL_HASH_ALGO
- hash_index=stream_make_hash(&(pindex->stream), (unsigned int)hash_max);
-#else
- hash_index=mkstreamhash_new(&(pindex->stream), hash_max);
-#endif
- }
-
- if(NULL == rb_root_table[hash_index].rb_node){ /* 2015-12-14 lijia add, HASH����ǰSLOT��û����, ֱ�ӷ��� */
- ptmp->hash_index = hash_index; /* �������õ���HASHֵ */
- return NULL;
- }
-
- a_index = rb_sapp_search(pindex, &rb_root_table[hash_index], hash_index);
-
- if (likely(a_index!=NULL)){
- pstream_pr = &(a_index->stream);
- a_stream=&(pstream_pr->stream_public);
-#if 0 /* 2014-12-18 */
- if(a_stream->dirreverse==1)
-#else
- if(pstream_pr->stream_dir != 1)
-#endif
- {
- if(ptmp->curdir==DIR_C2S)
- a_stream->curdir=DIR_S2C;
- else
- a_stream->curdir=DIR_C2S;
- }
- else
- {
- a_stream->curdir=ptmp->curdir;
- }
- a_stream->routedir=ptmp->routedir;
-
- return a_index;
- }
- else
- {
- ptmp->hash_index=hash_index;
- }
-
- return NULL;
-}
-
-void hash_add_stream(struct streamindex *pindex)
-{
- struct streaminfo *ptmp = &pindex->stream.stream_public;
- UCHAR threadnum = ptmp->threadnum;
- struct rb_root *rb_root_table;
-
- if (STREAM_TYPE_TCP==ptmp->type)
- {
- rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->tcp_rb_root_table;
- g_SysInputInfo[threadnum][SYS_TCP_LINK_NEW]++;
- }
- else if(STREAM_TYPE_UDP==ptmp->type)
- {
- rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->udp_rb_root_table;
- g_SysInputInfo[threadnum][SYS_UDP_LINK_NEW]++;
- }
-
- rb_sapp_insert(pindex, &rb_root_table[ptmp->hash_index]);
-}
-
-void hash_del_stream(struct streamindex *pindex)
-{
- struct streaminfo *ptmp = &pindex->stream.stream_public;
- UCHAR threadnum = ptmp->threadnum;
- struct rb_root *rb_root_table;
-
- if (STREAM_TYPE_TCP==ptmp->type)
- {
- rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->tcp_rb_root_table;
- g_SysInputInfo[threadnum][SYS_TCP_LINK_DEL]++;
- }
- else if(STREAM_TYPE_UDP==ptmp->type)
- {
- rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->udp_rb_root_table;
- g_SysInputInfo[threadnum][SYS_UDP_LINK_DEL]++;
- }
-
- rb_sapp_erase(pindex, &rb_root_table[ptmp->hash_index]);
-}
-#else
-/*
-//�����������˵�����
-//1) �ж��Ƿ������
-//2) �������ķ�����Ϣ ,dir ,curdir ,routdir
-*/
-struct streamindex *findstreamindex(struct streamindex *pindex, const raw_pkt_t *raw_pkt)
-{
- struct streamindex **phashstream=NULL;
- struct streamindex *a_index=NULL;
- struct streaminfo_private *pstream_pr = &(pindex->stream);
- struct streaminfo *ptmp=&pstream_pr->stream_public;
- struct streaminfo *a_stream=NULL;
- UCHAR threadnum = ptmp->threadnum;
- UCHAR stream_type = ptmp->type;
- unsigned int hash_max;
- unsigned int hash_index;
- unsigned short elemcount=0;
- struct global_stream *this_thread_stream = G_MESA_GLOBAL_STREAM[threadnum];
-
- if (STREAM_TYPE_TCP== stream_type)
- {
- phashstream=this_thread_stream->tcp_stream_table;
- hash_max=tcp_stream_table_size;
- }
- else if(STREAM_TYPE_UDP==stream_type)
- {
- phashstream=this_thread_stream->udp_stream_table;
- hash_max=udp_stream_table_size;
- }else{
- phashstream = NULL;
- hash_max = 0;
- assert(0);
- }
-
-#if IOMODE_DPDK
- if(raw_pkt->hd_hash != 0){ /* 2016-05-05 lijia add, �ײ�Ӳ���ṩHASH */
- hash_index = raw_pkt->hd_hash % hash_max;
- }
-
- else /* ��������HASHֵ */
-#endif
-
- {
-#if USE_LINUX_KERNEL_HASH_ALGO
- hash_index=stream_make_hash(&(pindex->stream), (unsigned int)hash_max);
-#else
- hash_index=mkstreamhash_new(&(pindex->stream), hash_max);
-#endif
- }
-
-
- if(STREAM_TYPE_UDP==stream_type)
- {
- elemcount=this_thread_stream->udp_stream_talbe_hash_count[hash_index];
- }
- else if (STREAM_TYPE_TCP==stream_type)
- {
- elemcount=this_thread_stream->tcp_stream_talbe_hash_count[hash_index];
- }
-
- if(0 == elemcount){ /* 2015-12-14 lijia add, HASH����ǰSLOT��û����, ֱ�ӷ��� */
- ptmp->hash_index = hash_index; /* �������õ���HASHֵ */
- return NULL;
- }
-
- //ptmp->hash_index=hash_index;
-
-#if 0 /* TODO, ���ֲ��� */
- a_index= findandsethashindex(phashstream[hash_index],pindex);
-#elif 0
- a_index=findandsethashindex_binsearch(elemcount,phashstream[hash_index],pindex);
-#elif 0
- a_index= findandsethashindex_20141217(phashstream[hash_index],pindex, elemcount);
-#else
- a_index = findandsethashindex_lru(phashstream[hash_index],pindex, elemcount);
-#endif
-
- if (likely(a_index!=NULL)){
-/*
- 2015-12-14 lijia add
- NOTE:
- �����ǰ������N�ζ�����HASH_SLOT�ĵ�һλ, ���ƶ�����һλ, �ƶ����������,
- �´β��ҿ�������,
- ͬʱ��ֹ����ͬһ��SLOT��������Ƶ���а��������, ָ��Ƶ�������ƶ�, ������������.
-
- Ŀ�����ĸ��������ݰ���, ˭�;����ڵ�һλ.
-*/
-if((a_index != phashstream[hash_index]) && (a_index->stream.hash_not_head_times++ > 5)){
- if(a_index->phashnext){
- a_index->phashnext->phashprev = a_index->phashprev;
- }
- if(a_index->phashprev){
- a_index->phashprev->phashnext = a_index->phashnext;
- }
-
- phashstream[hash_index]->phashprev=a_index;
- a_index->phashnext=phashstream[hash_index];
- phashstream[hash_index] = a_index;
- a_index->phashprev = NULL;
- a_index->stream.hash_not_head_times = 0;
-}
-
- pstream_pr = &(a_index->stream);
- a_stream=&(pstream_pr->stream_public);
-#if 0 /* 2014-12-18 */
- if(a_stream->dirreverse==1)
-#else
- if(pstream_pr->stream_dir != 1)
-#endif
- {
- if(ptmp->curdir==DIR_C2S)
- a_stream->curdir=DIR_S2C;
- else
- a_stream->curdir=DIR_C2S;
- }
- else
- {
- a_stream->curdir=ptmp->curdir;
- }
- a_stream->routedir=ptmp->routedir;
-
- return a_index;
- }
- else
- {
- ptmp->hash_index=hash_index;
- }
-
- return NULL;
-}
-
-
-void hash_add_stream(struct streamindex *pindex)
-{
- struct streamindex **phashstream;
- struct streaminfo_private *pstream_pr=&(pindex->stream);
- struct streaminfo *ptmp = &pstream_pr->stream_public;
- unsigned short *pelementcount;
- UCHAR threadnum = ptmp->threadnum;
- unsigned int hash_index = ptmp->hash_index;
-
- if (STREAM_TYPE_TCP==ptmp->type)
- {
- phashstream=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_table;
- pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_talbe_hash_count;
- g_SysInputInfo[threadnum][SYS_TCP_LINK_NEW]++;
- }
- else if(STREAM_TYPE_UDP==ptmp->type)
- {
- phashstream=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_table;
- pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_talbe_hash_count;
- g_SysInputInfo[threadnum][SYS_UDP_LINK_NEW]++;
- } else{
- phashstream = NULL;
- pelementcount = 0;
- assert(0);
- }
-
-
-#if 1 /* ����HASH-Slot��һ��λ�� */
- if(phashstream[hash_index]!=NULL)
- phashstream[hash_index]->phashprev=pindex;
- pindex->phashnext=phashstream[hash_index];
- phashstream[hash_index] = pindex;
-#else
- //adjust by lqy 20120803 �ڲ��ҹ������Ѿ�ȷ���˲���λ��
- if(pindex->phashprev==NULL)
- {
- phashstream[pindex->stream.stream_public.hash_index] = pindex;
- }
- else
- {
- pindex->phashprev->phashnext=pindex;
- }
- if(pindex->phashnext!=NULL)
- pindex->phashnext->phashprev=pindex;
-#endif
- pelementcount[hash_index]++;
-
-
-#if 0
-static int listnum = 0;
-if(pelementcount[pindex->hash_index] >= listnum){
- //printf("%s, ", printaddr(&pindex->stream.addr, pindex->stream.threadnum));
- printf("########### index:%d, list-num:%d\n", pindex->hash_index, pelementcount[pindex->hash_index]);
- listnum++;
-}
-#endif
- /*
-
- ptmindex=phashstream[pindex->hash_index] ;
- for(i=0;i<pelementcount[pindex->hash_index];i++)
- {
- assert(ptmindex!=NULL);
- if(ptmindex->hash_index !=pindex->hash_index)
- assert(0);
- ptmindex=ptmindex->phashnext;
- }
- */
-}
-
-
-void hash_del_stream(struct streamindex *pindex)
-{
- struct streamindex **phashstream;
- struct streaminfo_private *pstream_pr=&(pindex->stream);
- struct streaminfo *ptmp = &pstream_pr->stream_public;
- unsigned short *pelementcount;
- UCHAR threadnum = ptmp->threadnum;
-
- if(STREAM_TYPE_UDP==ptmp->type)
- {
- phashstream=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_table;
- pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_talbe_hash_count;
- g_SysInputInfo[threadnum][SYS_UDP_LINK_DEL]++;
- }
- else if (STREAM_TYPE_TCP==ptmp->type)
- {
- phashstream=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_table;
- pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_talbe_hash_count;
- g_SysInputInfo[threadnum][SYS_TCP_LINK_DEL]++;
- }else{
- phashstream = NULL;
- pelementcount = 0;
- assert(0);
- }
-
- if(pindex->phashprev!=NULL)
- {
- pindex->phashprev->phashnext=pindex->phashnext;
- }
- else
- {
- phashstream[ptmp->hash_index] = pindex->phashnext;
- }
- if(pindex->phashnext!=NULL)
- {
- pindex->phashnext->phashprev=pindex->phashprev;
- }
-
- pindex->phashnext=pindex->phashprev=NULL;
-
- pelementcount[ptmp->hash_index]--;
- /*
- //for test;
- ptmindex=phashstream[pindex->hash_index] ;
- for(i=0;i<pelementcount[pindex->hash_index];i++)
- {
- assert(ptmindex!=NULL);
- if(ptmindex->hash_index !=pindex->hash_index)
- assert(0);
- ptmindex=ptmindex->phashnext;
- }
- */
-
- /* 2014-10-15 lijia modify, �˴�free��, �����ȡ������ַ, �ƶ���tcp_free_stream()������� */
- //free_heap_stream_info(pindex->stream.pfather);
-}
-
-#endif
-
-void freeall_stream(int threadcount)
-{
- struct global_stream *g_stream;
- struct stream_list *plist;
- struct streamindex *pindex;
- int i,j;
- for( i = 0; i < threadcount; i++)
- {
- g_stream=G_MESA_GLOBAL_STREAM[i];
- for(j=0;j<MAX_TCP_STATE;j++)
- {
- plist=&(g_stream->tcpList[j]);
- while(plist->cnt>0)
- {
- pindex = plist->head;
- del_stream(pindex);
- }
- }
- for(j=0;j<MAX_UDP_STATE;j++)
- {
- plist=&(g_stream->udpList[j]);
- while(plist->cnt>0)
- {
- pindex = plist->head;
- del_stream(pindex);
- }
- }
-
- }
-
- sleep(1);
- printf("All stream has been freed!\n");
-//_exit(0);
-}
-
-
-
-int show_stream_state(int threadcount)
-{
- int i=0;
- int j=0;
-
- printf("\n");
- printf("\n\n-------------------------------tcp stream state---------------------------------------");
- for(i=0;i<MAX_TCP_STATE;i++)
- {
- switch(i)
- {
- case TCP_SYN_STATE:
- printf("\ntcp_syn:\t");
- break;
- case TCP_DATA_STATE:
- printf("\ntcp_data:\t");
- break;
- case TCP_NOUSE_STATE:
- printf("\ntcp_nouse:\t");
- break;
- default:
- break;
- }
-
- for(j=0;j<threadcount;j++)
- {
- printf("%d\t",G_MESA_GLOBAL_STREAM[j]->tcpList[i].cnt);
- }
-
- }
-
- printf("\n\n-------------------------------udp stream state---------------------------------------");
- for(i=0;i<MAX_UDP_STATE;i++)
- {
- switch(i)
- {
- case UDP_ONE_STATE:
- printf("\nudp one state:\t");
- break;
- case UDP_TWO_STATE:
- printf("\nudp two state:\t");
- break;
- case UDP_MORE_STATE:
- printf("\nudp more state:\t");
- break;
- default:
- break;
- }
-
- for(j=0;j<threadcount;j++)
- {
- printf("%d\t",G_MESA_GLOBAL_STREAM[j]->udpList[i].cnt);
- }
- }
-
- printf("\n");
-
- return 0;
-
-}
-
-#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x))
-
-
-
-int addr_dir_relation(void *addr, int addrtype)
-{
- int relation = 0;
- switch(addrtype){
- case __ADDR_TYPE_IP_PAIR_V4:
- {
- struct layer_addr_ipv4 *ip4_addr = (struct layer_addr_ipv4 *)addr;
- relation = memcmp(&ip4_addr->saddr, &ip4_addr->daddr, sizeof(int));
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V6:
- {
- struct layer_addr_ipv6 *ip6_addr = (struct layer_addr_ipv6 *)addr;
- relation = memcmp(ip6_addr->saddr, ip6_addr->daddr, 16);
- }
- break;
-
- case ADDR_TYPE_TCP:
- case ADDR_TYPE_UDP:
- {
- struct layer_addr_tcp *tcp_addr = (struct layer_addr_tcp *)addr;
- relation = memcmp(&tcp_addr->source, &tcp_addr->dest, sizeof(short));
- }
- break;
-
- default:
- /* ������ַ��������������, Ĭ��Ϊ0 */
- relation = 0;
- break;
- }
-
- return relation;
-}
-
-/* ��addr��ַ���� */
-void reverse_addr(void *addr, int addrtype)
-{
- UINT16 tmp_ushort;
- UINT32 tmp_uint;
-
- switch(addrtype){
- case ADDR_TYPE_IPV4:
- {
- struct layer_addr_ipv4 *ip4_addr = (struct layer_addr_ipv4 *)addr;
- tmp_uint = ip4_addr->saddr;
- ip4_addr->saddr = ip4_addr->daddr;
- ip4_addr->daddr = tmp_uint;
- tmp_ushort = ip4_addr->source;
- ip4_addr->source = ip4_addr->dest;
- ip4_addr->dest = tmp_ushort;
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V4:
- {
- struct layer_addr_ipv4 *ip4_pure_addr = (struct layer_addr_ipv4 *)addr;
- tmp_uint = ip4_pure_addr->saddr;
- ip4_pure_addr->saddr = ip4_pure_addr->daddr;
- ip4_pure_addr->daddr = tmp_uint;
- }
- break;
-
- case ADDR_TYPE_IPV6:
- {
- struct layer_addr_ipv6 *ip6_addr = (struct layer_addr_ipv6 *)addr;
- UCHAR tmp[16];
- memcpy(tmp, ip6_addr->saddr, IPV6_ADDR_LEN);
- memcpy(ip6_addr->saddr, ip6_addr->daddr, IPV6_ADDR_LEN);
- memcpy(ip6_addr->daddr, tmp, IPV6_ADDR_LEN);
- tmp_ushort = ip6_addr->source;
- ip6_addr->source = ip6_addr->dest;
- ip6_addr->dest = tmp_ushort;
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V6:
- {
- struct layer_addr_ipv6 *ip6_pure_addr = (struct layer_addr_ipv6 *)addr;
- UCHAR tmp[16];
- memcpy(tmp, ip6_pure_addr->saddr, IPV6_ADDR_LEN);
- memcpy(ip6_pure_addr->saddr, ip6_pure_addr->daddr, IPV6_ADDR_LEN);
- memcpy(ip6_pure_addr->daddr, tmp, IPV6_ADDR_LEN);
- }
- break;
-
- case ADDR_TYPE_TCP:
- case ADDR_TYPE_UDP:
- {
- struct layer_addr_tcp *tcp_addr = (struct layer_addr_tcp *)addr;
- tmp_ushort = tcp_addr->source;
- tcp_addr->source = tcp_addr->dest;
- tcp_addr->dest = tmp_ushort;
- }
- break;
-
- /* TODO:PPTP-addr */
-
- default:
- /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת�� */
- return;
- }
-
- return;
-}
-
-/* �����ַcopy */
-void addr_reverse_memcpy(void *daddr, const void *saddr, int addrtype, int addrlen)
-{
- switch(addrtype){
- case ADDR_TYPE_IPV4:
- {
- const struct layer_addr_ipv4 *ip4_saddr = (const struct layer_addr_ipv4 *)saddr;
- struct layer_addr_ipv4 *ip4_daddr = (struct layer_addr_ipv4 *)daddr;
- ip4_daddr->saddr = ip4_saddr->daddr;
- ip4_daddr->daddr = ip4_saddr->saddr;
- ip4_daddr->source = ip4_saddr->dest;
- ip4_daddr->dest = ip4_saddr->source;
- }
- break;
-
- case ADDR_TYPE_IPV6:
- {
- const struct layer_addr_ipv6 *ip6_saddr = (const struct layer_addr_ipv6 *)saddr;
- struct layer_addr_ipv6 *ip6_daddr = (struct layer_addr_ipv6 *)daddr;
- memcpy(ip6_daddr->saddr, ip6_saddr->daddr, IPV6_ADDR_LEN);
- memcpy(ip6_daddr->daddr, ip6_saddr->saddr, IPV6_ADDR_LEN);
- ip6_daddr->source = ip6_saddr->dest;
- ip6_daddr->dest = ip6_saddr->source;
- }
- break;
-
- case ADDR_TYPE_MAC: /* 2015-06-15 lijia add, ľ������豸���� */
- {
- const struct layer_addr_mac *mac_saddr = (const struct layer_addr_mac *)saddr;
- struct layer_addr_mac *mac_daddr = (struct layer_addr_mac *)daddr;
- memcpy(mac_daddr->src_mac, mac_saddr->dst_mac, MAC_ADDR_LEN);
- memcpy(mac_daddr->dst_mac, mac_saddr->src_mac, MAC_ADDR_LEN);
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V4: /* ��IPv4��, ͨ���ǻ�������ģʽ */
- {
- const struct layer_addr_ipv4 *ip4_saddr = (const struct layer_addr_ipv4 *)saddr;
- struct layer_addr_ipv4 *ip4_daddr = (struct layer_addr_ipv4 *)daddr;
- ip4_daddr->saddr = ip4_saddr->daddr;
- ip4_daddr->daddr = ip4_saddr->saddr; /* ֻ��IP��ַ */
- /* Note: ��IP��Ķ˿�ֵһ��Ҫ��Ϊ0, �����ʹ��δ��ʼ�����ڴ�, �ں�������ԭ��cmpaddr()������, ���ܻᵼ�����Ƚϵ�ַ��һ�����Ҳ��� */
- ip4_daddr->source = 0;
- ip4_daddr->dest = 0;
- }
- break;
-
- case __ADDR_TYPE_IP_PAIR_V6: /* ��IPv6��, ͨ���ǻ�������ģʽ */
- {
- const struct layer_addr_ipv6 *ip6_saddr = (const struct layer_addr_ipv6 *)saddr;
- struct layer_addr_ipv6 *ip6_daddr = (struct layer_addr_ipv6 *)daddr;
- memcpy(ip6_daddr->saddr, ip6_saddr->daddr, IPV6_ADDR_LEN);
- memcpy(ip6_daddr->daddr, ip6_saddr->saddr, IPV6_ADDR_LEN);
- /* Note: ��IP��Ķ˿�ֵһ��Ҫ��Ϊ0, �����ʹ��δ��ʼ�����ڴ�, �ں�������ԭ��cmpaddr()������, ���ܻᵼ�����Ƚϵ�ַ��һ�����Ҳ��� */
- ip6_daddr->source = 0;
- ip6_daddr->dest = 0;
- }
- break;
-
- /* TODO:PPTP-addr */
-
- default:
- /* ������ַ����, ��VLAN, L2TP��, ����������,ֱ��copy */
- memcpy(daddr, saddr, addrlen);
- return;
- }
-
- return;
-}
-
-/*
- ��ΪԤ�ȷ�����һЩstreamindex�ṹ, ÿ�δ�����ʱ, ��Ԥ�ȷ�����ڴ��л�ȡ,
- ��malloc_and_copy_streamindex()����, ֻcopy streamindex�ṹ, û��addr.paddr,
- ��Ҫ����ַ��ϢҲcopy������.
-*/
-int copy_ipport_union_addr(struct streaminfo *pstream_heap, struct streaminfo *pstream_stack, int reverse)
-{
- pstream_heap->addr.paddr = dictator_malloc(pstream_stack->threadnum, pstream_stack->addr.addrlen);
- if(0 == reverse){
- memcpy(pstream_heap->addr.paddr, pstream_stack->addr.paddr, pstream_stack->addr.addrlen);
- }else{
- addr_reverse_memcpy(pstream_heap->addr.paddr, pstream_stack->addr.paddr, pstream_stack->addr.addrtype, pstream_stack->addr.addrlen);
- }
-
- return 0;
-}
-
-#if 0
-void reverse_addr_new(void *addr, int addrtype)
-{
- UINT16 tmp_ushort;
- UINT32 tmp_uint;
-
- switch(addrtype){
- case ADDR_TYPE_IPV4:
- //case __ADDR_TYPE_IP_PAIR_V4:
- {
- struct layer_addr_ipv4 *ip4_addr = (struct layer_addr_ipv4 *)addr;
- tmp_uint = ip4_addr->saddr;
- ip4_addr->saddr = ip4_addr->daddr;
- ip4_addr->daddr = tmp_uint;
- tmp_ushort = ip4_addr->source;
- ip4_addr->source = ip4_addr->dest;
- ip4_addr->dest = tmp_ushort;
- }
- break;
-
- case ADDR_TYPE_IPV6:
- //case __ADDR_TYPE_IP_PAIR_V6:
- {
- struct layer_addr_ipv6 *ip6_addr = (struct layer_addr_ipv6 *)addr;
- UCHAR tmp[16];
- memcpy(tmp, ip6_addr->saddr, IPV6_ADDR_LEN);
- memcpy(ip6_addr->saddr, ip6_addr->daddr, IPV6_ADDR_LEN);
- memcpy(ip6_addr->daddr, tmp, IPV6_ADDR_LEN);
- tmp_ushort = ip6_addr->source;
- ip6_addr->source = ip6_addr->dest;
- ip6_addr->dest = tmp_ushort;
- }
- break;
-
- case ADDR_TYPE_TCP:
- case ADDR_TYPE_UDP:
- {
- struct layer_addr_tcp *tcp_addr = (struct layer_addr_tcp *)addr;
- tmp_ushort = tcp_addr->source;
- tcp_addr->source = tcp_addr->dest;
- tcp_addr->dest = tmp_ushort;
- }
- break;
-
- case ADDR_TYPE_MAC: /* 2015-06-15 lijia add, ľ������豸���� */
- {
- struct layer_addr_mac *mac_addr = (struct layer_addr_mac *)addr;
- UCHAR tmp_mac[MAC_ADDR_LEN];
- memcpy(tmp_mac, mac_addr->src_mac, MAC_ADDR_LEN);
- memcpy(mac_addr->src_mac, mac_addr->dst_mac, MAC_ADDR_LEN);
- memcpy(mac_addr->dst_mac, tmp_mac, MAC_ADDR_LEN);
- }
- break;
-
- default:
- /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת�� */
- return;
- }
-
- return;
-}
-#endif
-
-void free_thread_stream(int thread_seq)
-{
- struct global_stream *g_stream;
- struct stream_list *plist;
- struct streamindex *pindex;
- int j;
-
- g_stream=G_MESA_GLOBAL_STREAM[thread_seq];
- for(j=0;j<MAX_TCP_STATE;j++)
- {
- plist=&(g_stream->tcpList[j]);
- while(plist->cnt>0)
- {
- pindex = plist->head;
- del_stream(pindex);
- }
- }
- for(j=0;j<MAX_UDP_STATE;j++)
- {
- plist=&(g_stream->udpList[j]);
- while(plist->cnt>0)
- {
- pindex = plist->head;
- del_stream(pindex);
- }
- }
-
-#if HASH_COLLIDE_CALC
- printf("================ hash-collide-statistics =================\n");
-
- for(j = 0; j < HASH_COLLIDE_LIST_NUM_MAX; j++){
- printf("hash-collide-list-num:%d, \t times:%d\n", j, __hash_colliede_num[j]);
- }
- printf("================ hash-collide-statistics =================\n");
-#endif
-
-}
-
-/* 2014-04-08 LiJia add */
-void free_heap_stream_info(struct streaminfo *heap_stream, int layer)
-{
- struct streaminfo *pfather;
- struct layer_addr *addr;
- unsigned char threadnum;
-
- if(NULL == heap_stream){
- return;
- }
- threadnum = heap_stream->threadnum;
-
- pfather = heap_stream->pfather;
- addr = &heap_stream->addr;
-
- /* Ϊ�˽�ʡ�ռ�, ��ַ����ADDR_TYPE_IPV4/6ʵ�ʴ洢����һ���IP��, ����free */
- if(addr->paddr){
- dictator_free(threadnum, addr->paddr);
- addr->paddr = NULL;
- }
-
- if(layer != 0){
- /* ���ṹ�����ڲ㼴��0��, ʵ����ͨ��malloc_and_copy_streamindex()��ȡ��Ԥ�ȷ�����ڴ�,
- ����free,
- pfatherָ���streaminfo�ṹ����ͨ��copy_stream_info_to_heap()��̬malloc��.
- */
-#if SAPP_INSECTICIDE
- /* 2018-09-20 lijia close! �˴��ر����stream, ��Ϊ�����ܶ�ط����м�����op_state��ֵ, ���ó�0xFE�ᵼ��Ī����������� */
- //memset(heap_stream, 0xFE, sizeof(struct streaminfo));
-#endif
- dictator_free(threadnum, heap_stream);
- }
- return free_heap_stream_info(pfather, layer+1);
-}
-
-/* 2014-12-03 lijia add for sysinfo.log
- args:
- action: 1:add; -1:del;
-*/
-static inline void update_stream_status(UCHAR threadnum, UCHAR stream_type, UCHAR stream_state, int action)
-{
- if(STREAM_TYPE_TCP == stream_type){
- switch(stream_state)
- {
- case TCP_DATA_STATE:
- g_SysInputInfo[threadnum][SYS_TCP_LINK_DATA] += action;
- break;
- case TCP_NOUSE_STATE:
- g_SysInputInfo[threadnum][SYS_TCP_LINK_NOUSE] += action;
- break;
- case TCP_SYN_STATE:
- g_SysInputInfo[threadnum][SYS_TCP_LINK_SYN] += action;
- break;
- }
- }else if(STREAM_TYPE_UDP == stream_type){
- switch(stream_state)
- {
- case UDP_TWO_STATE:
- g_SysInputInfo[threadnum][SYS_UDP_LINK_TWO] += action;
- break;
- case UDP_MORE_STATE:
- g_SysInputInfo[threadnum][SYS_UDP_LINK_MORE] += action;
- break;
- case UDP_ONE_STATE:
- g_SysInputInfo[threadnum][SYS_UDP_LINK_ONE] += action;
- break;
- }
- }
-}
-
-
-/*
- TODO:
- ���ں�tilera�ܹ��µ��ڴ�����Ϊlocal��sharedģʽ,
- ԭ�������߳̽�����ȫ���ڴ�һ�����������ģʽ������,
- ��Ϊÿ�������߳��Լ���ʼ��.
-*/
-int thread_self_init(int thread_seq)
-{
- return 0;
-}
-
-
-
-#if DEBUG
-#include "iknow.h"
-typedef struct{
- packet_io_status_base_t io_base_status;
- unsigned long long total_search_times; /* ��HASH������, �������˶��ٴ�findstreamindex() */
- unsigned long long total_hash_list_num; /* ��HASH��ͻ��������, (total_hash_list_num/total_search_times)��hashƽ�����ҳ��� */
- unsigned long long max_hash_list_num; /* ����ͻ������ */
-}packet_io_status_stream_hash_t;
-
-extern packet_io_status_t *g_packet_io_status_pool[MAX_THREAD_NUM];
-
-static packet_io_status_stream_hash_t g_packet_io_status_stream_hash;
-
-void *packet_io_status_stream_hash_mem_init(void)
-{
- void *stream_hash_mem;
-
- stream_hash_mem = malloc(sizeof(packet_io_status_stream_hash_t));
- memset(stream_hash_mem, 0, sizeof(packet_io_status_stream_hash_t));
-
- return stream_hash_mem;
-}
-
-
-void packet_io_status_stream_hash_time_cb(void)
-{
- int i;
- unsigned long long tmp_max_hlist_num = 0;
- packet_io_status_stream_hash_t *psh;
-
- g_packet_io_status_stream_hash.total_search_times = 0;
- g_packet_io_status_stream_hash.total_hash_list_num = 0;
- for(i = 0; i < g_packet_io_thread_num; i++){
- psh = (packet_io_status_stream_hash_t *)(g_packet_io_status_pool[i]->packet_io_status[IO_STATUS_STREAM_HASH]);
- g_packet_io_status_stream_hash.total_search_times += psh->total_search_times;
- g_packet_io_status_stream_hash.total_hash_list_num += psh->total_hash_list_num;
- if(psh->max_hash_list_num > tmp_max_hlist_num){
- tmp_max_hlist_num = psh->max_hash_list_num;
- }
- }
- g_packet_io_status_stream_hash.max_hash_list_num = tmp_max_hlist_num;
-}
-
-static long ifuncb_show_stream_hash_summary(iknow_handle ihandle, iknow_conn iconn, const void *cb_fun_arg, const char *cmd_args)
-{
-
- iprintf(ihandle, iconn, "stream hash summary:\ntotal-hash-search-num:%llu\ntotal-hash-list-num:%llu\nmax-hash-list-num:%llu\navg-hlist-num:%.2f",
- g_packet_io_status_stream_hash.total_search_times,
- g_packet_io_status_stream_hash.total_hash_list_num,
- g_packet_io_status_stream_hash.max_hash_list_num,
- (float)g_packet_io_status_stream_hash.total_hash_list_num/(float)g_packet_io_status_stream_hash.total_search_times);
-
- return 0;
-}
-
-static long ifuncb_show_stream_hash_detail(iknow_handle ihandle, iknow_conn iconn, const void *cb_fun_arg, const char *cmd_args)
-{
- int i;
- packet_io_status_stream_hash_t *psh;
-
- iprintf(ihandle, iconn, "stream hash detail:\n%10s %15s %13s %13s %13s\n", "thread-seq", "hash-search-num", "hash-list-num", "max-hlist-num", "avg-hlist-num");
- for(i = 0; i < g_packet_io_thread_num; i++){
- psh = (packet_io_status_stream_hash_t *)(g_packet_io_status_pool[i]->packet_io_status[IO_STATUS_STREAM_HASH]);
- iprintf(ihandle, iconn, "%10d %15llu %13llu %13llu %13.2f\n",
- i, psh->total_search_times, psh->total_hash_list_num, psh->max_hash_list_num,
- (float)psh->total_hash_list_num/(float)psh->total_search_times);
- }
-
-
- return 0;
-}
-
-void iknow_register_stream_hash_cb(void *ihandle)
-{
- int ret;
-
- ret = iknow_register_cmd(ihandle, "show stream summary", "display stream hash summary", ifuncb_show_stream_hash_summary, NULL, 0, NULL);
- assert(0 == ret);
- ret = iknow_register_cmd(ihandle, "show stream detail", "display stream hash detail", ifuncb_show_stream_hash_detail, NULL, 0, NULL);
- assert(0 == ret);
-}
-
-void packet_io_status_stream_hash_update(int thread_seq, unsigned long hash_list_num)
-{
- packet_io_status_stream_hash_t *psh;
-
- psh = (packet_io_status_stream_hash_t *)g_packet_io_status_pool[thread_seq]->packet_io_status[IO_STATUS_STREAM_HASH];
-
- psh->total_search_times++;
- psh->total_hash_list_num += hash_list_num;
-
- if(unlikely(hash_list_num > psh->max_hash_list_num)){
- psh->max_hash_list_num = hash_list_num;
- }
-}
-
-#endif
-
-
-
-int get_stream_carry_tunnel_type(const struct streaminfo *this_stream,
- const struct streaminfo *upper_stream, unsigned short *tunnel_type)
-{
- if(NULL == this_stream){
- return -1;
- }
- /* ������ָ��, ע��: sapp_online���sapp_ip_port_union�治һ�� */
- const struct streaminfo *pfather = this_stream->pfather;
-
- switch(this_stream->addr.addrtype){
- case ADDR_TYPE_IPV4:
- pfather = this_stream->pfather; /* IP-TCP/UDP����Ϊ��һ������, ����pfatherָ�� */
- if(pfather != NULL){
- if(__ADDR_TYPE_IP_PAIR_V4 == pfather->addr.addrtype){
- *tunnel_type = (unsigned short)STREAM_TUNNLE_IP_IN_IP;
- }else if(__ADDR_TYPE_IP_PAIR_V6 == pfather->addr.addrtype){
- *tunnel_type = (unsigned short)STREAM_TUNNLE_4OVER6;
- }else if(ADDR_TYPE_GRE == pfather->addr.addrtype){
- *tunnel_type = (unsigned short)STREAM_TUNNLE_GRE;
- }
- }
- break;
-
- case ADDR_TYPE_IPV6:
- pfather = this_stream->pfather; /* IP-TCP/UDP����Ϊ��һ������, ����pfatherָ�� */
- if(pfather != NULL){
- if(__ADDR_TYPE_IP_PAIR_V4 == pfather->addr.addrtype){
- *tunnel_type = (unsigned short)STREAM_TUNNLE_6OVER4;
- }else if((ADDR_TYPE_IPV4 == pfather->addr.addrtype)
- && (STREAM_TYPE_UDP == pfather->type)){
- *tunnel_type = (unsigned short)STREAM_TUNNLE_TEREDO;
- }else if(ADDR_TYPE_GRE == pfather->addr.addrtype){
- *tunnel_type = (unsigned short)STREAM_TUNNLE_GRE;
- }
- }
- break;
-
- case ADDR_TYPE_GRE:
- if((ADDR_TYPE_IPV4 == upper_stream->addr.addrtype)
- ||((ADDR_TYPE_IPV6 == upper_stream->addr.addrtype))
- ||(__ADDR_TYPE_IP_PAIR_V4 == upper_stream->addr.addrtype)
- ||(__ADDR_TYPE_IP_PAIR_V6 == upper_stream->addr.addrtype)){
- *tunnel_type = (unsigned short)STREAM_TUNNLE_GRE;
- }
- break;
-
- case ADDR_TYPE_PPTP:
- /* pptp_entry()�������Ѿ�����ַ����ΪADDR_TYPE_PPTP, �϶�����PPTP���� */
- *tunnel_type = (unsigned short)STREAM_TUNNLE_PPTP;
- break;
-
- case ADDR_TYPE_L2TP:
- /* l2tp_entry()�������Ѿ�����ַ����ΪADDR_TYPE_L2TP, �϶�����L2TP���� */
- *tunnel_type = (unsigned short)STREAM_TUNNLE_L2TP;
- break;
-
- }
-
- return get_stream_carry_tunnel_type(pfather, this_stream, tunnel_type);
-}
-
-#ifdef __cplusplus
-}
-#endif
-
+ +/* +********************************************* +1. 20130803 ������ +1) ԭ����������hash����������Ĺ���ͬһkeyֵ�ϣ� + ��Ϊ������Ԫ��Ĵ�С��������hash����ͬһ�������ϣ� +2) ͨ���� findandsethashindex ����ʱԤ��Ҫ������������������ٲ���ʱ�����²�� + + +2 . adjust by lqy 20130917 streamaddlist +1) ��������̭֮����Ҫ���¼���Ҫ�����λ�� +2) ÿ����̭��������ĿΪ��������Ŀ��2% + +3 . adjust by lqy 20140810 +1) ������̭ʱ�������100�� + +4. adjust by lqy 20141203 +1)lruʱ����������һ���Ƿ�ʱ����ʱ������̭ + +5.adjust by lqy 20141212 +1)Ϊÿ��������ȫ��Ψһ����stream_index ����ͨ��������� +2)�Ƿ�֧��ȫ���ӿ��ɿ����趨 +3)ÿ�����Ӿ�����lru +4) fix hash��ͻʱ������������� +*/ +#include "stream_internal.h" +#include "stream_manage.h" +#include "sysinfo.h" +#include "packet_io.h" +#include "packet_io_internal.h" +#include "project_requirement.h" +#include "project_internal.h" +#include "dictator.h" +#include <MESA/MESA_handle_logger.h> +#include <stdio.h> +#include <unistd.h> +#include <assert.h> +#include <string.h> +#include <arpa/inet.h> +#include <pthread.h> +#include <sys/select.h> + +#ifdef __cplusplus +extern "C" { +#endif + +int Stream_Analyse_Process_Platform_version_VERSION_20181116; /* nm sapp | grep version */ +int sapp_args_v = 20181116; /* ./sapp -v */ + +/* + ����İ汾������packet_io_libУ��, ��ֹƽ̨��dl.so��ƥ�䵼�������쳣, ��������BUGδ����. +*/ +#if IOMODE_PCAP +int sapp_packet_io_v = 20170120; +#elif IOMODE_PAG +int sapp_packet_io_v = 20151106; +#elif IOMODE_PFRING +int sapp_packet_io_v = 20160505; +#elif IOMODE_DPDK +int sapp_packet_io_v = 20160929; +#elif IOMODE_IPFILE +int sapp_packet_io_v = 20160426; +#elif IOMODE_TOPSEC +int sapp_packet_io_v = 20151014; +#elif IOMODE_MARSIO +int sapp_packet_io_v = 20180716; +#elif IOMODE_SMITH +int sapp_packet_io_v = 20161027; +#elif IOMODE_DPDK_VXLAN +int sapp_packet_io_v = 20161101; +#elif IOMODE_PAG_MARSIO +int sapp_packet_io_v = 20170407; +#endif + +int g_sapp_log_level = 20; +void *g_sapp_log_handle; + +#define STREAM_ONCE_DEL_NUM 1000 +void packet_io_status_stream_hash_update(int thread_seq, unsigned long hash_list_num); +void packet_io_status_stream_update(int tid, UCHAR stream_type, UCHAR stream_state, int action); +static inline void update_stream_status(UCHAR threadnum, UCHAR type, UCHAR stream_state, int action); +int project_requirement_global_init(void); +static inline void __do_streamleavlist(struct streamindex *pindex,struct stream_list *plist); + +int tcp_stream_table_size=0; +int udp_stream_table_size=0; +//add by lqy +unsigned short udp_reset_time=0; +unsigned short link_default_nopkt_time=0; +int tcpstate_num[MAX_TCP_STATE]={8000,2000,4000}; +int udpstate_num[MAX_UDP_STATE]={2000,2000,2000}; + +int tcp_default_unorder=5; +int tcp_creatlink_model=TCP_CTEAT_LINK_BYSYN; +int tcp_flood_detect_model=TCP_FLOOD_DETECT_OFF; +int tcp_support_all=TCP_SUPPORT_ENTRYALL_OFF; +int tcp_dataflood_pktlen=5; +extern int g_packet_io_thread_num; +struct global_stream **G_MESA_GLOBAL_STREAM; + +//static struct stream_status stream_info[MAX_THREAD_NUM]; + +static int init_stream_detail(struct global_stream *g_stream) +{ + int i=0; + memset(g_stream,0,sizeof(struct global_stream)); + for(i=0;i<MAX_TCP_STATE;i++) + { + g_stream->tcpList[i].max_cnt=tcpstate_num[i]; + + //add by lqy����ÿ�λ��������ĿΪ100 + g_stream->tcpList[i].sift_out_cnt=g_stream->tcpList[i].max_cnt/50+1; +#if 0 + if(g_stream->tcpList[i].sift_out_cnt>100) + g_stream->tcpList[i].sift_out_cnt=100; +#else + g_stream->tcpList[i].sift_out_cnt=1; /* lijia modify, ���ٵ�����ʱ̫��, ÿ��ֻ��̭һ�� */ +#endif + g_stream->tcpList[i].cnt=0; + } + for(i=0;i<MAX_UDP_STATE;i++) + { + g_stream->udpList[i].max_cnt=udpstate_num[i]; + //add by lqy����ÿ�λ��������ĿΪ100 + g_stream->udpList[i].sift_out_cnt=g_stream->udpList[i].max_cnt/50+1; +#if 0 + if(g_stream->udpList[i].sift_out_cnt>100) + g_stream->udpList[i].sift_out_cnt=100; +#else + g_stream->udpList[i].sift_out_cnt=1; /* lijia modify, ���ٵ�����ʱ̫��, ÿ��ֻ��̭һ�� */ +#endif + g_stream->udpList[i].cnt=0; + } + g_stream->freeList.max_cnt=tcp_stream_table_size+udp_stream_table_size; + g_stream->freeList.cnt=0; + + //Ԥ�ȷ���hashָ�� +#if USE_RBTREE_INSTEAD_LIST + g_stream->tcp_rb_root_table = (struct rb_root *)malloc (tcp_stream_table_size * sizeof (struct rb_root)); + memset(g_stream->tcp_rb_root_table, 0, tcp_stream_table_size * sizeof (struct rb_root)); + + g_stream->udp_rb_root_table = (struct rb_root *)malloc (tcp_stream_table_size * sizeof (struct rb_root)); + memset(g_stream->udp_rb_root_table, 0, tcp_stream_table_size * sizeof (struct rb_root)); +#else + g_stream->tcp_stream_table = (struct streamindex **)malloc (tcp_stream_table_size * sizeof (char *)); + memset (g_stream->tcp_stream_table, 0, tcp_stream_table_size* sizeof (char *)); + g_stream->udp_stream_table = (struct streamindex **)malloc (udp_stream_table_size * sizeof (char *)); + memset (g_stream->udp_stream_table, 0, udp_stream_table_size* sizeof (char *)); + //��¼hash��״̬ + g_stream->tcp_stream_talbe_hash_count = (unsigned short *)malloc (tcp_stream_table_size* sizeof (short)); + memset (g_stream->tcp_stream_talbe_hash_count, 0, tcp_stream_table_size* sizeof (short)); + g_stream->udp_stream_talbe_hash_count = (unsigned short *)malloc (udp_stream_table_size* sizeof (short)); + memset (g_stream->udp_stream_talbe_hash_count, 0, udp_stream_table_size* sizeof (short)); +#endif + + g_stream->freeList.head=(struct streamindex *) malloc ((g_stream->freeList.max_cnt) * sizeof (struct streamindex)); + memset(g_stream->freeList.head, 0,(g_stream->freeList.max_cnt) * sizeof (struct streamindex)); + for (i = 0; i < g_stream->freeList.max_cnt-1; i++) + { + g_stream->freeList.head[i].next= &(g_stream->freeList.head[i + 1]); + g_stream->freeList.head[i+1].prev=&(g_stream->freeList.head[i]); + //g_stream->freeList.index=i; // Ϊÿ����������һ��ȫ�������� + //adjust by lqy 20150113 + g_stream->freeList.head[i].stream.stream_public.stream_index=i; + } + g_stream->freeList.head[g_stream->freeList.max_cnt-1].next=NULL; + //adjust by lqy 20150113 + g_stream->freeList.head[g_stream->freeList.max_cnt-1].stream.stream_public.stream_index=i; + g_stream->freeList.cnt= g_stream->freeList.max_cnt; + g_stream->freeList.tail= &(g_stream->freeList.head[g_stream->freeList.max_cnt-1]); + + return 0; +} + + +int tcp_set_flood_detect_model(int model) +{ + tcp_flood_detect_model=model; + return 0; +} + +int tcp_set_creatlink_model(int model) +{ + tcp_creatlink_model=model; + return 0; +} +//�����Ƿ���tcpallģʽ +int tcp_set_support_tcpall_model(int model) +{ + tcp_support_all=model; + return 0; +} +int tcp_set_default_unorder_num(unsigned short max_unorder) +{ + tcp_default_unorder=max_unorder; + return 0; +} + +int tcp_set_max_unorder(int max_unorder) //2014-12-25 lijia add, ���ݾ�ƽ̨, xj_fd +{ + tcp_default_unorder=max_unorder; + return 0; +} + +int tcp_set_stream_num(int tcpsyn,int tcpdata,int tcpnouse) +{ + + tcpstate_num[TCP_SYN_STATE]=tcpsyn; + tcpstate_num[TCP_DATA_STATE]=tcpdata; + tcpstate_num[TCP_NOUSE_STATE]=tcpnouse; + return 0; +} +void stream_set_default_nopkt_time(unsigned short timeout) +{ + link_default_nopkt_time=timeout; +} + +static inline UINT64 stream_get_detail_last_mtime(const struct streamindex *pindex) +{ + UINT64 last_mtime; + + if(STREAM_TYPE_TCP == pindex->stream.stream_public.type){ + last_mtime = pindex->stream.stream_public.ptcpdetail->lastmtime; + }else{ + last_mtime = pindex->stream.stream_public.pudpdetail->lastmtime; + } + + return last_mtime; +} + +static void stream_timeout_shift_lrulist(struct streamindex *pindex, unsigned short new_timeout) +{ + unsigned short global_default_timeout; + struct streamindex *tmp_index; + struct stream_list *lru_list_root; + const struct streaminfo *pstream = &pindex->stream.stream_public; + +//long search_times = 0; + + if(STREAM_TYPE_TCP == pindex->stream.stream_public.type){ + global_default_timeout = link_default_nopkt_time; + lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->tcpList[pstream->stream_state]); + }else{ + global_default_timeout = udp_reset_time; + lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->udpList[pstream->stream_state]); + } + + if(lru_list_root->head == lru_list_root->tail){ /* ��ǰ��ʣ���һ����, �����ƶ� */ + return; + } + + assert(lru_list_root->head); + assert(lru_list_root->tail); + __do_streamleavlist(pindex, lru_list_root); /* ��ԭλ��ժ�� */ + assert(lru_list_root->head); + assert(lru_list_root->tail); + + if(global_default_timeout > 0){ + /* + ���������ȫ�ֳ�ʱʱ��, �����Ľڵ�Ӧ�ô�tail->head�������, + ��Ϊ�����Ľڵ㳬ʱʱ�䳤, �����ǵ�ǰ��������, �϶�������Ĭ�ϵ���, �Լ�����������ù���ʱ����������ʱ. + */ + /* + �����ǰ�����õ��³�ʱʱ��, ��LRUĩβ����ʣ��ʱ��(�ް�����ʱ��)����, + ���仰˵, ��������̭������ʱ��Ҳ��LRUĩβ����Ҫ�ͺ�, + ������Ҫ��tail(ʱ����µ�)�����ƶ� . + */ + /* after-insert : head->pindex->tmp_index->tail, ����tmp_index֮ǰ */ + tmp_index = lru_list_root->tail; /* �����timeoutֵ��, �Ӳ��ױ���̭��, ��tail�ڵ㿪ʼ�Ƚ� */ + while(tmp_index + && (new_timeout < (tmp_index->stream.timeout -(g_CurrentTime -stream_get_detail_last_mtime(tmp_index))))){ /* �µij�ʱʱ�� < tmp����ʣ������ʱ�� */ +//printf("##### serach udp timeout place, %ld.....\n", search_times++); + tmp_index = tmp_index->prev; + } + + /* ������λ�� */ + if(NULL == tmp_index){ /* ��tail��ʼһֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�head */ + lru_list_root->head->prev = pindex; + pindex->next = lru_list_root->head; + lru_list_root->head = pindex; + pindex->prev = NULL; + }else{ + if(tmp_index->prev){ + tmp_index->prev->next = pindex; + }else{ + lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */ + } + pindex->prev = tmp_index->prev; + tmp_index->prev = pindex; + pindex->next = tmp_index; + } + }else{ + /* + ���û��������ȫ�ֳ�ʱʱ��, ����ΪĬ�ϳ�ʱʱ��ʱ����, ����������֮���ǿ����̭������. + ��ô�����Ľڵ�Ӧ�ô�head->tail�������, + ��Ϊ�����Ľڵ㳬ʱʱ���Ҳ��65535��, �϶������е������糬ʱ. + */ + tmp_index = lru_list_root->head; /* �����timeout��Ĭ��0�����ױ���̭ */ + while(tmp_index + && (tmp_index->stream.timeout != 0) /* ֮ǰij����Ҳ���������˳�ʱʱ�� */ + && (new_timeout > (tmp_index->stream.timeout - (g_CurrentTime -stream_get_detail_last_mtime(tmp_index))))){ /* �µij�ʱʱ�� > tmp����ʣ������ʱ�� */ +//printf("##### serach udp timeout place, %ld.....\n", search_times++); + tmp_index = tmp_index->next; + } + + /* ������λ�� */ + if(NULL == tmp_index){ /* ��head��ʼһֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�tail */ + lru_list_root->tail->next = pindex; + pindex->prev = lru_list_root->tail; + lru_list_root->tail = pindex; + pindex->next = NULL; + }else{ + if(tmp_index->prev){ + tmp_index->prev->next = pindex; + }else{ + lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */ + } + pindex->prev = tmp_index->prev; + tmp_index->prev = pindex; + pindex->next = tmp_index; + } + } + + lru_list_root->cnt++; /* ��__do_streamleavlist()�м�1, �˴��ָ����� */ + return; +} + +/* + NOTE: + �����õij�ʱʱ������ȫ�ֱ�timeoutֵ��!! + ��Ϊȫ����������ֻ��һ��LRU_time����, + ������ֵ�ܴ�, ���Խ���ǰ����LRUĩβ�����ʵ�λ��, + �������ǰֵ��ȫ�ֱ�С, ������Ҫ��LRU������tail����ѯ�ܶ���������ҵ��ʵ�λ��, + ���µ�����ʱ̫��. +*/ +int stream_set_single_stream_timeout(const struct streaminfo *pstream,unsigned short timeout) +{ + int ret=0; + struct streaminfo_private *pstream_pr=(struct streaminfo_private *)pstream; + struct streamindex *pindex; + + if(OP_STATE_CLOSE == pstream->opstate){/* ��ǰ���ѽ���, ���賬ʱҲû�������� */ + return -1; + } + + if((0 == pstream_pr->timeout) /* 0Ϊ��δ���ù� */ + || (timeout >= pstream_pr->timeout)) /* �����õij�ʱֵ������ڵ��ڵ�ǰֵ */ + { + /* TODO, �������ƶ���LRU��ʱ�����ʵ���λ��, ��head��ʼ��ʣ��ij�ʱʱ������ + NOTE: + ��Ϊ����ȫ�ֶ�û������ʱ, ����Ե����������˳�ʱ, ��ô����Ӧ���ƶ���LRUĩβ, + ����del_stream_by_time()����ֻ���ж���ĩβ�����Ƿ�ʱ, �����ж���������. + + ����: ������Ϊ�˱��������١�����ʱ�䳤����, ������������, ���ܳ�����������%p; + */ + pindex = sapp_get_struct_header(pstream_pr, struct streamindex, stream); + + if(STREAM_TYPE_UDP==pstream->type) + { + if(timeout < udp_reset_time){ /* �����ȫ��Ĭ��ֵ��, �Ҳ��ܱ�֮ǰ���õ�С */ + return -1; + } + if(sapp_global_mthread[pstream->threadnum].udp_stream_special_timeout_num >= udpstate_num[UDP_TWO_STATE]/5){ + return -2; + } + if(pstream_pr->timeout == udp_reset_time){ + /* �������������ó�ʱʱ��, ��ǰ����һ�����ó�ʱʱ���¼���� */ + sapp_global_mthread[pstream->threadnum].udp_stream_special_timeout_num++; + } +#if 0 + lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->udpList[pstream->stream_state]); + if(lru_list_root->head == lru_list_root->tail){ /* ��ǰ��ʣ���һ����, �����ƶ� */ + ret = 0; + goto done; + } + + /* before-insert : head->tmp_index->tail */ + + assert(lru_list_root->head); + assert(lru_list_root->tail); + __do_streamleavlist(pindex, lru_list_root); /* ��ԭλ��ժ�� */ + assert(lru_list_root->head); + assert(lru_list_root->tail); + /* + �����ǰ�����õ��³�ʱʱ��, ��LRUĩβ����ʣ��ʱ��(�ް�����ʱ��)����, + ���仰˵, ��������̭������ʱ��Ҳ��LRUĩβ����Ҫ�ͺ�, + ������Ҫ��tail(ʱ����µ�)�����ƶ� . + */ + /* after-insert : head->pindex->tmp_index->tail, ����tmp_index֮ǰ */ +long udp_times = 0; + tmp_index = lru_list_root->tail; /* �����timeoutֵ��, �Ӳ��ױ���̭��, ��tail�ڵ㿪ʼ�Ƚ� */ + while(tmp_index + && (tmp_index->stream.timeout != 0) /* timeoutĬ��Ϊ0����, ��û�賬ʱʱ��, ����Ϊ������ */ + && (timeout < (g_CurrentTime - tmp_index->stream.stream_public.pudpdetail->lastmtime))){ +printf("##### serach udp timeout place, %ld.....\n", udp_times++); + tmp_index = tmp_index->prev; + } + + /* ������λ�� */ + if(NULL == tmp_index){ /* һֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�head */ + lru_list_root->head->prev = pindex; + pindex->next = lru_list_root->head; + lru_list_root->head = pindex; + pindex->prev = NULL; + }else{ + if(tmp_index->prev){ + tmp_index->prev->next = pindex; + }else{ + lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */ + } + pindex->prev = tmp_index->prev; + tmp_index->prev = pindex; + pindex->next = tmp_index; + } + lru_list_root->cnt++; /* ��__do_streamleavlist()�м�1, �˴��ָ����� */ +#endif + } + else if (STREAM_TYPE_TCP==pstream->type) + { + if(timeout < link_default_nopkt_time){ /* �����ȫ��Ĭ��ֵ��, �Ҳ��ܱ�֮ǰ���õ�С */ + return -1; + } + if(sapp_global_mthread[pstream->threadnum].tcp_stream_special_timeout_num >= tcpstate_num[TCP_DATA_STATE]/10){ + return -2; + } + if(pstream_pr->timeout == link_default_nopkt_time){ + /* �������������ó�ʱʱ��, ��ǰ����һ�����ó�ʱʱ���¼���� */ + sapp_global_mthread[pstream->threadnum].tcp_stream_special_timeout_num++; + } +#if 0 + lru_list_root=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->tcpList[pstream->stream_state]); + if(lru_list_root->head == lru_list_root->tail){ /* ��ǰ��ʣ���һ����, �����ƶ� */ + ret = 0; + goto done; + } + + assert(lru_list_root->head); + assert(lru_list_root->tail); + __do_streamleavlist(pindex, lru_list_root); /* ��ԭλ��ժ��, �˴�����ֱ��ʹ��streamleavlist, ���ò��ִ��� */ + assert(lru_list_root->head); + assert(lru_list_root->tail); + /* + �����ǰ�����õ��³�ʱʱ��, ��LRUĩβ����ʣ��ʱ��(�ް�����ʱ��)����, + ���仰˵, ��������̭������ʱ��Ҳ��LRUĩβ����Ҫ�ͺ�, + ������Ҫ��tail(ʱ����µ�)�����ƶ� . + */ +long tcp_times = 0; + tmp_index = lru_list_root->tail; + while(tmp_index + && (tmp_index->stream.timeout != 0) /* timeoutĬ��Ϊ0����, ��û�賬ʱʱ��, ����Ϊ������ */ + && (timeout < (g_CurrentTime - tmp_index->stream.stream_public.ptcpdetail->lastmtime))){ +printf("##### serach tcp timeout place, %ld.....\n", tcp_times); + tmp_index = tmp_index->prev; + } + + /* ������λ�� */ + if(NULL == tmp_index){ /* һֱ�ҵ����һ���ڵ�, pindexӦ����Ϊ�µ�head */ + lru_list_root->head->prev = pindex; + pindex->next = lru_list_root->head; + lru_list_root->head = pindex; + pindex->prev = NULL; + }else{ + if(tmp_index->prev){ + tmp_index->prev->next = pindex; + }else{ + lru_list_root->head = pindex; /* ��Ϊhead�ڵ� */ + } + pindex->prev = tmp_index->prev; + tmp_index->prev = pindex; + pindex->next = tmp_index; + } + lru_list_root->cnt++; /* ��__do_streamleavlist()�м�1, �˴��ָ����� */ +#endif + }else{ + goto err; /* ��֧��TCP��UDP�� */ + } + + } + else + { + goto err; + } + + stream_timeout_shift_lrulist(pindex, timeout); + + pstream_pr->timeout=timeout; + return ret; + +err: + return -1; +} +#if 0 +void udp_set_stream_resettime(unsigned short timeout) +{ + udp_reset_time=timeout; +} +#endif + +void udp_set_stream_timeout(unsigned short timeout)//2014-12-25 lijia add, ���ݾ�ƽ̨, xj_fd +{ + udp_reset_time=(unsigned short)timeout; +} + +void udp_set_stream_resettime(unsigned short timeout) +{ + udp_reset_time=(unsigned short)timeout; +} + +int udp_set_stream_num(int udp1,int udp2,int udp3) +{ + udpstate_num[UDP_ONE_STATE]=udp1; + udpstate_num[UDP_TWO_STATE]=udp2; + udpstate_num[UDP_MORE_STATE]=udp3; + + return 0; +} + +int init_stream_manage(int threadcount) +{ + int i; + + wy_init_hash(); + for(i=0;i<MAX_TCP_STATE;i++) + tcp_stream_table_size+=tcpstate_num[i]; + for(i=0;i<MAX_UDP_STATE;i++) + udp_stream_table_size+=udpstate_num[i]; + + G_MESA_GLOBAL_STREAM = (struct global_stream **)malloc(threadcount * sizeof (char *)); + memset (G_MESA_GLOBAL_STREAM, 0, threadcount * sizeof (char *)); + for(i=0;i<threadcount;i++) + { + G_MESA_GLOBAL_STREAM[i]=(struct global_stream *)malloc(sizeof(struct global_stream)); + init_stream_detail(G_MESA_GLOBAL_STREAM[i]); + } + + //add 20140626 ��ʼ��procject�ӿ� + project_requirement_global_init(); + + return 0; +} + +static struct streaminfo *del_stream(struct streamindex *pindex) +{ + struct streaminfo_private *pstream_pr=&(pindex->stream); + struct streaminfo *pstream=&(pstream_pr->stream_public); + + if(STREAM_TYPE_UDP==pstream->type) + { + udp_free_stream(pindex); + } + else if (STREAM_TYPE_TCP==pstream->type) + { + ((struct tcpdetail_private*)(pstream->pdetail))->link_state=STREAM_LINK_LRU_OUT; + tcp_free_stream(pindex, NULL,NULL,NULL); + } + + return NULL; +} + +static inline void __do_streamleavlist(struct streamindex *pindex,struct stream_list *plist) +{ + if(pindex->prev != NULL) + { + pindex->prev->next = pindex->next; + } + else + { + plist->head=pindex->next; + if(plist->head!=NULL) + plist->head->prev = NULL; + } + if(pindex->next != NULL) + { + pindex->next->prev = pindex->prev; + } + else + { + plist->tail = pindex->prev; + if(plist->tail!=NULL) + plist->tail->next = NULL; + } + pindex->next= NULL; /* NOTE: �˴�Ӧ�ÿ�, �������ܻ���addlist���� */ + pindex->prev = NULL; /* NOTE: �˴�Ӧ�ÿ�, �������ܻ���addlist���� */ + plist->cnt--; + + return; +} + +void streamleavlist(struct streamindex *pindex,struct stream_list *plist) +{ + struct streaminfo *pstream=(struct streaminfo *)(&(pindex->stream)); + + __do_streamleavlist(pindex, plist); + + update_stream_status(pstream->threadnum, pstream->type, pstream->stream_state, -1); + +#if DEBUG + packet_io_status_stream_update(pstream->threadnum, pstream->type, pstream->stream_state, -1); +#endif +} + + +void streamaddlist(struct streamindex *pindex,struct stream_list *plist) +{ + struct streamindex *tmpstream; + struct streaminfo *pstream=(struct streaminfo *)(&(pindex->stream)); + if((plist->head != NULL) && (plist->cnt >= plist->max_cnt-1)) + { + //ÿ����̭�ܴ�С��2% + //istreamdelnum=plist->max_cnt/50+1; + //adjust by lqy 20140810 + //istreamdelnum=plist->sift_out_cnt; + + /* + 2017-06-07 lijia add, + ���жϵ�ǰLRUĩβ�����Ƿ��˳�ʱʱ��, �����DZ���IM�Ȱ����١�ʱ�䳤���� + ����: ���ȫ��Ĭ����60��, ����Ϊ����̫��, ��û���ﳬʱʱ��, ��ô��? + + ʹ���������, if(pstream->timtout > defalut) && (g_current - lastmtime < pstream->timtout) + + */ + tmpstream = plist->head; + + while(tmpstream){ + if(STREAM_TYPE_TCP == tmpstream->stream.stream_public.type){ + /* ��ǰ�����г�ʱʱ���Ĭ��ȫ�ֵĴ�, �һ�û�г�ʱ, ������̭! */ + if((tmpstream->stream.timeout > link_default_nopkt_time) + && (g_CurrentTime - tmpstream->stream.stream_public.ptcpdetail->lastmtime < tmpstream->stream.timeout)){ +/* +printf("###### %s timeout is %d, remain %lld, can't be free!\n", printaddr(&tmpstream->stream.stream_public.addr, tmpstream->stream.stream_public.threadnum), + tmpstream->stream.timeout, + g_CurrentTime - tmpstream->stream.stream_public.ptcpdetail->lastmtime); +*/ + tmpstream = tmpstream->next; + }else{ + del_stream(tmpstream); + break; + } + }else{ + if((tmpstream->stream.timeout > udp_reset_time) + && (g_CurrentTime - tmpstream->stream.stream_public.pudpdetail->lastmtime < tmpstream->stream.timeout)){ + tmpstream = tmpstream->next; + }else{ + del_stream(tmpstream); + break; + } + } + } + } + pindex->prev = plist->tail; + pindex->next= NULL; + if(plist->tail != NULL) + { + plist->tail->next = pindex; + plist->tail = pindex; + } + else + { + plist->tail = pindex; + plist->head = pindex; + } + plist->cnt++; + //add by lqy 20120919 just for show + +#if 0 + if(pstream->type==STREAM_TYPE_TCP) + { + switch(pstream->stream_state) + { + case TCP_SYN_STATE: + stream_info[pstream->threadnum].syn_cnt++; + break; + case TCP_DATA_STATE: + stream_info[pstream->threadnum].ack_cnt++; + break; + case TCP_NOUSE_STATE: + stream_info[pstream->threadnum].tcp_num++; + break; + } + + } +#endif + update_stream_status(pstream->threadnum, pstream->type, pstream->stream_state, 1); +#if DEBUG + packet_io_status_stream_update(pstream->threadnum, pstream->type, pstream->stream_state, 1); +#endif +} + +//struct streamindex *malloc_streamindex(int threadnum) +struct streamindex *malloc_and_copy_streamindex(int threadnum, struct streamindex *pindex_stack) +{ + struct streamindex *pindex=NULL; + struct global_stream *g_stream=G_MESA_GLOBAL_STREAM[threadnum]; + + pindex =g_stream->freeList.head; + + g_stream->freeList.index=pindex->stream.stream_public.stream_index;//add by lqy 20150113 + g_stream->freeList.head=pindex->next; + g_stream->freeList.cnt--; + + memcpy(pindex,pindex_stack,sizeof(struct streamindex)); + + pindex->stream.stream_public.stream_index=g_stream->freeList.index; + + pindex->stream.stream_killed_flag = 0; + pindex->stream.hash_not_head_times = 0; + pindex->stream.stream_carry_up_layer_tunnel_type = 0; + pindex->stream.stream_low_layer_tunnel_type = 0; + + return pindex; + +} +void free_streamindex(int threadnum,struct streamindex *pindex) +{ + struct global_stream *g_stream=G_MESA_GLOBAL_STREAM[threadnum]; + //free(pindex); + if(g_stream->freeList.tail==NULL) + { + assert(0); + pindex->prev=NULL; + pindex->next=NULL; + g_stream->freeList.tail=pindex; + g_stream->freeList.head=pindex; + } + else + { + g_stream->freeList.tail->next=pindex; + pindex->prev=g_stream->freeList.tail; + pindex->next=NULL; + g_stream->freeList.tail=pindex; + + } + g_stream->freeList.cnt++; + +#if SAPP_INSECTICIDE + /* ��free���������ṹ��������, ��ֹuse-after-free�����, ע��ֻ�������ڲ���stream�ṹ, pindex�ṹ��ָ��, ��������Ķ� */ + /* 2018-09-20 lijia close! �˴��ر����stream, ��Ϊ�����ܶ�ط����м�����op_state��ֵ, ���ó�0xFE�ᵼ��Ī����������� */ + //memset(&pindex->stream, 0xFE, sizeof(struct streaminfo_private)); +#endif +} + +//add by lqy 20141203 +//�������β�������һ�������Ƿ�ʱ�������ʱ����̭֮ + +/* 2015-06-10 lijia add arg "current_drive_index", ��ʾ��ǰ���ṹָ�� + return value: + 0: normal; + 1: del self by time. + + modify by lqy 20150612 + ��ʱɾ��������ͷ����ʼ��һ��ɾ�� +*/ +int del_stream_by_time(struct stream_list *plist, const struct streamindex *current_drive_index) +{ + struct streamindex *pindex; + struct streaminfo_private *pstream_pr; + struct streaminfo *pstream; + long lastmtime; + + pindex=plist->head; + pstream_pr=(&(pindex->stream)); + + if(0 == pstream_pr->timeout){ /* δ������ʱ��̭ */ + return 0 ; + } + + pstream=&pstream_pr->stream_public; + + if(STREAM_TYPE_UDP==pstream->type) + { + lastmtime=((struct udpdetail *)(pstream->pdetail))->lastmtime; + } + else if (STREAM_TYPE_TCP==pstream->type) + { + lastmtime=((struct tcpdetail *)(pstream->pdetail))->lastmtime; + } + else + { + sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: del_stream_by_time() error! stream->type is:%d, not TCP or UDP.\n", __FILE__, __LINE__, pstream->type); + return -1; + //assert(0); + } + + //����趨�˳�ʱʱ�䣬�����Ѿ���ʱ + if(unlikely(g_CurrentTime-lastmtime > pstream_pr->timeout)) + { + if(pindex == current_drive_index){ /* ��ǰ�����ڵ�����ʱ, ˵��֮ǰ�ܳ�ʱ��û�а�����, ��ʱ����free, ���Ƿ���1, �ٵ���reset */ + if (STREAM_TYPE_TCP==pstream->type) + { + /* 2016-12-15 lijia add, ��ʱreset֮ǰ, ��Ҫ����link_state */ + ((struct tcpdetail_private *)(pstream->pdetail))->link_state=STREAM_LINK_TIMEOUT; + } + return 1; + } +#if 0 /* 2016-12-15 lijia close, ������ز�����tcp_free_stream()��, �˴����λ����ָ�����, �ڴ�й©, ��δ֪BUG */ + if(pindex->next!=NULL) + { + pindex->next->prev =NULL; + plist->head= pindex->next; + } + else + { + plist->head=plist->tail=NULL; + } +#endif + if (STREAM_TYPE_TCP==pstream->type) + { + ((struct tcpdetail_private *)(pstream->pdetail))->link_state=STREAM_LINK_TIMEOUT; + tcp_free_stream(pindex, NULL,NULL,NULL); + } + else + { + udp_free_stream(pindex); + } + } + + return 0; +} + +/* + return value: + 0: normal; + 1: del self by timeout, need reset. +*/ +int lrustream(struct streamindex *pindex) +{ + struct stream_list *plist=NULL; + struct streamindex * next,*prev; + struct streaminfo_private *pstream_pr=&(pindex->stream); + struct streaminfo *pstream = &pstream_pr->stream_public; + + if(STREAM_TYPE_UDP==pstream->type) + { + plist=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->udpList[pstream->stream_state]); + if(1 == del_stream_by_time(plist, pindex)){ + return 1; + } + } + else if (STREAM_TYPE_TCP==pstream->type) + { + plist=&(G_MESA_GLOBAL_STREAM[pstream->threadnum]->tcpList[pstream->stream_state]); + /* 2015-06-10 lijia modify, UDPʹ��udp_reset_stream_bytime()��̭ */ + if(1 == del_stream_by_time(plist, pindex)){ + return 1; + } + } + +// if ( plist->cnt > STREAM_ONCE_DEL_NUM && + if(plist->tail && (plist->tail != pindex)) /* �����ǰ�ڵ㲻���������´�, LRU */ + { + next = pindex->next; + prev = pindex->prev; + if(next) next->prev = prev; + if(prev) prev->next = next; + plist->tail->next = pindex ; + pindex->prev = plist->tail; + pindex->next = NULL; + plist->tail= pindex; + + if(plist->head == pindex) + { + plist->head = next; + plist->head->prev = NULL; + } + } + + return 0; +} + +#if (0 == USE_LINUX_KERNEL_HASH_ALGO) +static int mkstreamhash_single(struct streaminfo_private *pstream_pr, int maxsize) +{ + char addr_hash_buf[MTU_MAX]; + char *hdata; + struct streaminfo *this_stream = &pstream_pr->stream_public; + struct layer_addr *saddr = &this_stream->addr; + + /* lijia comment, ����hashʱʹ��ͬһ�ַ���, ��"src>=dst" */ + + /* ��Ԫ������, ����һ������, ��ʱֻ�ܿ�ip�ķ���, �˿���Ϣ��ͨ��ָ����ݻ�ȥ, �̲���ʹ�� */ + if(0 == pstream_pr->layer_dir){ + memcpy(addr_hash_buf, saddr->paddr, this_stream->addr.addrlen); + reverse_addr((void *)addr_hash_buf, saddr->addrtype); + hdata = (char *)&addr_hash_buf[0]; + }else{ + hdata = (char *)(saddr->paddr); + } + + return mkaddrhash(hdata, this_stream->addr.addrlen) % maxsize; +} + + +int mkstreamhash_new(struct streaminfo_private *pstream_pr, int maxsize) +{ + int hash = 0; + struct streaminfo *this_stream = &pstream_pr->stream_public; + + switch(this_stream->addr.addrtype){ + case __ADDR_TYPE_IP_PAIR_V4: + case __ADDR_TYPE_IP_PAIR_V6: + case ADDR_TYPE_VLAN: + case ADDR_TYPE_MAC: + case ADDR_TYPE_PPPOE_SES: + case ADDR_TYPE_L2TP: + hash = mkstreamhash_single(pstream_pr, maxsize); + break; + + case ADDR_TYPE_IPV4: + case ADDR_TYPE_IPV6: + case ADDR_TYPE_TCP: + case ADDR_TYPE_UDP: +#if 0 + /* + TCP��UDPЭ�������, ���ֻ�ñ���Ķ˿ڼ���HASH, ��ͻ��̫��, + ��Ҫ����fstream�е�IP��ַ, ʹ����Ԫ��һ�����. + */ + hash = mkstreamhash_dual(this_stream, maxsize); +#else + hash = mkstreamhash_new((struct streaminfo_private *)this_stream->pfather, maxsize); +#endif + break; + break; + + default: + printf("Unknown addr type:%d!", this_stream->addr.addrtype); + sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: mkstreamhash_new() Unknown addr type:%d.\n", __FILE__, __LINE__, this_stream->addr.addrtype); + //assert(0); + return -1; + break; + } + + return hash; +} +#endif + +static int cmpaddr_positive(void *addr_heap, void *addr_stack, UCHAR addrtype, UCHAR addrlen) +{ + int ret; + + switch(addrtype){ + case ADDR_TYPE_IPV4: + { + /* 2015-07-14 lijia modify, ��Ҫ�Ƚ϶˿� */ + struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap; + struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack; + + /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */ + ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->source; + if(ret != 0){ + return ret; + } + ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->saddr; + if(ret != 0){ + return ret; + } + ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->daddr; + if(ret != 0){ + return ret; + } + ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->dest; + if(ret != 0){ + return ret; + } + } + break; + + case ADDR_TYPE_IPV6: + { + struct stream_tuple4_v6 *ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap; + struct stream_tuple4_v6 *ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack; + /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */ + ret = (int)ip6_addr_heap->source - (int)ip6_addr_stack->source; + if(ret != 0){ + return ret; + } + ret = memcmp(ip6_addr_heap->saddr, ip6_addr_stack->saddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + ret = memcmp(ip6_addr_heap->daddr, ip6_addr_stack->daddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + ret = (int)ip6_addr_heap->dest - (int)ip6_addr_stack->dest; + if(ret != 0){ + return ret; + } + } + break; + + case __ADDR_TYPE_IP_PAIR_V4: + { + struct stream_tuple4_v4 *pure_ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap; + struct stream_tuple4_v4 *pure_ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack; + + ret = (int)pure_ip4_addr_heap->saddr - (int)pure_ip4_addr_stack->saddr; + if(ret != 0){ + return ret; + } + ret = (int)pure_ip4_addr_heap->daddr - (int)pure_ip4_addr_stack->daddr; + if(ret != 0){ + return ret; + } + } + break; + + case __ADDR_TYPE_IP_PAIR_V6: + { + struct stream_tuple4_v6 *pure_ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap; + struct stream_tuple4_v6 *pure_ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack; + + ret = memcmp(pure_ip6_addr_heap->saddr, pure_ip6_addr_stack->saddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + ret = memcmp(pure_ip6_addr_heap->daddr, pure_ip6_addr_stack->daddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + } + break; + + /* TODO: MPLS��ַֻ�Ƚ�label, TTL�Ȳ������ַHASH */ + + default: + /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת��, ֱ�ӱȽϼ��� */ + ret = memcmp(addr_heap, addr_stack, addrlen); + } + + return ret; +} +static int cmpaddr_reverse(void *addr_heap, void *addr_stack, UCHAR addrtype, UCHAR addrlen) +{ + int ret; + + switch(addrtype){ + case ADDR_TYPE_IPV4: + { + /* 2015-07-14 lijia modify, ��Ҫ�Ƚ϶˿� */ + struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap; + struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack; + + /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */ + ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->dest; + if(ret != 0){ + return ret; + } + ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->daddr; + if(ret != 0){ + return ret; + } + ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->saddr; + if(ret != 0){ + return ret; + } + ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->source; + if(ret != 0){ + return ret; + } + } + break; + + case ADDR_TYPE_IPV6: + { + struct stream_tuple4_v6 *ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap; + struct stream_tuple4_v6 *ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack; + /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */ + ret = (int)ip6_addr_heap->source - (int)ip6_addr_stack->dest; + if(ret != 0){ + return ret; + } + ret = memcmp(ip6_addr_heap->saddr, ip6_addr_stack->daddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + ret = memcmp(ip6_addr_heap->daddr, ip6_addr_stack->saddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + ret = (int)ip6_addr_heap->dest - (int)ip6_addr_stack->source; + if(ret != 0){ + return ret; + } + } + break; + + case __ADDR_TYPE_IP_PAIR_V4: + { + struct stream_tuple4_v4 *pure_ip4_addr_heap = (struct stream_tuple4_v4 *)addr_heap; + struct stream_tuple4_v4 *pure_ip4_addr_stack = (struct stream_tuple4_v4 *)addr_stack; + + ret = (int)pure_ip4_addr_heap->saddr - (int)pure_ip4_addr_stack->daddr; + if(ret != 0){ + return ret; + } + ret = (int)pure_ip4_addr_heap->daddr - (int)pure_ip4_addr_stack->saddr; + if(ret != 0){ + return ret; + } + } + break; + + case __ADDR_TYPE_IP_PAIR_V6: + { + struct stream_tuple4_v6 *pure_ip6_addr_heap = (struct stream_tuple4_v6 *)addr_heap; + struct stream_tuple4_v6 *pure_ip6_addr_stack = (struct stream_tuple4_v6 *)addr_stack; + + ret = memcmp(pure_ip6_addr_heap->saddr, pure_ip6_addr_stack->daddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + ret = memcmp(pure_ip6_addr_heap->daddr, pure_ip6_addr_stack->saddr, IPV6_ADDR_LEN); + if(ret != 0){ + return ret; + } + } + break; + + default: + /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת�� */ + ret = memcmp(addr_heap, addr_stack, addrlen); + } + + return ret; +} + + +#if USE_RBTREE_INSTEAD_LIST +/* + pheap_stream_pr : already in stream htable. + pstack_stream_pr: new element recv from network; +*/ +static int cmpaddr_new(struct streaminfo_private *pheap_stream_pr, + struct streaminfo_private *pstack_stream_pr, unsigned short hash_elem_count) +{ + int ret; + struct layer_addr *heap_addr, *stack_addr; + + heap_addr = &pheap_stream_pr->stream_public.addr; + stack_addr = &pstack_stream_pr->stream_public.addr; + + if(pheap_stream_pr->stream_dir == pstack_stream_pr->layer_dir){ + struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)heap_addr->tuple4_v4; + struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)stack_addr->tuple4_v4; + + /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */ + ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->source; + if(ret != 0){ + goto done; + } + ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->saddr; + if(ret != 0){ + goto done; + } + ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->daddr; + if(ret != 0){ + goto done; + } + ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->dest; + if(ret != 0){ + goto done; + } + + ret = 0; + }else{ + struct stream_tuple4_v4 *ip4_addr_heap = (struct stream_tuple4_v4 *)heap_addr->tuple4_v4; + struct stream_tuple4_v4 *ip4_addr_stack = (struct stream_tuple4_v4 *)stack_addr->tuple4_v4; + + /* 2015-07-15 lijia modify, ����ܲ�һ���ı���������ǰ��Ƚ� */ + ret = (int)ip4_addr_heap->source - (int)ip4_addr_stack->dest; + if(ret != 0){ + goto done; + } + ret = (int)ip4_addr_heap->saddr - (int)ip4_addr_stack->daddr; + if(ret != 0){ + goto done; + } + ret = (int)ip4_addr_heap->daddr - (int)ip4_addr_stack->saddr; + if(ret != 0){ + goto done; + } + ret = (int)ip4_addr_heap->dest - (int)ip4_addr_stack->source; + if(ret != 0){ + goto done; + } + + ret = 0; + } + +done: + if(0 == ret){ + //pst1->p_layer_header = pst2->p_layer_header; + pheap_stream_pr->offset_to_raw_pkt_hdr = pstack_stream_pr->offset_to_raw_pkt_hdr; + } + + return ret; +} +#else + +/* + pheap_stream_pr : already in stream htable. + pstack_stream_pr: new element recv from network; +*/ +static int cmpaddr_new(struct streaminfo_private *pheap_stream_pr, + struct streaminfo_private *pstack_stream_pr, unsigned short hash_elem_count) +{ + int ret; + struct layer_addr *heap_addr, *stack_addr; + + heap_addr = &pheap_stream_pr->stream_public.addr; + stack_addr = &pstack_stream_pr->stream_public.addr; + + if(pheap_stream_pr->stream_dir == pstack_stream_pr->layer_dir){ + /* 2015-07-15 lijia modify, + ��Ԫ��Ƚ�ʹ�����ʽ, ȡ����HASH��ͻ��, + ��ͻ�ʸ�ʱʹ��cmpaddr_positive()���ܸ���, + ����ʹ��memcmp()���ܺ�. + + Ŀǰƽ̨��HASH��ͻ��С��20%, ��ʱʹ��memcmp(). + */ + if(likely(hash_elem_count <= 2)){ + ret = memcmp(heap_addr->paddr, stack_addr->paddr, heap_addr->addrlen); + }else{ + ret = cmpaddr_positive(heap_addr->paddr, stack_addr->paddr, + heap_addr->addrtype, heap_addr->addrlen); + } + }else{ + /* 2015-03-04 lijia add, �����ַ�Ƚ�, ������COPY.*/ + ret = cmpaddr_reverse(heap_addr->paddr, stack_addr->paddr, + heap_addr->addrtype, heap_addr->addrlen); + } +#if 0 +printf("lijia debug: cmpaddr, addr_len:%d:\n", pst1->addr.addrlen); +int i; +printf("heap addr \t :"); +for(i = 0; i < pst1->addr.addrlen; i++){ +printf("%02x,", ((UINT8 *)pst1->addr.paddr)[i]); +} +printf(" <-->\n"); + +printf("new stack addr:\t"); + for(i = 0; i < pst2->addr.addrlen; i++){ + printf("%02x,", ((UINT8 *)ptmp)[i]); + } +printf("\n====================================================\n\n"); +#endif + + if(0 == ret){ + //pst1->p_layer_header = pst2->p_layer_header; + pheap_stream_pr->offset_to_raw_pkt_hdr = pstack_stream_pr->offset_to_raw_pkt_hdr; + } + + return ret; +} + +#endif + +/*�Ƚ��������ṹ���С */ +//adjust 20140324 �Ƚ��������ṹ�Ƿ���ͬ������pfather �ڵ���ȫ��ͬ, +//�ݹ�Ƚ��������ĸ�������� + +/* lijia comment: + pst1: in htable. + pst2: new element; + + return value: + 0 : found, success; + 1 : pst1 > pst2; + -1: pst1 < pst2 +*/ +int checkstreamorder(struct streaminfo_private *pheap_stream_pr, + struct streaminfo_private *pstack_stream_pr, unsigned short hash_elem_count) +{ + int ret; + //int isreverse=0; + struct streaminfo *pheap_stream; + struct streaminfo *pstack_stream; + + if(pheap_stream_pr==NULL) + { + if(unlikely(pstack_stream_pr!=NULL)){ + return -1; + }else{ + return 0; + } + } + else + { + if(unlikely(pstack_stream_pr==NULL)){ + return 1; + } + } + + pheap_stream = &pheap_stream_pr->stream_public; + pstack_stream = &pstack_stream_pr->stream_public; + + ret = (int)(pheap_stream->type - pstack_stream->type); + if(ret != 0) + { + return ret; + } + + ret = (int)(pheap_stream->addr.addrtype - pstack_stream->addr.addrtype); + if(ret != 0) + { + return ret; + } + + /* 2014-04-01 lijia add, ��������Ƚ�, ֱ������ */ + if((0 == pheap_stream_pr->addr_use_as_hash) && (0 == pstack_stream_pr->addr_use_as_hash)){ + pheap_stream_pr->offset_to_raw_pkt_hdr = pstack_stream_pr->offset_to_raw_pkt_hdr; + return checkstreamorder((struct streaminfo_private *)(pheap_stream->pfather), + (struct streaminfo_private *)(pstack_stream->pfather), + hash_elem_count); + } + + ret = cmpaddr_new(pheap_stream_pr, pstack_stream_pr, hash_elem_count); + if(0 != ret){ + return ret; + }else{ + /* 2015-03-11 lijia add, ��Ҫ����ǰ��IP��İ���Ϣ���ݵ�����Ϣ */ + pheap_stream_pr->stream_public.addr.pktipfragtype = pstack_stream_pr->stream_public.addr.pktipfragtype; + } + + ret=checkstreamorder((struct streaminfo_private *)(pheap_stream->pfather), + (struct streaminfo_private *)(pstack_stream->pfather), + hash_elem_count); + if(ret != 0) + { + return ret; + } + + return ret; +} + +#if 0 +static struct streamindex * findandsethashindex_20141217(struct streamindex *plinkhead, + struct streamindex *pinsert, int hash_elem_count) +{ + struct streamindex *phead=plinkhead; + struct streamindex *res = NULL; + struct streaminfo_private *pshead_pr=NULL; + struct streaminfo_private *psinsert_pr=&(pinsert->stream); + int result; + unsigned long hash_list_num = 0; + UCHAR threadnum = psinsert_pr->stream_public.threadnum; + + while(phead!=NULL) + { + pshead_pr=&(phead->stream); +#if USE_LINUX_KERNEL_HASH_ALGO + /* 2015-12-14 lijia add, ��ͨ���ڶ���HASHֵ�����ж��Ƿ����, ���HASH��ͬ, ��ַ�϶���ͬ */ + if(pshead_pr->hash_slave != psinsert_pr->hash_slave){ + phead=phead->phashnext; + hash_list_num++; + continue; + } +#endif + result=checkstreamorder(pshead_pr,psinsert_pr, hash_elem_count); + if(result==0){ + //�ҵ���ͬ�ڵ� + res = phead; + break; + } + //prev=phead; + + phead=phead->phashnext; + hash_list_num++; + } + + if(unlikely(hash_list_num > g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX])){ + g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX] = hash_list_num; + } + +#if DEBUG + packet_io_status_stream_hash_update(threadnum, hash_list_num+1); +#endif + + return res; +} +#endif + +static struct streamindex * findandsethashindex_lru(struct streamindex *plinkhead, + struct streamindex *pinsert, unsigned short hash_elem_count) +{ + struct streamindex *phead=plinkhead; + struct streamindex *res = NULL; + struct streaminfo_private *pshead_pr=NULL; + struct streaminfo_private *psinsert_pr=&(pinsert->stream); + int result; + unsigned long hash_list_num = 0; + UCHAR threadnum = psinsert_pr->stream_public.threadnum; + + while(phead!=NULL) + { + pshead_pr=&(phead->stream); +#if USE_LINUX_KERNEL_HASH_ALGO + /* 2015-12-14 lijia add, ��ͨ���ڶ���HASHֵԤ���ж��Ƿ����, ���HASH��ͬ, ��ַ�϶���ͬ */ + if(pshead_pr->hash_slave != psinsert_pr->hash_slave){ + phead=phead->phashnext; + hash_list_num++; + continue; + } +#endif + result=checkstreamorder(pshead_pr,psinsert_pr, hash_elem_count); + if(result==0){ + res = phead; + break; + } + //prev=phead; + + phead=phead->phashnext; + hash_list_num++; + } + + if(unlikely(hash_list_num > g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX])){ + g_SysInputInfo[threadnum][SYS_HASH_LIST_MAX] = hash_list_num; + } + +#if DEBUG + packet_io_status_stream_hash_update(threadnum, hash_list_num+1); +#endif + + return res; +} + +#if 0 +/*�ж��������Ƿ�����Ӧ��������Ϣ + ���û�У��Կ��ܵIJ���λ�ý���Ԥ��*/ +struct streamindex * findandsethashindex(struct streamindex *plinkhead,struct streamindex *pinsert, int hash_elem_count) +{ + struct streamindex *phead=plinkhead; + struct streamindex *prev=NULL; + struct streaminfo_private *pshead=NULL; + struct streaminfo_private *psinsert=&(pinsert->stream); + int result; + while(phead!=NULL) + { + pshead=&(phead->stream); + result=checkstreamorder(pshead,psinsert,hash_elem_count); + if(result>0) + { + //�ڸýڵ�ǰ���� + pinsert->phashprev=phead->phashprev; + pinsert->phashnext=phead; + return NULL; + } + else if(result==0) + { + //�ҵ���ͬ�ڵ� + return phead; + } + prev=phead; + phead=phead->phashnext; + } + if(phead==NULL) + { + //��ĩβ���� + if(prev!=NULL) + { + pinsert->phashprev=prev; + pinsert->phashnext=NULL; + } + } + return NULL; +} +#endif + +#if 0 +struct streamindex * findandsethashindex_binsearch(int elementcount,struct streamindex *plinkhead, + struct streamindex *pinsert) +{ + struct streamindex *ptag=plinkhead; + struct streaminfo_private *pshead=NULL; + struct streaminfo_private *psinsert=&(pinsert->stream); + int result=0; + + int high=elementcount-1; + int low=0; + int mid=0,i=0,itag=0; + //������ + if(ptag==NULL){ + return NULL; + } + //assert(plinkhead->hash_index==pinsert->hash_index); + while(low<=high) + { + mid=low+(high-low)/2; + if(mid>=itag) + for(i=0;i<(mid-itag);i++) ptag=ptag->phashnext; + else + for(i=0;i<(itag-mid);i++) ptag=ptag->phashprev; + itag=mid; + + //compare + pshead=&(ptag->stream); +#if 1 //2014-12-15 lqy modify + result=checkstreamorder(pshead,psinsert,elementcount); +#else + result=checkstreamorder(psinsert, pshead); +#endif + if(result==0) //�ҵ���ͬ�ڵ� + { + return ptag; + } + + if(result>0) + high=mid-1; + else + low=mid+1; + } + + if(low>high) //����ʧ�� + { + if(result>0) + { + //�ڸýڵ�ǰ���� + pinsert->phashprev=ptag->phashprev; + pinsert->phashnext=ptag; + } + else + { + //�ڸýڵ����� + pinsert->phashprev=ptag; + pinsert->phashnext=ptag->phashnext; + } + } + /* + if(pinsert->phashprev!=NULL) + { + assert(pinsert->phashprev->hash_index==pinsert->hash_index); + } + if(pinsert->phashnext!=NULL) + { + assert(pinsert->phashnext->hash_index==pinsert->hash_index); + } + */ + return NULL; +} +#endif + +#if USE_LINUX_KERNEL_HASH_ALGO +extern int stream_make_hash(struct streaminfo_private *stream_pr, unsigned int maxsize); +#endif + +#if USE_RBTREE_INSTEAD_LIST +extern void rb_sapp_insert(struct streamindex *new_sapp_node, struct rb_root *root); +extern struct streamindex *rb_sapp_search(struct streamindex *new_sapp_node, struct rb_root *rbroot, unsigned int hash_index); +extern void rb_sapp_erase(struct streamindex *new_sapp_node, struct rb_root *root); + + +struct streamindex *findstreamindex(struct streamindex *pindex, const raw_pkt_t *raw_pkt) +{ + struct streamindex **phashstream=NULL; + struct streamindex *a_index=NULL; + struct streaminfo_private *pstream_pr = &(pindex->stream); + struct streaminfo *ptmp=&pstream_pr->stream_public; + struct streaminfo *a_stream=NULL; + UCHAR threadnum = ptmp->threadnum; + UCHAR stream_type = ptmp->type; + unsigned int hash_max; + unsigned int hash_index; + unsigned short elemcount=0; + struct global_stream *this_thread_stream = G_MESA_GLOBAL_STREAM[threadnum]; + struct rb_root *rb_root_table; + + if (STREAM_TYPE_TCP== stream_type) + { + rb_root_table = this_thread_stream->tcp_rb_root_table; + hash_max=tcp_stream_table_size; + } + else if(STREAM_TYPE_UDP==stream_type) + { + rb_root_table = this_thread_stream->udp_rb_root_table; + hash_max=udp_stream_table_size; + } + +#if IOMODE_DPDK + if(raw_pkt->hd_hash != 0){ /* 2016-05-05 lijia add, �ײ�Ӳ���ṩHASH */ + hash_index = raw_pkt->hd_hash % hash_max;; + } + else /* ��������HASHֵ */ +#endif + + { +#if USE_LINUX_KERNEL_HASH_ALGO + hash_index=stream_make_hash(&(pindex->stream), (unsigned int)hash_max); +#else + hash_index=mkstreamhash_new(&(pindex->stream), hash_max); +#endif + } + + if(NULL == rb_root_table[hash_index].rb_node){ /* 2015-12-14 lijia add, HASH����ǰSLOT��û����, ֱ�ӷ��� */ + ptmp->hash_index = hash_index; /* �������õ���HASHֵ */ + return NULL; + } + + a_index = rb_sapp_search(pindex, &rb_root_table[hash_index], hash_index); + + if (likely(a_index!=NULL)){ + pstream_pr = &(a_index->stream); + a_stream=&(pstream_pr->stream_public); +#if 0 /* 2014-12-18 */ + if(a_stream->dirreverse==1) +#else + if(pstream_pr->stream_dir != 1) +#endif + { + if(ptmp->curdir==DIR_C2S) + a_stream->curdir=DIR_S2C; + else + a_stream->curdir=DIR_C2S; + } + else + { + a_stream->curdir=ptmp->curdir; + } + a_stream->routedir=ptmp->routedir; + + return a_index; + } + else + { + ptmp->hash_index=hash_index; + } + + return NULL; +} + +void hash_add_stream(struct streamindex *pindex) +{ + struct streaminfo *ptmp = &pindex->stream.stream_public; + UCHAR threadnum = ptmp->threadnum; + struct rb_root *rb_root_table; + + if (STREAM_TYPE_TCP==ptmp->type) + { + rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->tcp_rb_root_table; + g_SysInputInfo[threadnum][SYS_TCP_LINK_NEW]++; + } + else if(STREAM_TYPE_UDP==ptmp->type) + { + rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->udp_rb_root_table; + g_SysInputInfo[threadnum][SYS_UDP_LINK_NEW]++; + } + + rb_sapp_insert(pindex, &rb_root_table[ptmp->hash_index]); +} + +void hash_del_stream(struct streamindex *pindex) +{ + struct streaminfo *ptmp = &pindex->stream.stream_public; + UCHAR threadnum = ptmp->threadnum; + struct rb_root *rb_root_table; + + if (STREAM_TYPE_TCP==ptmp->type) + { + rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->tcp_rb_root_table; + g_SysInputInfo[threadnum][SYS_TCP_LINK_DEL]++; + } + else if(STREAM_TYPE_UDP==ptmp->type) + { + rb_root_table = G_MESA_GLOBAL_STREAM[threadnum]->udp_rb_root_table; + g_SysInputInfo[threadnum][SYS_UDP_LINK_DEL]++; + } + + rb_sapp_erase(pindex, &rb_root_table[ptmp->hash_index]); +} +#else +/* +//�����������˵����� +//1) �ж��Ƿ������ +//2) �������ķ�����Ϣ ,dir ,curdir ,routdir +*/ +struct streamindex *findstreamindex(struct streamindex *pindex, const raw_pkt_t *raw_pkt) +{ + struct streamindex **phashstream=NULL; + struct streamindex *a_index=NULL; + struct streaminfo_private *pstream_pr = &(pindex->stream); + struct streaminfo *ptmp=&pstream_pr->stream_public; + struct streaminfo *a_stream=NULL; + UCHAR threadnum = ptmp->threadnum; + UCHAR stream_type = ptmp->type; + unsigned int hash_max; + unsigned int hash_index; + unsigned short elemcount=0; + struct global_stream *this_thread_stream = G_MESA_GLOBAL_STREAM[threadnum]; + + if (STREAM_TYPE_TCP== stream_type) + { + phashstream=this_thread_stream->tcp_stream_table; + hash_max=tcp_stream_table_size; + } + else if(STREAM_TYPE_UDP==stream_type) + { + phashstream=this_thread_stream->udp_stream_table; + hash_max=udp_stream_table_size; + }else{ + phashstream = NULL; + hash_max = 0; + assert(0); + } + +#if IOMODE_DPDK + if(raw_pkt->hd_hash != 0){ /* 2016-05-05 lijia add, �ײ�Ӳ���ṩHASH */ + hash_index = raw_pkt->hd_hash % hash_max; + } + + else /* ��������HASHֵ */ +#endif + + { +#if USE_LINUX_KERNEL_HASH_ALGO + hash_index=stream_make_hash(&(pindex->stream), (unsigned int)hash_max); +#else + hash_index=mkstreamhash_new(&(pindex->stream), hash_max); +#endif + } + + + if(STREAM_TYPE_UDP==stream_type) + { + elemcount=this_thread_stream->udp_stream_talbe_hash_count[hash_index]; + } + else if (STREAM_TYPE_TCP==stream_type) + { + elemcount=this_thread_stream->tcp_stream_talbe_hash_count[hash_index]; + } + + if(0 == elemcount){ /* 2015-12-14 lijia add, HASH����ǰSLOT��û����, ֱ�ӷ��� */ + ptmp->hash_index = hash_index; /* �������õ���HASHֵ */ + return NULL; + } + + //ptmp->hash_index=hash_index; + +#if 0 /* TODO, ���ֲ��� */ + a_index= findandsethashindex(phashstream[hash_index],pindex); +#elif 0 + a_index=findandsethashindex_binsearch(elemcount,phashstream[hash_index],pindex); +#elif 0 + a_index= findandsethashindex_20141217(phashstream[hash_index],pindex, elemcount); +#else + a_index = findandsethashindex_lru(phashstream[hash_index],pindex, elemcount); +#endif + + if (likely(a_index!=NULL)){ +/* + 2015-12-14 lijia add + NOTE: + �����ǰ������N�ζ�����HASH_SLOT�ĵ�һλ, ���ƶ�����һλ, �ƶ����������, + �´β��ҿ�������, + ͬʱ��ֹ����ͬһ��SLOT��������Ƶ���а��������, ָ��Ƶ�������ƶ�, ������������. + + Ŀ�����ĸ��������ݰ���, ˭�;����ڵ�һλ. +*/ +if((a_index != phashstream[hash_index]) && (a_index->stream.hash_not_head_times++ > 5)){ + if(a_index->phashnext){ + a_index->phashnext->phashprev = a_index->phashprev; + } + if(a_index->phashprev){ + a_index->phashprev->phashnext = a_index->phashnext; + } + + phashstream[hash_index]->phashprev=a_index; + a_index->phashnext=phashstream[hash_index]; + phashstream[hash_index] = a_index; + a_index->phashprev = NULL; + a_index->stream.hash_not_head_times = 0; +} + + pstream_pr = &(a_index->stream); + a_stream=&(pstream_pr->stream_public); +#if 0 /* 2014-12-18 */ + if(a_stream->dirreverse==1) +#else + if(pstream_pr->stream_dir != 1) +#endif + { + if(ptmp->curdir==DIR_C2S) + a_stream->curdir=DIR_S2C; + else + a_stream->curdir=DIR_C2S; + } + else + { + a_stream->curdir=ptmp->curdir; + } + a_stream->routedir=ptmp->routedir; + + return a_index; + } + else + { + ptmp->hash_index=hash_index; + } + + return NULL; +} + + +void hash_add_stream(struct streamindex *pindex) +{ + struct streamindex **phashstream; + struct streaminfo_private *pstream_pr=&(pindex->stream); + struct streaminfo *ptmp = &pstream_pr->stream_public; + unsigned short *pelementcount; + UCHAR threadnum = ptmp->threadnum; + unsigned int hash_index = ptmp->hash_index; + + if (STREAM_TYPE_TCP==ptmp->type) + { + phashstream=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_table; + pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_talbe_hash_count; + g_SysInputInfo[threadnum][SYS_TCP_LINK_NEW]++; + } + else if(STREAM_TYPE_UDP==ptmp->type) + { + phashstream=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_table; + pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_talbe_hash_count; + g_SysInputInfo[threadnum][SYS_UDP_LINK_NEW]++; + } else{ + phashstream = NULL; + pelementcount = 0; + assert(0); + } + + +#if 1 /* ����HASH-Slot��һ��λ�� */ + if(phashstream[hash_index]!=NULL) + phashstream[hash_index]->phashprev=pindex; + pindex->phashnext=phashstream[hash_index]; + phashstream[hash_index] = pindex; +#else + //adjust by lqy 20120803 �ڲ��ҹ������Ѿ�ȷ���˲���λ�� + if(pindex->phashprev==NULL) + { + phashstream[pindex->stream.stream_public.hash_index] = pindex; + } + else + { + pindex->phashprev->phashnext=pindex; + } + if(pindex->phashnext!=NULL) + pindex->phashnext->phashprev=pindex; +#endif + pelementcount[hash_index]++; + + +#if 0 +static int listnum = 0; +if(pelementcount[pindex->hash_index] >= listnum){ + //printf("%s, ", printaddr(&pindex->stream.addr, pindex->stream.threadnum)); + printf("########### index:%d, list-num:%d\n", pindex->hash_index, pelementcount[pindex->hash_index]); + listnum++; +} +#endif + /* + + ptmindex=phashstream[pindex->hash_index] ; + for(i=0;i<pelementcount[pindex->hash_index];i++) + { + assert(ptmindex!=NULL); + if(ptmindex->hash_index !=pindex->hash_index) + assert(0); + ptmindex=ptmindex->phashnext; + } + */ +} + + +void hash_del_stream(struct streamindex *pindex) +{ + struct streamindex **phashstream; + struct streaminfo_private *pstream_pr=&(pindex->stream); + struct streaminfo *ptmp = &pstream_pr->stream_public; + unsigned short *pelementcount; + UCHAR threadnum = ptmp->threadnum; + + if(STREAM_TYPE_UDP==ptmp->type) + { + phashstream=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_table; + pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->udp_stream_talbe_hash_count; + g_SysInputInfo[threadnum][SYS_UDP_LINK_DEL]++; + } + else if (STREAM_TYPE_TCP==ptmp->type) + { + phashstream=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_table; + pelementcount=G_MESA_GLOBAL_STREAM[threadnum]->tcp_stream_talbe_hash_count; + g_SysInputInfo[threadnum][SYS_TCP_LINK_DEL]++; + if(ptmp->ptcpdetail->clientpktnum+ptmp->ptcpdetail->serverpktnum >= 3 + && ptmp->ptcpdetail->clientbytes+ptmp->ptcpdetail->serverbytes >= 5) + { + if(ptmp->dir == DIR_DOUBLE) + { + g_SysInputInfo[threadnum][LINK_DOUBLE]++; + } + else if(ptmp->dir == DIR_C2S) + { + g_SysInputInfo[threadnum][LINK_SINGLE_C2S]++; + } + else if(ptmp->dir == DIR_S2C) + { + g_SysInputInfo[threadnum][LINK_SINGLE_S2C]++; + } + else + ; + } + }else{ + phashstream = NULL; + pelementcount = 0; + assert(0); + } + + if(pindex->phashprev!=NULL) + { + pindex->phashprev->phashnext=pindex->phashnext; + } + else + { + phashstream[ptmp->hash_index] = pindex->phashnext; + } + if(pindex->phashnext!=NULL) + { + pindex->phashnext->phashprev=pindex->phashprev; + } + + pindex->phashnext=pindex->phashprev=NULL; + + pelementcount[ptmp->hash_index]--; + /* + //for test; + ptmindex=phashstream[pindex->hash_index] ; + for(i=0;i<pelementcount[pindex->hash_index];i++) + { + assert(ptmindex!=NULL); + if(ptmindex->hash_index !=pindex->hash_index) + assert(0); + ptmindex=ptmindex->phashnext; + } + */ + + /* 2014-10-15 lijia modify, �˴�free��, �����ȡ������ַ, �ƶ���tcp_free_stream()������� */ + //free_heap_stream_info(pindex->stream.pfather); +} + +#endif + +void freeall_stream(int threadcount) +{ + struct global_stream *g_stream; + struct stream_list *plist; + struct streamindex *pindex; + int i,j; + for( i = 0; i < threadcount; i++) + { + g_stream=G_MESA_GLOBAL_STREAM[i]; + for(j=0;j<MAX_TCP_STATE;j++) + { + plist=&(g_stream->tcpList[j]); + while(plist->cnt>0) + { + pindex = plist->head; + del_stream(pindex); + } + } + for(j=0;j<MAX_UDP_STATE;j++) + { + plist=&(g_stream->udpList[j]); + while(plist->cnt>0) + { + pindex = plist->head; + del_stream(pindex); + } + } + + } + + sleep(1); + printf("All stream has been freed!\n"); +//_exit(0); +} + + + +int show_stream_state(int threadcount) +{ + int i=0; + int j=0; + + printf("\n"); + printf("\n\n-------------------------------tcp stream state---------------------------------------"); + for(i=0;i<MAX_TCP_STATE;i++) + { + switch(i) + { + case TCP_SYN_STATE: + printf("\ntcp_syn:\t"); + break; + case TCP_DATA_STATE: + printf("\ntcp_data:\t"); + break; + case TCP_NOUSE_STATE: + printf("\ntcp_nouse:\t"); + break; + default: + break; + } + + for(j=0;j<threadcount;j++) + { + printf("%d\t",G_MESA_GLOBAL_STREAM[j]->tcpList[i].cnt); + } + + } + + printf("\n\n-------------------------------udp stream state---------------------------------------"); + for(i=0;i<MAX_UDP_STATE;i++) + { + switch(i) + { + case UDP_ONE_STATE: + printf("\nudp one state:\t"); + break; + case UDP_TWO_STATE: + printf("\nudp two state:\t"); + break; + case UDP_MORE_STATE: + printf("\nudp more state:\t"); + break; + default: + break; + } + + for(j=0;j<threadcount;j++) + { + printf("%d\t",G_MESA_GLOBAL_STREAM[j]->udpList[i].cnt); + } + } + + printf("\n"); + + return 0; + +} + +#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x)) + + + +int addr_dir_relation(void *addr, int addrtype) +{ + int relation = 0; + switch(addrtype){ + case __ADDR_TYPE_IP_PAIR_V4: + { + struct layer_addr_ipv4 *ip4_addr = (struct layer_addr_ipv4 *)addr; + relation = memcmp(&ip4_addr->saddr, &ip4_addr->daddr, sizeof(int)); + } + break; + + case __ADDR_TYPE_IP_PAIR_V6: + { + struct layer_addr_ipv6 *ip6_addr = (struct layer_addr_ipv6 *)addr; + relation = memcmp(ip6_addr->saddr, ip6_addr->daddr, 16); + } + break; + + case ADDR_TYPE_TCP: + case ADDR_TYPE_UDP: + { + struct layer_addr_tcp *tcp_addr = (struct layer_addr_tcp *)addr; + relation = memcmp(&tcp_addr->source, &tcp_addr->dest, sizeof(short)); + } + break; + + default: + /* ������ַ��������������, Ĭ��Ϊ0 */ + relation = 0; + break; + } + + return relation; +} + +/* ��addr��ַ���� */ +void reverse_addr(void *addr, int addrtype) +{ + UINT16 tmp_ushort; + UINT32 tmp_uint; + + switch(addrtype){ + case ADDR_TYPE_IPV4: + { + struct layer_addr_ipv4 *ip4_addr = (struct layer_addr_ipv4 *)addr; + tmp_uint = ip4_addr->saddr; + ip4_addr->saddr = ip4_addr->daddr; + ip4_addr->daddr = tmp_uint; + tmp_ushort = ip4_addr->source; + ip4_addr->source = ip4_addr->dest; + ip4_addr->dest = tmp_ushort; + } + break; + + case __ADDR_TYPE_IP_PAIR_V4: + { + struct layer_addr_ipv4 *ip4_pure_addr = (struct layer_addr_ipv4 *)addr; + tmp_uint = ip4_pure_addr->saddr; + ip4_pure_addr->saddr = ip4_pure_addr->daddr; + ip4_pure_addr->daddr = tmp_uint; + } + break; + + case ADDR_TYPE_IPV6: + { + struct layer_addr_ipv6 *ip6_addr = (struct layer_addr_ipv6 *)addr; + UCHAR tmp[16]; + memcpy(tmp, ip6_addr->saddr, IPV6_ADDR_LEN); + memcpy(ip6_addr->saddr, ip6_addr->daddr, IPV6_ADDR_LEN); + memcpy(ip6_addr->daddr, tmp, IPV6_ADDR_LEN); + tmp_ushort = ip6_addr->source; + ip6_addr->source = ip6_addr->dest; + ip6_addr->dest = tmp_ushort; + } + break; + + case __ADDR_TYPE_IP_PAIR_V6: + { + struct layer_addr_ipv6 *ip6_pure_addr = (struct layer_addr_ipv6 *)addr; + UCHAR tmp[16]; + memcpy(tmp, ip6_pure_addr->saddr, IPV6_ADDR_LEN); + memcpy(ip6_pure_addr->saddr, ip6_pure_addr->daddr, IPV6_ADDR_LEN); + memcpy(ip6_pure_addr->daddr, tmp, IPV6_ADDR_LEN); + } + break; + + case ADDR_TYPE_TCP: + case ADDR_TYPE_UDP: + { + struct layer_addr_tcp *tcp_addr = (struct layer_addr_tcp *)addr; + tmp_ushort = tcp_addr->source; + tcp_addr->source = tcp_addr->dest; + tcp_addr->dest = tmp_ushort; + } + break; + + /* TODO:PPTP-addr */ + + default: + /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת�� */ + return; + } + + return; +} + +/* �����ַcopy */ +void addr_reverse_memcpy(void *daddr, const void *saddr, int addrtype, int addrlen) +{ + switch(addrtype){ + case ADDR_TYPE_IPV4: + { + const struct layer_addr_ipv4 *ip4_saddr = (const struct layer_addr_ipv4 *)saddr; + struct layer_addr_ipv4 *ip4_daddr = (struct layer_addr_ipv4 *)daddr; + ip4_daddr->saddr = ip4_saddr->daddr; + ip4_daddr->daddr = ip4_saddr->saddr; + ip4_daddr->source = ip4_saddr->dest; + ip4_daddr->dest = ip4_saddr->source; + } + break; + + case ADDR_TYPE_IPV6: + { + const struct layer_addr_ipv6 *ip6_saddr = (const struct layer_addr_ipv6 *)saddr; + struct layer_addr_ipv6 *ip6_daddr = (struct layer_addr_ipv6 *)daddr; + memcpy(ip6_daddr->saddr, ip6_saddr->daddr, IPV6_ADDR_LEN); + memcpy(ip6_daddr->daddr, ip6_saddr->saddr, IPV6_ADDR_LEN); + ip6_daddr->source = ip6_saddr->dest; + ip6_daddr->dest = ip6_saddr->source; + } + break; + + case ADDR_TYPE_MAC: /* 2015-06-15 lijia add, ľ������豸���� */ + { + const struct layer_addr_mac *mac_saddr = (const struct layer_addr_mac *)saddr; + struct layer_addr_mac *mac_daddr = (struct layer_addr_mac *)daddr; + memcpy(mac_daddr->src_mac, mac_saddr->dst_mac, MAC_ADDR_LEN); + memcpy(mac_daddr->dst_mac, mac_saddr->src_mac, MAC_ADDR_LEN); + } + break; + + case __ADDR_TYPE_IP_PAIR_V4: /* ��IPv4��, ͨ���ǻ�������ģʽ */ + { + const struct layer_addr_ipv4 *ip4_saddr = (const struct layer_addr_ipv4 *)saddr; + struct layer_addr_ipv4 *ip4_daddr = (struct layer_addr_ipv4 *)daddr; + ip4_daddr->saddr = ip4_saddr->daddr; + ip4_daddr->daddr = ip4_saddr->saddr; /* ֻ��IP��ַ */ + /* Note: ��IP��Ķ˿�ֵһ��Ҫ��Ϊ0, �����ʹ��δ��ʼ�����ڴ�, �ں�������ԭ��cmpaddr()������, ���ܻᵼ�����Ƚϵ�ַ��һ�����Ҳ��� */ + ip4_daddr->source = 0; + ip4_daddr->dest = 0; + } + break; + + case __ADDR_TYPE_IP_PAIR_V6: /* ��IPv6��, ͨ���ǻ�������ģʽ */ + { + const struct layer_addr_ipv6 *ip6_saddr = (const struct layer_addr_ipv6 *)saddr; + struct layer_addr_ipv6 *ip6_daddr = (struct layer_addr_ipv6 *)daddr; + memcpy(ip6_daddr->saddr, ip6_saddr->daddr, IPV6_ADDR_LEN); + memcpy(ip6_daddr->daddr, ip6_saddr->saddr, IPV6_ADDR_LEN); + /* Note: ��IP��Ķ˿�ֵһ��Ҫ��Ϊ0, �����ʹ��δ��ʼ�����ڴ�, �ں�������ԭ��cmpaddr()������, ���ܻᵼ�����Ƚϵ�ַ��һ�����Ҳ��� */ + ip6_daddr->source = 0; + ip6_daddr->dest = 0; + } + break; + + /* TODO:PPTP-addr */ + + default: + /* ������ַ����, ��VLAN, L2TP��, ����������,ֱ��copy */ + memcpy(daddr, saddr, addrlen); + return; + } + + return; +} + +/* + ��ΪԤ�ȷ�����һЩstreamindex�ṹ, ÿ�δ�����ʱ, ��Ԥ�ȷ�����ڴ��л�ȡ, + ��malloc_and_copy_streamindex()����, ֻcopy streamindex�ṹ, û��addr.paddr, + ��Ҫ����ַ��ϢҲcopy������. +*/ +int copy_ipport_union_addr(struct streaminfo *pstream_heap, struct streaminfo *pstream_stack, int reverse) +{ + pstream_heap->addr.paddr = dictator_malloc(pstream_stack->threadnum, pstream_stack->addr.addrlen); + if(0 == reverse){ + memcpy(pstream_heap->addr.paddr, pstream_stack->addr.paddr, pstream_stack->addr.addrlen); + }else{ + addr_reverse_memcpy(pstream_heap->addr.paddr, pstream_stack->addr.paddr, pstream_stack->addr.addrtype, pstream_stack->addr.addrlen); + } + + return 0; +} + +#if 0 +void reverse_addr_new(void *addr, int addrtype) +{ + UINT16 tmp_ushort; + UINT32 tmp_uint; + + switch(addrtype){ + case ADDR_TYPE_IPV4: + //case __ADDR_TYPE_IP_PAIR_V4: + { + struct layer_addr_ipv4 *ip4_addr = (struct layer_addr_ipv4 *)addr; + tmp_uint = ip4_addr->saddr; + ip4_addr->saddr = ip4_addr->daddr; + ip4_addr->daddr = tmp_uint; + tmp_ushort = ip4_addr->source; + ip4_addr->source = ip4_addr->dest; + ip4_addr->dest = tmp_ushort; + } + break; + + case ADDR_TYPE_IPV6: + //case __ADDR_TYPE_IP_PAIR_V6: + { + struct layer_addr_ipv6 *ip6_addr = (struct layer_addr_ipv6 *)addr; + UCHAR tmp[16]; + memcpy(tmp, ip6_addr->saddr, IPV6_ADDR_LEN); + memcpy(ip6_addr->saddr, ip6_addr->daddr, IPV6_ADDR_LEN); + memcpy(ip6_addr->daddr, tmp, IPV6_ADDR_LEN); + tmp_ushort = ip6_addr->source; + ip6_addr->source = ip6_addr->dest; + ip6_addr->dest = tmp_ushort; + } + break; + + case ADDR_TYPE_TCP: + case ADDR_TYPE_UDP: + { + struct layer_addr_tcp *tcp_addr = (struct layer_addr_tcp *)addr; + tmp_ushort = tcp_addr->source; + tcp_addr->source = tcp_addr->dest; + tcp_addr->dest = tmp_ushort; + } + break; + + case ADDR_TYPE_MAC: /* 2015-06-15 lijia add, ľ������豸���� */ + { + struct layer_addr_mac *mac_addr = (struct layer_addr_mac *)addr; + UCHAR tmp_mac[MAC_ADDR_LEN]; + memcpy(tmp_mac, mac_addr->src_mac, MAC_ADDR_LEN); + memcpy(mac_addr->src_mac, mac_addr->dst_mac, MAC_ADDR_LEN); + memcpy(mac_addr->dst_mac, tmp_mac, MAC_ADDR_LEN); + } + break; + + default: + /* ������ַ����, ��VLAN, L2TP��, ����������, ����ת�� */ + return; + } + + return; +} +#endif + +void free_thread_stream(int thread_seq) +{ + struct global_stream *g_stream; + struct stream_list *plist; + struct streamindex *pindex; + int j; + + g_stream=G_MESA_GLOBAL_STREAM[thread_seq]; + for(j=0;j<MAX_TCP_STATE;j++) + { + plist=&(g_stream->tcpList[j]); + while(plist->cnt>0) + { + pindex = plist->head; + del_stream(pindex); + } + } + for(j=0;j<MAX_UDP_STATE;j++) + { + plist=&(g_stream->udpList[j]); + while(plist->cnt>0) + { + pindex = plist->head; + del_stream(pindex); + } + } + +#if HASH_COLLIDE_CALC + printf("================ hash-collide-statistics =================\n"); + + for(j = 0; j < HASH_COLLIDE_LIST_NUM_MAX; j++){ + printf("hash-collide-list-num:%d, \t times:%d\n", j, __hash_colliede_num[j]); + } + printf("================ hash-collide-statistics =================\n"); +#endif + +} + +/* 2014-04-08 LiJia add */ +void free_heap_stream_info(struct streaminfo *heap_stream, int layer) +{ + struct streaminfo *pfather; + struct layer_addr *addr; + unsigned char threadnum; + + if(NULL == heap_stream){ + return; + } + threadnum = heap_stream->threadnum; + + pfather = heap_stream->pfather; + addr = &heap_stream->addr; + + /* Ϊ�˽�ʡ�ռ�, ��ַ����ADDR_TYPE_IPV4/6ʵ�ʴ洢����һ���IP��, ����free */ + if(addr->paddr){ + dictator_free(threadnum, addr->paddr); + addr->paddr = NULL; + } + + if(layer != 0){ + /* ���ṹ�����ڲ㼴��0��, ʵ����ͨ��malloc_and_copy_streamindex()��ȡ��Ԥ�ȷ�����ڴ�, + ����free, + pfatherָ���streaminfo�ṹ����ͨ��copy_stream_info_to_heap()��̬malloc��. + */ +#if SAPP_INSECTICIDE + /* 2018-09-20 lijia close! �˴��ر����stream, ��Ϊ�����ܶ�ط����м�����op_state��ֵ, ���ó�0xFE�ᵼ��Ī����������� */ + //memset(heap_stream, 0xFE, sizeof(struct streaminfo)); +#endif + dictator_free(threadnum, heap_stream); + } + return free_heap_stream_info(pfather, layer+1); +} + +/* 2014-12-03 lijia add for sysinfo.log + args: + action: 1:add; -1:del; +*/ +static inline void update_stream_status(UCHAR threadnum, UCHAR stream_type, UCHAR stream_state, int action) +{ + if(STREAM_TYPE_TCP == stream_type){ + switch(stream_state) + { + case TCP_DATA_STATE: + g_SysInputInfo[threadnum][SYS_TCP_LINK_DATA] += action; + break; + case TCP_NOUSE_STATE: + g_SysInputInfo[threadnum][SYS_TCP_LINK_NOUSE] += action; + break; + case TCP_SYN_STATE: + g_SysInputInfo[threadnum][SYS_TCP_LINK_SYN] += action; + break; + } + }else if(STREAM_TYPE_UDP == stream_type){ + switch(stream_state) + { + case UDP_TWO_STATE: + g_SysInputInfo[threadnum][SYS_UDP_LINK_TWO] += action; + break; + case UDP_MORE_STATE: + g_SysInputInfo[threadnum][SYS_UDP_LINK_MORE] += action; + break; + case UDP_ONE_STATE: + g_SysInputInfo[threadnum][SYS_UDP_LINK_ONE] += action; + break; + } + } +} + + +/* + TODO: + ���ں�tilera�ܹ��µ��ڴ�����Ϊlocal��sharedģʽ, + ԭ�������߳̽�����ȫ���ڴ�һ�����������ģʽ������, + ��Ϊÿ�������߳��Լ���ʼ��. +*/ +int thread_self_init(int thread_seq) +{ + return 0; +} + + + +#if DEBUG +#include "iknow.h" +typedef struct{ + packet_io_status_base_t io_base_status; + unsigned long long total_search_times; /* ��HASH������, �������˶��ٴ�findstreamindex() */ + unsigned long long total_hash_list_num; /* ��HASH��ͻ��������, (total_hash_list_num/total_search_times)��hashƽ�����ҳ��� */ + unsigned long long max_hash_list_num; /* ����ͻ������ */ +}packet_io_status_stream_hash_t; + +extern packet_io_status_t *g_packet_io_status_pool[MAX_THREAD_NUM]; + +static packet_io_status_stream_hash_t g_packet_io_status_stream_hash; + +void *packet_io_status_stream_hash_mem_init(void) +{ + void *stream_hash_mem; + + stream_hash_mem = malloc(sizeof(packet_io_status_stream_hash_t)); + memset(stream_hash_mem, 0, sizeof(packet_io_status_stream_hash_t)); + + return stream_hash_mem; +} + + +void packet_io_status_stream_hash_time_cb(void) +{ + int i; + unsigned long long tmp_max_hlist_num = 0; + packet_io_status_stream_hash_t *psh; + + g_packet_io_status_stream_hash.total_search_times = 0; + g_packet_io_status_stream_hash.total_hash_list_num = 0; + for(i = 0; i < g_packet_io_thread_num; i++){ + psh = (packet_io_status_stream_hash_t *)(g_packet_io_status_pool[i]->packet_io_status[IO_STATUS_STREAM_HASH]); + g_packet_io_status_stream_hash.total_search_times += psh->total_search_times; + g_packet_io_status_stream_hash.total_hash_list_num += psh->total_hash_list_num; + if(psh->max_hash_list_num > tmp_max_hlist_num){ + tmp_max_hlist_num = psh->max_hash_list_num; + } + } + g_packet_io_status_stream_hash.max_hash_list_num = tmp_max_hlist_num; +} + +static long ifuncb_show_stream_hash_summary(iknow_handle ihandle, iknow_conn iconn, const void *cb_fun_arg, const char *cmd_args) +{ + + iprintf(ihandle, iconn, "stream hash summary:\ntotal-hash-search-num:%llu\ntotal-hash-list-num:%llu\nmax-hash-list-num:%llu\navg-hlist-num:%.2f", + g_packet_io_status_stream_hash.total_search_times, + g_packet_io_status_stream_hash.total_hash_list_num, + g_packet_io_status_stream_hash.max_hash_list_num, + (float)g_packet_io_status_stream_hash.total_hash_list_num/(float)g_packet_io_status_stream_hash.total_search_times); + + return 0; +} + +static long ifuncb_show_stream_hash_detail(iknow_handle ihandle, iknow_conn iconn, const void *cb_fun_arg, const char *cmd_args) +{ + int i; + packet_io_status_stream_hash_t *psh; + + iprintf(ihandle, iconn, "stream hash detail:\n%10s %15s %13s %13s %13s\n", "thread-seq", "hash-search-num", "hash-list-num", "max-hlist-num", "avg-hlist-num"); + for(i = 0; i < g_packet_io_thread_num; i++){ + psh = (packet_io_status_stream_hash_t *)(g_packet_io_status_pool[i]->packet_io_status[IO_STATUS_STREAM_HASH]); + iprintf(ihandle, iconn, "%10d %15llu %13llu %13llu %13.2f\n", + i, psh->total_search_times, psh->total_hash_list_num, psh->max_hash_list_num, + (float)psh->total_hash_list_num/(float)psh->total_search_times); + } + + + return 0; +} + +void iknow_register_stream_hash_cb(void *ihandle) +{ + int ret; + + ret = iknow_register_cmd(ihandle, "show stream summary", "display stream hash summary", ifuncb_show_stream_hash_summary, NULL, 0, NULL); + assert(0 == ret); + ret = iknow_register_cmd(ihandle, "show stream detail", "display stream hash detail", ifuncb_show_stream_hash_detail, NULL, 0, NULL); + assert(0 == ret); +} + +void packet_io_status_stream_hash_update(int thread_seq, unsigned long hash_list_num) +{ + packet_io_status_stream_hash_t *psh; + + psh = (packet_io_status_stream_hash_t *)g_packet_io_status_pool[thread_seq]->packet_io_status[IO_STATUS_STREAM_HASH]; + + psh->total_search_times++; + psh->total_hash_list_num += hash_list_num; + + if(unlikely(hash_list_num > psh->max_hash_list_num)){ + psh->max_hash_list_num = hash_list_num; + } +} + +#endif + + + +int get_stream_carry_tunnel_type(const struct streaminfo *this_stream, + const struct streaminfo *upper_stream, unsigned short *tunnel_type) +{ + if(NULL == this_stream){ + return -1; + } + /* ������ָ��, ע��: sapp_online���sapp_ip_port_union�治һ�� */ + const struct streaminfo *pfather = this_stream->pfather; + + switch(this_stream->addr.addrtype){ + case ADDR_TYPE_IPV4: + pfather = this_stream->pfather; /* IP-TCP/UDP����Ϊ��һ������, ����pfatherָ�� */ + if(pfather != NULL){ + if(__ADDR_TYPE_IP_PAIR_V4 == pfather->addr.addrtype){ + *tunnel_type = (unsigned short)STREAM_TUNNLE_IP_IN_IP; + }else if(__ADDR_TYPE_IP_PAIR_V6 == pfather->addr.addrtype){ + *tunnel_type = (unsigned short)STREAM_TUNNLE_4OVER6; + }else if(ADDR_TYPE_GRE == pfather->addr.addrtype){ + *tunnel_type = (unsigned short)STREAM_TUNNLE_GRE; + } + } + break; + + case ADDR_TYPE_IPV6: + pfather = this_stream->pfather; /* IP-TCP/UDP����Ϊ��һ������, ����pfatherָ�� */ + if(pfather != NULL){ + if(__ADDR_TYPE_IP_PAIR_V4 == pfather->addr.addrtype){ + *tunnel_type = (unsigned short)STREAM_TUNNLE_6OVER4; + }else if((ADDR_TYPE_IPV4 == pfather->addr.addrtype) + && (STREAM_TYPE_UDP == pfather->type)){ + *tunnel_type = (unsigned short)STREAM_TUNNLE_TEREDO; + }else if(ADDR_TYPE_GRE == pfather->addr.addrtype){ + *tunnel_type = (unsigned short)STREAM_TUNNLE_GRE; + } + } + break; + + case ADDR_TYPE_GRE: + if((ADDR_TYPE_IPV4 == upper_stream->addr.addrtype) + ||((ADDR_TYPE_IPV6 == upper_stream->addr.addrtype)) + ||(__ADDR_TYPE_IP_PAIR_V4 == upper_stream->addr.addrtype) + ||(__ADDR_TYPE_IP_PAIR_V6 == upper_stream->addr.addrtype)){ + *tunnel_type = (unsigned short)STREAM_TUNNLE_GRE; + } + break; + + case ADDR_TYPE_PPTP: + /* pptp_entry()�������Ѿ�����ַ����ΪADDR_TYPE_PPTP, �϶�����PPTP���� */ + *tunnel_type = (unsigned short)STREAM_TUNNLE_PPTP; + break; + + case ADDR_TYPE_L2TP: + /* l2tp_entry()�������Ѿ�����ַ����ΪADDR_TYPE_L2TP, �϶�����L2TP���� */ + *tunnel_type = (unsigned short)STREAM_TUNNLE_L2TP; + break; + + } + + return get_stream_carry_tunnel_type(pfather, this_stream, tunnel_type); +} + +#ifdef __cplusplus +} +#endif + diff --git a/entry/sapp_init.c b/entry/sapp_init.c index 66d371d..0e017eb 100644 --- a/entry/sapp_init.c +++ b/entry/sapp_init.c @@ -1,392 +1,393 @@ -#include <stdio.h>
-#include <string.h>
-#include <pthread.h>
-#include <unistd.h>
-#include <arpa/inet.h>
-#include <MESA/MESA_handle_logger.h>
-#include "MESA/MESA_prof_load.h"
-#include "dealpkt.h"
-#include "deal_ipv4.h"
-#include "deal_ipv6.h"
-#include "packet_io.h"
-#include "project_requirement.h"
-#include "stream_internal.h"
-#include "mesa_net.h"
-#include "dictator.h"
-#include <sysinfo.h>
-#include <assert.h>
-#include <errno.h>
-
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-static int threadnum = 1;
-static volatile long stream_num = 0;
-int g_iThreadNum = 1;
-time_t g_CurrentTime;//add by lqy 20070606
-time_t g_sapp_start_time;
-volatile unsigned long long g_SysInputInfo[MAX_THREAD_NUM][COUNTER_NUM];
-volatile unsigned long long sysinfo_new[COUNTER_NUM] = {0};
-extern int sapp_args_v;
-int ipv4_frag_init(int thread_count, unsigned int hash_size);
-int ipv6_frag_init(int thread_count, unsigned hash_size);
-int project_requirement_global_init(void);
-int packet_io_set_cap_level(int cap_level);
-int plugctrl_proc(void);
-int runtime_log_init(char * file, int level);
-int iknow_info_init(void);
-extern void sesame_open_door(const char *lock_path, const char *lock_name);
-/* (0:pag,1:pcap,2:dumpfile,3:pfring,4:DPDK,5:ppf,6:NPacket,7:qnf,8:N95,9:pcap-dumpfile-list, 10:topsec) */
-int cap_mode = 2;
-extern int G_DICTATOR_SW;
-extern struct global_stream **G_MESA_GLOBAL_STREAM;
-extern int G_SKIP_NOT_IP_LAYER; /* ������IPЭ��IJ�, ����˫ջ�����������, RST������ */
-//static int __times = 0;
-int g_timestamp_record_sw = 0;
-extern long long g_timedelay_threshold;
-extern int g_use_MESA_sleep_sw;
-extern int g_raw_pkt_broken_check;
-extern int g_packet_io_cap_mode;
-extern int g_skip_ethernet_layer_sw;
-extern int g_discard_ack_sw;
-extern int g_kill_tcp_remedy_sw;
-extern int g_analyse_tcp_opt_sw;
-extern int g_encapsulate_with_ddp;
-extern int G_DICTATOR_SW;
-extern int g_PollingFunNum;
-extern int packet_io_set_ipv6_raw_socket_enable(int op_switch);
-extern const char *stream_addr_list_ntop_outward(const struct streaminfo *pstream);
-extern const char *stream_addr_list_ntop_inward(const struct streaminfo *pstream);
-extern const struct streaminfo *stream_addr_list_pton_outward(const char *addr_list_str, int thread_index);
-extern const struct streaminfo *stream_addr_list_pton_inward(const char *addr_list_str, int thread_index);
-extern int cycle_pkt_dump_init(int argc, char *argv[]);
-extern void udp_set_stream_timeout(unsigned short timeout);
-extern char udp_teredo_identify_entry(const struct streaminfo *pstream, const void *this_hdr, const void *raw_pkt, void **pme);
-extern char isakmp_protocol_entry(const struct streaminfo *a_udp, void **pme, int thread_seq,const void *ip_hdr);
-extern int stream_register_udp_raw(SAPP_STREAM_FUN_T x);
-extern int number_is_2powerN(uint n);
-extern int symbol_check(void);
-extern int timestamp_record_init(void);
-extern void sapp_global_val_check(void);
-extern int sapp_global_val_init(void);
-
-static void forbid_call_exit_in_running_state(void)
-{
- if(g_packet_io_cap_mode != CAP_MODEL_PCAP_DUMPFILE){
- printf("\033[41mCall exit() in running state is forbidden!!\033[0m\n");
- abort();
- }
-}
-
-/* 2016-09-22 lijia add, dictator�ڵ�һ�α�����ʱ�г�ʼ������, ��ֹ���߳������ͻ, ƽ̨�ȵ���һ�� */
-static void sapp_dictator_init(void)
-{
-#if USE_MEMPOOL
- void *tmp;
- if(G_DICTATOR_SW){
- tmp = __wrap_malloc(1);
- assert(tmp != NULL);
- __wrap_free(tmp);
-
- tmp = malloc(1);
- assert(tmp != NULL);
- free(tmp);
- }else{
- printf("\033[33m[Warning]dictator is not enable, please check 'conf/main.conf -> dictator_switch'.\033[0m\n");
- }
-#else
- printf("\033[33m[Warning]sapp is compiled without dictator.\033[0m\n");
-#endif
-}
-
-int MESA_platform_init(int argc, char *argv[])
-{
- int top_mode, queue_max_num, ipv6_switch = 0;
- char pcap_dev1[128], pcap_dev2[128], snd_dev[128];
- char cap_filter[1024] = {};
- char gateway_mac[32] = {};
- int max_udp_stream_num, max_tcp_stream_num, create_link_mode;
- int TcpAllEnable = 0;
- //pthread_t gettimeThread;
- int maxrandval, randkeyval;
- int load_plug_sw = 1;
- unsigned short udp_stream_timeout = 300, max_unorder_num;
- unsigned short link_timeout = 600;
- int int_tmp;
-
- g_CurrentTime = time(NULL);
- g_sapp_start_time = g_CurrentTime;
-
- /* 2016-09-22 lijia add, dictator�ڵ�һ�α�����ʱ�г�ʼ������, ��ֹ���߳������ͻ, ƽ̨�ȵ���һ��, ��Ҫ��ȡ�����ļ����� */
- MESA_load_profile_int_def("conf/main.conf","Module", "dictator_switch", &G_DICTATOR_SW, 1);
- sapp_dictator_init();
-
- MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"ShowStatInfo", (char *)"platform_log_level", &g_sapp_log_level, 10);
- g_sapp_log_handle = MESA_create_runtime_log_handle("./log/runtimelog", g_sapp_log_level);
- if(NULL == g_sapp_log_handle)
- {
- printf("MESA_create_runtime_log_handle() error!\n");
- return -1;
- }
-
- if(MESA_load_profile_int_def("conf/main.conf", "Module", "capdatamodlel", &cap_mode, 2) < 0){
- printf("read conf 'capdatamodlel' error!\n");
- exit(1);
- }
-
- if(MESA_load_profile_int_def("conf/main.conf", "Module", "threadnum", &threadnum, 1) < 0){
- printf("read conf 'threadnum' error!\n");
- exit(1);
- }
- g_iThreadNum = threadnum;
-
- MESA_load_profile_int_def("conf/main.conf","Module", "MaxUdpStreams",&max_udp_stream_num , 65536);
-#if HIGH_PERF
- if(number_is_2powerN(max_udp_stream_num) == 0){
- printf("\033[41m '%s' must be power of 2! such as 32768,65536,131072...\033[0m\n", "MaxUdpStreams");
- exit(1);
- }
-#endif
-
- MESA_load_profile_int_def("conf/main.conf","Module", "MaxTcpStreams",&max_tcp_stream_num, 65536);
-#if HIGH_PERF
- if(number_is_2powerN(max_udp_stream_num) == 0){
- printf("\033[41m '%s' must be power of 2! such as 32768,65536,131072...\033[0m\n", "MaxTcpStreams");
- exit(1);
- }
-#endif
-
- MESA_load_profile_int_def("conf/main.conf","Module", "CreatLinkMode", &create_link_mode, TCP_CTEAT_LINK_BYSYN);
- MESA_load_profile_int_def("conf/main.conf","Module", "TcpAllEnable",&TcpAllEnable, 0);
- tcp_set_support_tcpall_model(TcpAllEnable);
-
- MESA_load_profile_int_def("conf/main.conf","Module", "discard_ack",&g_discard_ack_sw, 0);
- MESA_load_profile_int_def("conf/main.conf","Module", "kill_tcp_remedy",&g_kill_tcp_remedy_sw, 0);
-
- MESA_load_profile_int_def("conf/main.conf","Module", "maxrandval", &maxrandval, 65535);
- set_rst_max_rnd_val(maxrandval);
- MESA_load_profile_int_def("conf/main.conf","Module", "randkeyval", &randkeyval, 13);
- set_rst_rand_key(randkeyval);
-
- ipv4_frag_init(threadnum, 1024 * 512);
- MESA_load_profile_int_def("conf/main.conf","Module", "IPv6_module_enable", &ipv6_switch, 1);
- packet_io_set_ipv6_module_enable(ipv6_switch);
- MESA_load_profile_int_def("conf/main.conf","Module", "IPv6_raw_socket", &ipv6_switch, 0); /* Ϊ�˱��ڲ���, ����Ĭ�Ϲر� */
- packet_io_set_ipv6_raw_socket_enable(ipv6_switch);
- ipv6_frag_init(threadnum, 1024 * 256);
-
- tcp_set_creatlink_model(create_link_mode);
- MESA_load_profile_short_def("conf/main.conf","Module", "MaxUnorderNum",(short *)&max_unorder_num, 5);
- tcp_set_default_unorder_num(max_unorder_num);
- tcp_set_stream_num(max_tcp_stream_num*3,max_tcp_stream_num,max_tcp_stream_num*2);
-
- MESA_load_profile_short_def("conf/main.conf","Module", "LinkTimeout", (short *)&link_timeout, 600);
- stream_set_default_nopkt_time(link_timeout);
-
- MESA_load_profile_short_def("conf/main.conf","Module", "UdpResetTime", (short *)&udp_stream_timeout, 300);
- udp_set_stream_timeout(udp_stream_timeout);
- udp_set_stream_num(max_udp_stream_num*3,max_udp_stream_num,max_udp_stream_num*2);
-
- MESA_load_profile_int_def("conf/main.conf","Module", "skip_not_ip_layer", &G_SKIP_NOT_IP_LAYER, 0);
- if(G_SKIP_NOT_IP_LAYER != 0){
- printf("\033[41mWarning! '%s' is enable!\033[0m\n", "skip_not_ip_layer");
- sleep(1);
- }
-
- MESA_load_profile_int_def("conf/main.conf","Module", "timestamp_record", &g_timestamp_record_sw, 0);
- MESA_load_profile_int_def("conf/main.conf","Module", "timedelay_threshold", &int_tmp, 10000000);
- g_timedelay_threshold = (long long)int_tmp;
-
- MESA_load_profile_int_def("conf/main.conf","Module", "use_MESAsleep", &g_use_MESA_sleep_sw, 0);
-
- MESA_load_profile_int_def("conf/main.conf","Module", "analyse_tcp_option", &g_analyse_tcp_opt_sw, 1);
-
- MESA_load_profile_int_def("conf/main.conf","Module", "encapsulate_with_ddp", &g_encapsulate_with_ddp, 0);
- if(g_encapsulate_with_ddp != 0){
- printf("\033[32m[Notice] '%s' is enable!\033[0m\n", "encapsulate_with_ddp");
- sleep(1);
- }
-
-#if (0 == SAPP_AS_TARGET_SO) /* ��Ϊ.soģʽ, ����dlopen���io.so */
- if(packet_io_lib_load(cap_mode) < 0){
- return -1;
- }
-
- packet_io_set_work_thread_num(threadnum);
-
- packet_io_set_cap_mode(cap_mode);
-
- /*
- (0:pag,1:pcap,2:dumpfile,3:pfring,4:DPDK,5:ppf,6:NPacket,7:qnf,8:N95,9:pcap-dumpfile-list, 10:topsec)
- 11:ipfile, 12:marsio4, 13:agent_smith, 14:dpdk_vxlan, 15:marsio_vxlan, 16:pag_marsio.
- */
- switch(cap_mode){
- case CAP_MODEL_PAG:
- case CAP_MODEL_PAG_MARSIO:
-#if USE_PAG_GET_FRAME
- packet_io_set_cap_level(CAP_LEVEL_MAC);
-#else
- packet_io_set_cap_level(CAP_LEVEL_IPV4);
-#endif
- break;
-
- case CAP_MODEL_PPF:
- case CAP_MODEL_TOPSEC:
- case CAP_MODEL_IPFILE:
- packet_io_set_cap_level(CAP_LEVEL_IPV4);
- break;
-
- case CAP_MODEL_PCAP_ONLINE:
- case CAP_MODEL_PCAP_DUMPFILE:
- case CAP_MODEL_AGENT_SMITH:
-#if PCAP_CAP_FROM_IP
- packet_io_set_cap_level(CAP_LEVEL_IPV4);
- break;
-#endif
-
- case CAP_MODEL_PFRING:
- case CAP_MODEL_DPDK:
- case CAP_MODEL_NPACKET:
- case CAP_MODEL_PCAP_DUMPLIST:
- case CAP_MODEL_MARSIOV4:
- case CAP_MODEL_DPDK_VXLAN:
- case CAP_MODEL_MARSIOV4_VXLAN:
- packet_io_set_cap_level(CAP_LEVEL_MAC);
- break;
- default:
- printf("Invalid cap_mode:%d\n", cap_mode);
- exit(1);
- }
-
- if(MESA_load_profile_int_def("conf/main.conf", "Module", "net_connection_mode", &top_mode, 1) < 0){
- printf("read conf 'net_connection_mode' error!\n");
- exit(1);
- }
- packet_io_set_topology_mode(top_mode);
-
- if(MESA_load_profile_string_def("conf/main.conf", "Module", "pcapdevice", pcap_dev1, 128, "eth0") < 0){
- printf("read conf 'pcapdevice' error!\n");
- exit(1);
- }
- packet_io_set_capdev_parallel(pcap_dev1);
-
- if(2 == top_mode){
- if(MESA_load_profile_string_def("conf/main.conf", "Module", "pcapdevice2", pcap_dev2, 128, "eth1") < 0){
- printf("read main.conf 'pcapdevice2' error!\n");
- exit(1);
- }
- packet_io_set_capdev_serial(pcap_dev1, pcap_dev2);
- }
-
- if(MESA_load_profile_string_def("conf/main.conf", "Module", "pcapfilter", cap_filter, 1024, "") < 0){
- printf("read conf 'pcapfilter' error!\n");
- exit(1);
- }
- if(cap_filter[0] != '\0'){
- printf("\033[33m[Warning] pcap filter is '%s'.\033[0m\n", cap_filter);
- packet_io_set_capture_filter(cap_filter);
- }
-
- if(MESA_load_profile_int_def("conf/main.conf", "Module", "queue_max_num", &queue_max_num, 2000) < 0){
- printf("read conf 'queue_max_num' error!\n");
- exit(1);
- }
-
- packet_io_set_cap_buf_queue(queue_max_num);
-
- if(MESA_load_profile_string_nodef("conf/main.conf", "Module", "gateway_mac", gateway_mac, 32) < 0){
- printf("read conf 'gateway_mac' error!\n");
- }
- packet_io_set_gateway_mac(gateway_mac); /* TODO, ��ȡ�����ļ� */
-
- if(MESA_load_profile_string_nodef("conf/main.conf", "Module", "senddevice", snd_dev, 128) < 0){
- printf("read conf 'senddevice' error!\n");
- exit(1);
- }
- packet_io_set_send_dev(snd_dev);
-
- MESA_load_profile_int_def("conf/main.conf", "Module", "signal_take_over_switch", &sapp_global_single.signal_take_over_sw, 0);
- MESA_load_profile_int_def("conf/main.conf", "Module", "ipentry_priority_over_ipfrag", &sapp_global_single.ipentry_priority_over_ipfrag, 0);
- MESA_load_profile_int_def("conf/main.conf", "Module", "tuple4_reuse_time_interval", &sapp_global_single.tuple4_reuse_time_interval, 3);
-#endif
-
- init_stream_manage(threadnum);
-
-#if DEBUG
- iknow_info_init();
-
- timestamp_record_init();
-#endif
-
- MESA_load_profile_int_def("conf/main.conf","Module", "load_plug_switch", &load_plug_sw, 1);
- if(load_plug_sw){
- plugctrl_proc();
- }
-
- /* 2017-02-24 lijia modify,
- NOTE:
- �ȳ�ʼ�����, ����ڲ����ܻ����pthread_create��̬�����߳�,
- ������, ��DPDKģʽ��, dpdk��ʼ���ξͻ��ǰ�̵߳�ijCPU����,
- ��ô������pthread_create���߳�Ĭ�ϼ̳������Ϊ,
- ����, �ȳ�ʼ���߳��ٳ�ʼ��packet_io.
- */
- if(packet_io_init(argc, argv) < 0){
- return -1;
- }
-
-
-#if CYCLE_PKT_DUMP
- if(cycle_pkt_dump_init(argc , argv) < 0){
- return -1;
- }
-#else
- int __pkt_dump_switch = 0;
- MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_switch", &__pkt_dump_switch, 0);
- if(__pkt_dump_switch != 0){
- printf("\n\033[33m[Warning] pkt_dump enable, but the platform is compiled without debug feature.\033[0m\n");
- printf("\033[33mIf you want pkt_dump function, please recompile platform use 'make debug=0'.\033[0m\n");
- printf("\033[33mOtherwise, please close 'pkt_dump_switch', or ignore this message.\033[0m\n");
- usleep(10000);
- }
-#endif
-
- MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"Module", (char *)"raw_pkt_broken_check", &g_raw_pkt_broken_check, 0);
- MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"Module", (char *)"skip_ethernet_layer", &g_skip_ethernet_layer_sw, 0);
-
- /* ƽ̨���ù̶������IJ�� */
- stream_register_udp_raw(udp_teredo_identify_entry);
-
- sapp_assistant_init();
-
- /* NOTE: ���������һ��Ҫ��plugctrl_proc(), ���������֮�� */
- symbol_check(); /* 2016-01-13 Lijia add, ���ȫ�ַ�������ͻ */
-
- sapp_global_val_init();
-
- if(g_PollingFunNum != 0){
- printf("\n\033[33m[Warning]There are %d plugs on [POLLING] entry, this will increase latency of packet process!\033[0m\n", g_PollingFunNum);
- usleep(10000);
- }
- return 0;
-}
-
-
-void MESA_platform_run(void)
-{
-#if (0 == SAPP_AS_TARGET_SO)
- /*
- ʹ��atexit()ע��ĺ���, ������ע�ắ���෴, ���Ծ������Ӻ�atexit()ע��λ��,
- �������г�ʼ�����֮��, packet_io_run()֮ǰ��
- */
- //atexit(forbid_call_exit_in_running_state); //wangyan��Ŀ��������������exi, ��дcore
- packet_io_run();
-#endif
-}
-
-#ifdef __cplusplus
-}
-#endif
-
-
+#include <stdio.h> +#include <string.h> +#include <pthread.h> +#include <unistd.h> +#include <arpa/inet.h> +#include <MESA/MESA_handle_logger.h> +#include "MESA/MESA_prof_load.h" +#include "dealpkt.h" +#include "deal_ipv4.h" +#include "deal_ipv6.h" +#include "packet_io.h" +#include "project_requirement.h" +#include "stream_internal.h" +#include "mesa_net.h" +#include "dictator.h" +#include <sysinfo.h> +#include <assert.h> +#include <errno.h> + + +#ifdef __cplusplus +extern "C" { +#endif + +static int threadnum = 1; +static volatile long stream_num = 0; +int g_iThreadNum = 1; +time_t g_CurrentTime;//add by lqy 20070606 +time_t g_sapp_start_time; +volatile unsigned long long g_SysInputInfo[MAX_THREAD_NUM][COUNTER_NUM]; +volatile unsigned long long sysinfo_new[COUNTER_NUM] = {0}; +extern int sapp_args_v; +int ipv4_frag_init(int thread_count, unsigned int hash_size); +int ipv6_frag_init(int thread_count, unsigned hash_size); +int project_requirement_global_init(void); +int packet_io_set_cap_level(int cap_level); +int plugctrl_proc(void); +int runtime_log_init(char * file, int level); +int iknow_info_init(void); +extern void sesame_open_door(const char *lock_path, const char *lock_name); +/* (0:pag,1:pcap,2:dumpfile,3:pfring,4:DPDK,5:ppf,6:NPacket,7:qnf,8:N95,9:pcap-dumpfile-list, 10:topsec) */ +int cap_mode = 2; +int top_mode = 0; +extern int G_DICTATOR_SW; +extern struct global_stream **G_MESA_GLOBAL_STREAM; +extern int G_SKIP_NOT_IP_LAYER; /* ������IPЭ��IJ�, ����˫ջ�����������, RST������ */ +//static int __times = 0; +int g_timestamp_record_sw = 0; +extern long long g_timedelay_threshold; +extern int g_use_MESA_sleep_sw; +extern int g_raw_pkt_broken_check; +extern int g_packet_io_cap_mode; +extern int g_skip_ethernet_layer_sw; +extern int g_discard_ack_sw; +extern int g_kill_tcp_remedy_sw; +extern int g_analyse_tcp_opt_sw; +extern int g_encapsulate_with_ddp; +extern int G_DICTATOR_SW; +extern int g_PollingFunNum; +extern int packet_io_set_ipv6_raw_socket_enable(int op_switch); +extern const char *stream_addr_list_ntop_outward(const struct streaminfo *pstream); +extern const char *stream_addr_list_ntop_inward(const struct streaminfo *pstream); +extern const struct streaminfo *stream_addr_list_pton_outward(const char *addr_list_str, int thread_index); +extern const struct streaminfo *stream_addr_list_pton_inward(const char *addr_list_str, int thread_index); +extern int cycle_pkt_dump_init(int argc, char *argv[]); +extern void udp_set_stream_timeout(unsigned short timeout); +extern char udp_teredo_identify_entry(const struct streaminfo *pstream, const void *this_hdr, const void *raw_pkt, void **pme); +extern char isakmp_protocol_entry(const struct streaminfo *a_udp, void **pme, int thread_seq,const void *ip_hdr); +extern int stream_register_udp_raw(SAPP_STREAM_FUN_T x); +extern int number_is_2powerN(uint n); +extern int symbol_check(void); +extern int timestamp_record_init(void); +extern void sapp_global_val_check(void); +extern int sapp_global_val_init(void); + +static void forbid_call_exit_in_running_state(void) +{ + if(g_packet_io_cap_mode != CAP_MODEL_PCAP_DUMPFILE){ + printf("\033[41mCall exit() in running state is forbidden!!\033[0m\n"); + abort(); + } +} + +/* 2016-09-22 lijia add, dictator�ڵ�һ�α�����ʱ�г�ʼ������, ��ֹ���߳������ͻ, ƽ̨�ȵ���һ�� */ +static void sapp_dictator_init(void) +{ +#if USE_MEMPOOL + void *tmp; + if(G_DICTATOR_SW){ + tmp = __wrap_malloc(1); + assert(tmp != NULL); + __wrap_free(tmp); + + tmp = malloc(1); + assert(tmp != NULL); + free(tmp); + }else{ + printf("\033[33m[Warning]dictator is not enable, please check 'conf/main.conf -> dictator_switch'.\033[0m\n"); + } +#else + printf("\033[33m[Warning]sapp is compiled without dictator.\033[0m\n"); +#endif +} + +int MESA_platform_init(int argc, char *argv[]) +{ + int queue_max_num, ipv6_switch = 0; + char pcap_dev1[128], pcap_dev2[128], snd_dev[128]; + char cap_filter[1024] = {}; + char gateway_mac[32] = {}; + int max_udp_stream_num, max_tcp_stream_num, create_link_mode; + int TcpAllEnable = 0; + //pthread_t gettimeThread; + int maxrandval, randkeyval; + int load_plug_sw = 1; + unsigned short udp_stream_timeout = 300, max_unorder_num; + unsigned short link_timeout = 600; + int int_tmp; + + g_CurrentTime = time(NULL); + g_sapp_start_time = g_CurrentTime; + + /* 2016-09-22 lijia add, dictator�ڵ�һ�α�����ʱ�г�ʼ������, ��ֹ���߳������ͻ, ƽ̨�ȵ���һ��, ��Ҫ��ȡ�����ļ����� */ + MESA_load_profile_int_def("conf/main.conf","Module", "dictator_switch", &G_DICTATOR_SW, 1); + sapp_dictator_init(); + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"ShowStatInfo", (char *)"platform_log_level", &g_sapp_log_level, 10); + g_sapp_log_handle = MESA_create_runtime_log_handle("./log/runtimelog", g_sapp_log_level); + if(NULL == g_sapp_log_handle) + { + printf("MESA_create_runtime_log_handle() error!\n"); + return -1; + } + + if(MESA_load_profile_int_def("conf/main.conf", "Module", "capdatamodlel", &cap_mode, 2) < 0){ + printf("read conf 'capdatamodlel' error!\n"); + exit(1); + } + + if(MESA_load_profile_int_def("conf/main.conf", "Module", "threadnum", &threadnum, 1) < 0){ + printf("read conf 'threadnum' error!\n"); + exit(1); + } + g_iThreadNum = threadnum; + + MESA_load_profile_int_def("conf/main.conf","Module", "MaxUdpStreams",&max_udp_stream_num , 65536); +#if HIGH_PERF + if(number_is_2powerN(max_udp_stream_num) == 0){ + printf("\033[41m '%s' must be power of 2! such as 32768,65536,131072...\033[0m\n", "MaxUdpStreams"); + exit(1); + } +#endif + + MESA_load_profile_int_def("conf/main.conf","Module", "MaxTcpStreams",&max_tcp_stream_num, 65536); +#if HIGH_PERF + if(number_is_2powerN(max_udp_stream_num) == 0){ + printf("\033[41m '%s' must be power of 2! such as 32768,65536,131072...\033[0m\n", "MaxTcpStreams"); + exit(1); + } +#endif + + MESA_load_profile_int_def("conf/main.conf","Module", "CreatLinkMode", &create_link_mode, TCP_CTEAT_LINK_BYSYN); + MESA_load_profile_int_def("conf/main.conf","Module", "TcpAllEnable",&TcpAllEnable, 0); + tcp_set_support_tcpall_model(TcpAllEnable); + + MESA_load_profile_int_def("conf/main.conf","Module", "discard_ack",&g_discard_ack_sw, 0); + MESA_load_profile_int_def("conf/main.conf","Module", "kill_tcp_remedy",&g_kill_tcp_remedy_sw, 0); + + MESA_load_profile_int_def("conf/main.conf","Module", "maxrandval", &maxrandval, 65535); + set_rst_max_rnd_val(maxrandval); + MESA_load_profile_int_def("conf/main.conf","Module", "randkeyval", &randkeyval, 13); + set_rst_rand_key(randkeyval); + + ipv4_frag_init(threadnum, 1024 * 512); + MESA_load_profile_int_def("conf/main.conf","Module", "IPv6_module_enable", &ipv6_switch, 1); + packet_io_set_ipv6_module_enable(ipv6_switch); + MESA_load_profile_int_def("conf/main.conf","Module", "IPv6_raw_socket", &ipv6_switch, 0); /* Ϊ�˱��ڲ���, ����Ĭ�Ϲر� */ + packet_io_set_ipv6_raw_socket_enable(ipv6_switch); + ipv6_frag_init(threadnum, 1024 * 256); + + tcp_set_creatlink_model(create_link_mode); + MESA_load_profile_short_def("conf/main.conf","Module", "MaxUnorderNum",(short *)&max_unorder_num, 5); + tcp_set_default_unorder_num(max_unorder_num); + tcp_set_stream_num(max_tcp_stream_num*3,max_tcp_stream_num,max_tcp_stream_num*2); + + MESA_load_profile_short_def("conf/main.conf","Module", "LinkTimeout", (short *)&link_timeout, 600); + stream_set_default_nopkt_time(link_timeout); + + MESA_load_profile_short_def("conf/main.conf","Module", "UdpResetTime", (short *)&udp_stream_timeout, 300); + udp_set_stream_timeout(udp_stream_timeout); + udp_set_stream_num(max_udp_stream_num*3,max_udp_stream_num,max_udp_stream_num*2); + + MESA_load_profile_int_def("conf/main.conf","Module", "skip_not_ip_layer", &G_SKIP_NOT_IP_LAYER, 0); + if(G_SKIP_NOT_IP_LAYER != 0){ + printf("\033[41mWarning! '%s' is enable!\033[0m\n", "skip_not_ip_layer"); + sleep(1); + } + + MESA_load_profile_int_def("conf/main.conf","Module", "timestamp_record", &g_timestamp_record_sw, 0); + MESA_load_profile_int_def("conf/main.conf","Module", "timedelay_threshold", &int_tmp, 10000000); + g_timedelay_threshold = (long long)int_tmp; + + MESA_load_profile_int_def("conf/main.conf","Module", "use_MESAsleep", &g_use_MESA_sleep_sw, 0); + + MESA_load_profile_int_def("conf/main.conf","Module", "analyse_tcp_option", &g_analyse_tcp_opt_sw, 1); + + MESA_load_profile_int_def("conf/main.conf","Module", "encapsulate_with_ddp", &g_encapsulate_with_ddp, 0); + if(g_encapsulate_with_ddp != 0){ + printf("\033[32m[Notice] '%s' is enable!\033[0m\n", "encapsulate_with_ddp"); + sleep(1); + } + +#if (0 == SAPP_AS_TARGET_SO) /* ��Ϊ.soģʽ, ����dlopen���io.so */ + if(packet_io_lib_load(cap_mode) < 0){ + return -1; + } + + packet_io_set_work_thread_num(threadnum); + + packet_io_set_cap_mode(cap_mode); + + /* + (0:pag,1:pcap,2:dumpfile,3:pfring,4:DPDK,5:ppf,6:NPacket,7:qnf,8:N95,9:pcap-dumpfile-list, 10:topsec) + 11:ipfile, 12:marsio4, 13:agent_smith, 14:dpdk_vxlan, 15:marsio_vxlan, 16:pag_marsio. + */ + switch(cap_mode){ + case CAP_MODEL_PAG: + case CAP_MODEL_PAG_MARSIO: +#if USE_PAG_GET_FRAME + packet_io_set_cap_level(CAP_LEVEL_MAC); +#else + packet_io_set_cap_level(CAP_LEVEL_IPV4); +#endif + break; + + case CAP_MODEL_PPF: + case CAP_MODEL_TOPSEC: + case CAP_MODEL_IPFILE: + packet_io_set_cap_level(CAP_LEVEL_IPV4); + break; + + case CAP_MODEL_PCAP_ONLINE: + case CAP_MODEL_PCAP_DUMPFILE: + case CAP_MODEL_AGENT_SMITH: +#if PCAP_CAP_FROM_IP + packet_io_set_cap_level(CAP_LEVEL_IPV4); + break; +#endif + + case CAP_MODEL_PFRING: + case CAP_MODEL_DPDK: + case CAP_MODEL_NPACKET: + case CAP_MODEL_PCAP_DUMPLIST: + case CAP_MODEL_MARSIOV4: + case CAP_MODEL_DPDK_VXLAN: + case CAP_MODEL_MARSIOV4_VXLAN: + packet_io_set_cap_level(CAP_LEVEL_MAC); + break; + default: + printf("Invalid cap_mode:%d\n", cap_mode); + exit(1); + } + + if(MESA_load_profile_int_def("conf/main.conf", "Module", "net_connection_mode", &top_mode, 1) < 0){ + printf("read conf 'net_connection_mode' error!\n"); + exit(1); + } + packet_io_set_topology_mode(top_mode); + + if(MESA_load_profile_string_def("conf/main.conf", "Module", "pcapdevice", pcap_dev1, 128, "eth0") < 0){ + printf("read conf 'pcapdevice' error!\n"); + exit(1); + } + packet_io_set_capdev_parallel(pcap_dev1); + + if(2 == top_mode){ + if(MESA_load_profile_string_def("conf/main.conf", "Module", "pcapdevice2", pcap_dev2, 128, "eth1") < 0){ + printf("read main.conf 'pcapdevice2' error!\n"); + exit(1); + } + packet_io_set_capdev_serial(pcap_dev1, pcap_dev2); + } + + if(MESA_load_profile_string_def("conf/main.conf", "Module", "pcapfilter", cap_filter, 1024, "") < 0){ + printf("read conf 'pcapfilter' error!\n"); + exit(1); + } + if(cap_filter[0] != '\0'){ + printf("\033[33m[Warning] pcap filter is '%s'.\033[0m\n", cap_filter); + packet_io_set_capture_filter(cap_filter); + } + + if(MESA_load_profile_int_def("conf/main.conf", "Module", "queue_max_num", &queue_max_num, 2000) < 0){ + printf("read conf 'queue_max_num' error!\n"); + exit(1); + } + + packet_io_set_cap_buf_queue(queue_max_num); + + if(MESA_load_profile_string_nodef("conf/main.conf", "Module", "gateway_mac", gateway_mac, 32) < 0){ + printf("read conf 'gateway_mac' error!\n"); + } + packet_io_set_gateway_mac(gateway_mac); /* TODO, ��ȡ�����ļ� */ + + if(MESA_load_profile_string_nodef("conf/main.conf", "Module", "senddevice", snd_dev, 128) < 0){ + printf("read conf 'senddevice' error!\n"); + exit(1); + } + packet_io_set_send_dev(snd_dev); + + MESA_load_profile_int_def("conf/main.conf", "Module", "signal_take_over_switch", &sapp_global_single.signal_take_over_sw, 0); + MESA_load_profile_int_def("conf/main.conf", "Module", "ipentry_priority_over_ipfrag", &sapp_global_single.ipentry_priority_over_ipfrag, 0); + MESA_load_profile_int_def("conf/main.conf", "Module", "tuple4_reuse_time_interval", &sapp_global_single.tuple4_reuse_time_interval, 3); +#endif + + init_stream_manage(threadnum); + +#if DEBUG + iknow_info_init(); + + timestamp_record_init(); +#endif + + MESA_load_profile_int_def("conf/main.conf","Module", "load_plug_switch", &load_plug_sw, 1); + if(load_plug_sw){ + plugctrl_proc(); + } + + /* 2017-02-24 lijia modify, + NOTE: + �ȳ�ʼ�����, ����ڲ����ܻ����pthread_create��̬�����߳�, + ������, ��DPDKģʽ��, dpdk��ʼ���ξͻ��ǰ�̵߳�ijCPU����, + ��ô������pthread_create���߳�Ĭ�ϼ̳������Ϊ, + ����, �ȳ�ʼ���߳��ٳ�ʼ��packet_io. + */ + if(packet_io_init(argc, argv) < 0){ + return -1; + } + + +#if CYCLE_PKT_DUMP + if(cycle_pkt_dump_init(argc , argv) < 0){ + return -1; + } +#else + int __pkt_dump_switch = 0; + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_switch", &__pkt_dump_switch, 0); + if(__pkt_dump_switch != 0){ + printf("\n\033[33m[Warning] pkt_dump enable, but the platform is compiled without debug feature.\033[0m\n"); + printf("\033[33mIf you want pkt_dump function, please recompile platform use 'make debug=0'.\033[0m\n"); + printf("\033[33mOtherwise, please close 'pkt_dump_switch', or ignore this message.\033[0m\n"); + usleep(10000); + } +#endif + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"Module", (char *)"raw_pkt_broken_check", &g_raw_pkt_broken_check, 0); + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"Module", (char *)"skip_ethernet_layer", &g_skip_ethernet_layer_sw, 0); + + /* ƽ̨���ù̶������IJ�� */ + stream_register_udp_raw(udp_teredo_identify_entry); + + sapp_assistant_init(); + + /* NOTE: ���������һ��Ҫ��plugctrl_proc(), ���������֮�� */ + symbol_check(); /* 2016-01-13 Lijia add, ���ȫ�ַ�������ͻ */ + + sapp_global_val_init(); + + if(g_PollingFunNum != 0){ + printf("\n\033[33m[Warning]There are %d plugs on [POLLING] entry, this will increase latency of packet process!\033[0m\n", g_PollingFunNum); + usleep(10000); + } + return 0; +} + + +void MESA_platform_run(void) +{ +#if (0 == SAPP_AS_TARGET_SO) + /* + ʹ��atexit()ע��ĺ���, ������ע�ắ���෴, ���Ծ������Ӻ�atexit()ע��λ��, + �������г�ʼ�����֮��, packet_io_run()֮ǰ�� + */ + //atexit(forbid_call_exit_in_running_state); //wangyan��Ŀ��������������exi, ��дcore + packet_io_run(); +#endif +} + +#ifdef __cplusplus +} +#endif + + diff --git a/include/stream_internal.h b/include/stream_internal.h index 0ff5f45..29d0309 100644 --- a/include/stream_internal.h +++ b/include/stream_internal.h @@ -1,342 +1,343 @@ -#ifndef _APP_STREAM_INTERNAL_H_
-#define _APP_STREAM_INTERNAL_H_
-
-#include "stream_inc/stream_base.h"
-#include "stream_inc/stream_project.h"
-#include "stream_inc/stream_proxy.h"
-#include "stream_inc/stream_tunnel.h"
-#include "stream_register.h"
-#include "stream_custom.h"
-#include "stream_inc/stream_inject.h"
-#include "stream_inc/stream_rawpkt.h"
-#include "stream_inc/stream_control.h"
-#include "mesa_net.h"
-#include "sysinfo.h"
-#include <MESA/MESA_handle_logger.h>
-
-#define STREAM_BASE_MD5_CHECK "f9efd82ec3123dcb9075ea916e3936aa"
-#define STREAM_CONTROL_MD5_CHECK "f96fffb08330abde4e058db57ef63a43"
-#define STREAM_ENTRY_MD5_CHECK "7dab86d65114ebe5438e85e0d008645d"
-#define STREAM_INJECT_MD5_CHECK "3853e02b53c916979d1d4cba4df00d0b"
-#define STREAM_PROJECT_MD5_CHECK "fc2b981f7e2c99e73d8857e206cb4977"
-#define STREAM_PROXY_MD5_CHECK "25cec664c9b44a8cac29f6e3e117eaa6"
-#define STREAM_RAWPKT_MD5_CHECK "7a2023151fe16d54dfefd2724413bcc6"
-#define STREAM_TUNNEL_MD5_CHECK "d20aa6b4f5683b7b0040676547997be0"
-
-#define IP_PORT_UNION_VERSION (1) /* �Ƿ�IP-PORT�ϲ� */
-#define COMPAT_PAPP_FOR_BENCHMARK (0) /* 2015-01-07 lijia add, ͬpapp�ԱȽ��ʱ, ��ʱ�ر�Ƕ�ס���������Э��, ����һ�� */
-#define USE_RBTREE_INSTEAD_LIST (0) /* ��HASH��ͻʱ, ʹ�ú������������, ������������±��� */
-#define USE_LINUX_KERNEL_HASH_ALGO (1) /* ʹ��LINUX�ں�HASH�㷨 */
-//#define SAPP_AS_TARGET_SO (1) /* 2017-05-17 lijia add, sapp��so��ʽ, ��IP_ENTRY����ص���һ��ƽ̨������ */
-
-#define SAPP_INSECTICIDE (0) /* ɱ��(DEBUG)��ʱ����, ������Ī�������BUGʱ��ʱ����, ���緢������, �����糬�� */
-
-#define CYCLE_PKT_DUMP (1 && DEBUG) /* 2015-04-16 lijia add, �������������coredump, ���Ҳ���ԭ�� */
-
-#define PCAP_CAP_FROM_IP (0) /* Ϊ��ģ��pag��������, ��pcapҲģ���IPv4ͷ����ʼ��ȡ */
-
-#define NON_PKT_THREAD_SEND_QUEUE (64) /* �ǰ������߳�����Ͷ������� */
-
-#ifndef likely
-#define likely(x) __builtin_expect(!!(x), 1)
-#endif
-
-#ifndef unlikely
-#define unlikely(x) __builtin_expect(!!(x), 0)
-#endif
-#ifndef offsetof
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
-
-#ifndef container_of
-#define container_of(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
-#endif
-
-#define sapp_get_struct_header(ptr, type, member) container_of(ptr, type, member)
-
-#define RAW_PKT_MAGIC_NUM (0xF1E2D3C4) /* 1:���ڼ��ݾ���Ŀ, ��call_old��������ݽṹʱ,ʶ��ԭʼ������Դ; 2:��ȫ�Լ�� */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* ԭʼ���ṹ */
-typedef struct {
- unsigned int magic_num;
- int offset_to_raw_pkt_hdr; /* ����ص���Ӧ��������raw_pkt_data��ƫ���� */
- enum addr_type_t low_layer_type; /* ԭʼ����ײ�Э�������, ������MAC(pcap����), Ҳ������IPv4(pag����) */
- int __lib_raw_pkt_len; /* �ײ㲶�����ṩ�İ���ʵԭʼ���� */
- int raw_pkt_len; /* ���ϲ�Ӧ�ÿ���ԭʼ���ܳ���, �п��ܲ�����ʵ��, ����������Ethernet�� */
- unsigned int hd_hash; /* ����Ӳ���������Ԫ��HASH, ����ƽ̨CPU�������� */
- const void *__lib_raw_pkt_data; /* �ײ㲶�����ṩ����ʵԭʼ��ָ�� */
- const void *raw_pkt_data; /* ���ϲ�Ӧ�ÿ���ԭʼ��ͷָ��, �п���������Ethernet��, ����low_layer_type�ж�Э������ */
- struct timeval raw_pkt_ts; /* ԭʼ������ʱ���, ���ȫΪ0��֧�ִ˹���(��pagģʽ) */
- const void *io_lib_pkt_reference; /* ���õײ�I/O���ԭʼ�������ṹ, ����:����marsio��˵, ���ײ��mbuf�ṹ */
-}raw_pkt_t;
-
-
-struct buf_unorder
-{
- struct buf_unorder *next;
- struct buf_unorder *prev;
- //void *data;
- //UINT32 len;
- void *this_ip_hdr; /* ������Դ����ʵԭʼ��, Ҳ������Դ�������IP��Ƭ��,��'ip_reassemble_pkt'���� */
- struct mesa_tcp_hdr *this_tcp_hdr;
- void *tcpdata; /* TCP�����ݲ���ָ��, ����TCPͷ */
- UINT16 tcpdatalen; /* TCP�����ݲ��ֳ��� */
- UINT16 urg_ptr;
- char fin;
- char urg;
- char rst;
- unsigned char ip_reassemble_pkt;
- UINT32 seq;
- UINT32 ack;
- raw_ipfrag_list_t *ipfrag_list; /* �����ǰ����IP��Ƭ����İ�, ��Ҫ�洢����IP��Ƭ������ */
- raw_pkt_t raw_pkt; /* ������洢ԭʼ�� */
-};
-
-/*�����ṹ�嶨�壺*/
-struct half_tcpstream
-{
- UCHAR *data;
- UINT32 offset; /*data�е�һ���ֽ���TCP�������е�ƫ����*/
- UINT32 count; /*�����ӽ���������Ϊֹ������������ܳ����ֽ���*/
- UINT32 count_new; /*�����µ��������ֽ���*/
- UINT32 count_ideal; /*�����ӽ���������Ϊֹ��������Ӧ�õ���������ܳ���*/
- UINT32 pktcout; /*�����ۼƵ���İ�����, �����ش���, ACK, ������SYN */
- UINT32 totallost; /*�����ۼƶ�������*/
- UINT32 seq; /*���������ڴ���seq���*/
- UINT32 first_data_seq; /*����������ʼ�ĵ�seq���*/
-
- //UINT32 ack_seq; /*�����������ʹ�õ�Ӧ���, 2017-08-02 lijia modify , �ƶ���struct tcpdetail_private */
-
- UINT16 window; /*�������ݻ������ڴ�С*/
- UCHAR __pad__;
- UCHAR finstate; /*fin״̬*/
- UINT16 unorder_cnt;/* 2014-11-27 lijia modify, ijЩ����������, UCHAR�Ͳ���, ��չΪUINT16 */
- UINT16 maxunorder; /* 2014-11-27 lijia modify, ijЩ����������, UCHAR�Ͳ���, ��չΪUINT16 */
- struct buf_unorder *unorderlist; /*�����������*/
- struct buf_unorder *unorderlisttail; /*�����������β��ָ��*/
-};
-
-struct streaminfo_private
-{
- /* ����ṹ�����ڽṹ����ǰ, ָ���ַ���Ի���ǿת */
- struct streaminfo stream_public;
- /* ���±���Ϊƽ̨�ڲ�˽��, ���ⲻ�ɼ� */
- void *pproject; //ÿ�����̿����Զ���ʹ�ã�
- UCHAR layer_dir:2; /* ������Ч, ��ǰ��ĵ�ַ�Ƿ��Ĭ�Ϲ���"��˿��ǿͻ���"��ͬ */
- UCHAR stream_dir:2; /* ��������������Ч, ���Ĵ洢�ĵ�ַ�Ƿ��Ĭ�Ϲ���"��˿��ǿͻ���"��ͬ */
- UCHAR addr_use_as_hash:1; /* �����addr�Ƿ���ΪHASH����ͱȽϵIJ���, ��:MAC��ַ��������� */
- UCHAR addr_skip_for_layer_cmp:1;/*�����addr�Ƿ���Ϊ��ַ�ȽϵIJ㼶���磺MPLS��ַ����Ϊ�������㣬ֱ����������ֵĬ��Ϊ0������Ҫ�Ƚ�*/
- UCHAR need_update_opposite_addr:1;/*�����addr�Ƿ��ڶԲ�����ʱ���£��磺MPLS��ǩ�ǶԳ�ʱ��Ҫ��S2C���һ������¼�����ǩ,��ֵĬ��Ϊ0��������Ҫ����*/
- UCHAR stream_killed_flag:1; /* 2014-08-22 lijia add, ����ģʽ��, �Ѿ������Kill, ֮�������ֱ��Drop��Kill, �����ٸ��ϲ��� */
- UCHAR dirreverse; /* ��������ʱ�Ƿ������ip��ַ��ת, ����"��˿��ǿͻ���"�����෴ */
- UINT16 timeout;/* ÿ�����ӵĶ��г�ʱʱ��, ��ֵ��������;, 1:���ھ�����̭��ʱ���ް��������, �Խ�Լ�ڴ�; 2:���ڱ�����ʱ���ް�����, ����IM�ʱ���ް������Ӳ�δ����, �����ýϴ��timeout */
- unsigned short offset_to_raw_pkt_hdr; /* ����ͷ�����ԭʼ������ʼ��ַ��ƫ���� */
- unsigned short offset_to_ip_hdr; /* 2015-12-07 lijia add, UDP/TCP ��ͷ����ڳ��ص�IP��ͷƫ���� */
- const raw_pkt_t *raw_pkt; /* 2014-12-30 lijia add, ��Щ�ص�������֧��ԭʼ��, ����������Ҫ, �洢��private�ṹ�� */
- unsigned int hash_slave; /* 2015-12-14 lijia add, ʹ��linux_jhash����ʱ, ����ͬʱ�õ��������ϵ�HASHֵ, ��HASH����ȷ����HASH����SLOTλ��, slave_HASH���ڿ��ٱȽϵ�ַ�Ƿ���� */
- unsigned char hash_not_head_times;/* 2015-12-15 lijia add, ��ǰindex����HASH SLOT�ĵ�һλ�Ĵ��� */
- unsigned char cur_layer_raw_hdr_len; /* 2017-10-31 lijia add, ��ǰ���ԭʼ����ַ����, ��pppͷ��ѹ��, ԭ���汾���������״̬, ����ʱҲ����֪��ַ������ */
- char __pad;
- unsigned char gdev_block_timer;
- unsigned short stream_low_layer_tunnel_type; /* 2016-07-25 lijia add, ��¼�����ײ���������, 0Ϊ������, �������:enum stream_carry_tunnel_t */
- unsigned short stream_carry_up_layer_tunnel_type; /* ��ǰ���ϲ����������, ���統ǰ��ΪUDP, ���صĿ�����teredo������L2TP���� */
-
- /* 2016-07-08 lijia add, for janus hijack, ��Ӧ�ô洢��half_stream, �����յ�SYNʱ, ��û�д���half_streamʵ��, �����ݴ���streaminfo_private */
- unsigned short syn_opt_num;
- unsigned short synack_opt_num;
- struct tcp_option *syn_opt_array;
- struct tcp_option *synack_opt_array;
-};
-
-struct tcpdetail_private
-{
- /* ����ṹ�����ڽṹ����ǰ, ָ���ַ���Ի���ǿת */
- struct tcpdetail tcpdetail_public;
- UCHAR multisynflag:2; // multi syn
- UCHAR ignore_rst_fin:2;//������rst, fin����, ֻ����ʱ��lru����
- UCHAR needackflag:2; //��Ҫ�ϴ�ack����
- UCHAR takeoverflag:2;
- UCHAR tcpstateflag; // ���ڼ�¼tcp�ĻỰSYN���״̬
- UCHAR link_state; // ���ӵ�״̬
- UCHAR creat_mod;
- UINT16 tcpoverlen; // modify by lqy 20150225, ��¼��ǰ������һ�������ص�tcp���ȣ�
- UINT16 pad;
- struct half_tcpstream *pclient; //��client��TCP������Ϣ
- struct half_tcpstream *pserver; //�� server��TCP������Ϣ
- UINT32 iserverseq; //���ӽ���ʱ��ʱ�洢seq, C2S, ISN+1
- UINT32 iclientseq; //���ӽ���ʱ��ʱ�洢seq, S2C, ISN+1
- /* NOTE:
- ��first_ack_seq������half_tcpstream��?
- ��half_tcpstream�����յ���һ�����и��صİ��ŷ����,
- �����tcp_deal_ack()����������, �����S2C����, ��ô��һ��S2C���ݰ�����ʱ, �Ѿ�����first_ack_seq��.
- �����Է�ֹDDOS����.
- */
- UINT32 C2S_first_ack_seq; /* 2017-08-02 lijia modify, C2S���һ��ACK��, ��half_tcpstream->ack_seqһ����Լ�����������Բ���������� */
- UINT32 C2S_ack_seq; /* 2017-08-02 lijia add, C2S�൱ǰ��ACK�� */
- UINT32 S2C_first_ack_seq; /* 2017-08-02 lijia modify, C2S���һ��ACK��, ��half_tcpstream->ack_seqһ����Լ�����������Բ���������� */
- UINT32 S2C_ack_seq; /* 2017-08-02 lijia add, S2C�൱ǰ��ACK�� */
- void *apme; //Ӧ�ò�������
- void *pAllpktpme; //��״̬��tcp����������
- struct tcp_flow_stat *flow_stat; /* 2016-07-14 lijia add, ���ڼ�¼TCP��data���ļ���, ʵ���ڴ������ͷ���projectģ�����, ����ΪNULL, */
- struct tcp_flow_stat *deduce_flow_stat; /* 2018-10-30 lijia add, ���ڼ�¼������ƶϳ������ϴ��������, ���������ij���; �Լ������������, �Զ�Ӧ���յ������� */
-};
-
-struct udpdetail_private
-{
- /* ����ṹ�����ڽṹ����ǰ, ָ���ַ���Ի���ǿת */
- struct udpdetail udpdetail_public;
- void *apme; //Ӧ�ò�������
- struct udp_flow_stat *flow_stat; /* 2015-12-28 lijia add, udpdetail�е���ϸ����(64bit), ʵ���ڴ������ͷ���projectģ�����, ����ΪNULL */
-};
-
-/* 2015-02-26 lijia add, for stream-addr-list ntop, pton */
-typedef struct{
- struct streaminfo stream;
- char addr_value[MAX_ADDR_BIN_VALUE_LEN]; /* Ϊ��paddr������malloc, �������, ��stream������һ�黺�� */
-}addr_continuous_bin_t;
-
-typedef struct{
- enum addr_type_t addr_type_bin;
- UCHAR stream_type;/* ����ʶ��ADDR_TYPE_IPV4��TCP����UDP */
- const char *addr_type_str;
- const char *addr_type_prefix;
- const char *addr_type_prefix_with_delim; /* ���ָ�����ǰ, ���ڱȽ��ַ���, ��ֹIPv4��IPv4_TCP�ַ����Ƚ�ʱ����, �����IPv4:, IPv4_TCP:�Ͳ����ڻ������� */
- int (*addr_n2p_fun)(const struct layer_addr *paddr, char *buf, int buf_len);
- int (*addr_p2n_fun)(char *addr_str, addr_continuous_bin_t *addr_bin_val);
-}addr_convert_t;
-
-struct pptp_stream_key{
- UINT32 sip; /* TCP-SYN����ԴIP, ����ǰ�data������, ����pptp->message_type�ж�, REQ���������ԴIP��Ϊ��sip */
- UINT32 dip; /* sip,dip are network order */
- UINT16 sip_side_call_id; /* �������ݰ�����sip������GRE����callid, ʵ�������ʾpeer call id, network order */
- UINT16 dip_side_call_id; /* �������ݰ�����dip������GRE����callid, ʵ�������ʾpeer call id, network order */
- //struct streaminfo_private *stream_pr; /* TODO, ���ڱ����ַ����, Ŀǰ��������ײ��IP��call_id */
-};
-
-/* PPTPЭ�����������ṹ, sapp�ڲ�ʹ�� */
-struct pptp_info_pri{
- struct MESA_tunnel_info tunnel_context;
- struct pptp_stream_key pptp_key;
- stSessionInfo ssinfo; /* ����ҵ�����������Ϣ */
- void *biz_pme; /* ҵ����Զ������� */
- struct streaminfo *my_stream; /* pptp�ײ�UDP�� */
- unsigned char threadnum;
- char insert_hash_flag; /* ���������ѽ�key����HASH�� */
- char content_notify_biz_flag; /* �Ƿ��Ѿ����ù�ҵ����, ��֮content_type */
-};
-
-/* ˽��pptp��ַ, pptp_addr�ṩ��ҵ����, ����gre_layer_len����FDʱ����ʶ�𱾲�ͷ������ */
-struct layer_addr_pptp_pri{
- struct layer_addr_pptp pptp_addr;
- int gre_layer_len;
-};
-
-struct l2tp_stream_key{
- UINT32 sip; /* L2TP������������IP, ����������SCCRQ, ICRQ, SCCCN�Ȱ���ԴIP, network order */
- UINT32 dip; /* L2TP�����������˵�IP, network order */
- UINT16 sport; /* network order */
- UINT16 dport; /* network order */
- UINT16 sip_side_tunnel_id; /* sip�ⷢ������tunnelid, network order */
- UINT16 sip_side_session_id; /* sip�ⷢ������tunnelid, network order */
- UINT16 dip_side_tunnel_id; /* dip�ⷢ������tunnelid, network order */
- UINT16 dip_side_session_id; /* dip�ⷢ������tunnelid, network order */
- //struct streaminfo_private *stream_pr; /* TODO, ���ڱ����ַ����, Ŀǰ��������ײ��IP��call_id */
-};
-
-struct l2tp_info_pri{
- struct MESA_tunnel_info tunnel_context;
- struct l2tp_stream_key l2tp_key;
- stSessionInfo ssinfo; /* ����ҵ��������� */
- void *biz_pme;
- const struct streaminfo *my_stream; /* l2tp�ײ�UDP�� */
- unsigned char thread_seq;
- char content_notify_biz_flag; /* �Ƿ��Ѿ����ù�ҵ����, ��֮content_type */
-};
-
-/* ȫ��Ψһ��ȫ�ֱ���, ͨ���ǿ�����������, ���仯��ȫ�ֱ���, ����������, ������ֻ�� */
-struct sapp_global_single_t{
- int signal_take_over_sw; /* �Ƿ�ӹܳ���ϵͳ�ź�, ��SIGSEGV, SIGABRT�ȵ� */
- int ipentry_priority_over_ipfrag; /* IP_entry���ȼ�����IP_frag_entry, Ĭ��Ϊ0, ��ӦWY�����ض����� */
- int tuple4_reuse_time_interval; /* ��Ԫ�����õ���С���ʱ�� */
- void *fs2_handle;
- const char *fs2_server_ip;
- unsigned short fs2_server_port_host;
- short __pad1;
- int fs2_filed_id_array[COUNTER_NUM];
- int treat_vlan_as_mac_in_mac_sw;
-};
-
-#define TIMESTAMP_SINGLE_PKT_REGION (4) /* ������ʱͳ������ */
-#define TIMESTAMP_SINGLE_PKT_REGION_MAX (5) /* ��������ͳ�������ֵ */
-typedef struct{
- unsigned int runtime_pkt_max_delay; /* �����������ʱ */
- unsigned long long pkt_total_num[TIMESTAMP_SINGLE_PKT_REGION_MAX]; /* ��¼��������ʱ���ܰ���, ������ͳ�� */
- unsigned long long pkt_total_time; /* ���һ��ʱ�������ʱ, ���ڼ���ƽ������������ʱ */
-}timestamp_record_region_t;
-/* ���߳�ȫ�ֱ���, �������ݰ�ͳ����Ϣ, ͨ��ֵ��ʵʱ�仯 */
-struct sapp_global_mthread_t{
- int ipv6_pkt_type_flag; /* ipv6 packet type, normal, or rebuild */
- int pptp_hash_max_search_times;
- int l2tp_hash_max_search_times;
- char __pad1[4];
- double pptp_hash_avg_search_times;
- double l2tp_hash_avg_search_times;
- int tcp_stream_special_timeout_num; /* ���õ�����ʱʱ�����, Ĭ�ϲ�����������������10% */
- int udp_stream_special_timeout_num;/* ���õ�����ʱʱ�����, Ĭ�ϲ�����������������10% */
- timestamp_record_region_t runtime_record; /* ƽ̨����������ʱ */
- timestamp_record_region_t runtime_record_driver; /* �����������õ���, ��ƽ̨�õ�������ʱ */
- char __pad2__[40];
-};
-
-extern struct sapp_global_single_t sapp_global_single;
-extern struct sapp_global_mthread_t sapp_global_mthread[MAX_THREAD_NUM];
-
-extern int G_SKIP_NOT_IP_LAYER;
-extern int g_sapp_log_level;
-extern void *g_sapp_log_handle;
-
-#define sapp_runtime_log(log_level, format, args...) do{if(log_level>=g_sapp_log_level){MESA_handle_runtime_log(g_sapp_log_handle, log_level, "sapp", format, ##args);}}while(0)
-
-
-int MESA_kill_tcp_remedy(struct streaminfo *stream, const void *ext_raw_pkt);
-
-int get_stream_carry_tunnel_type(const struct streaminfo *this_stream, const struct streaminfo *upper_stream, unsigned short *tunnel_type);
-
-void cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt);
-char biz_retval_to_platform(char biz_ret);
-char plat_state_to_biz(char plat_state);
-void idle_polling_call(int thread_seq);
-int sapp_identify_broad_multicast_pkt(const void *this_layer_data, const raw_pkt_t *raw_pkt);
-int sapp_assistant_init(void);
-
-static inline long long sapp_get_cpu_cycle(void)
-{
-#ifdef __x86_64
-#define X86_64_ENV 1
-#endif
-#ifdef __x86_64__
-#define X86_64_ENV 1
-#endif
-
-#ifdef X86_64_ENV
- long long l;
- long long h;
-
- __asm__ volatile("rdtsc" : "=a"(l), "=d"(h));
- return (long long )l | ((long long )h<<32);
-#else
- return 0;
-#endif
-}
-#ifdef __cplusplus
-}
-#endif
-
-#endif
-
+#ifndef _APP_STREAM_INTERNAL_H_ +#define _APP_STREAM_INTERNAL_H_ + +#include "stream_inc/stream_base.h" +#include "stream_inc/stream_project.h" +#include "stream_inc/stream_proxy.h" +#include "stream_inc/stream_tunnel.h" +#include "stream_register.h" +#include "stream_custom.h" +#include "stream_inc/stream_inject.h" +#include "stream_inc/stream_rawpkt.h" +#include "stream_inc/stream_control.h" +#include "mesa_net.h" +#include "sysinfo.h" +#include <MESA/MESA_handle_logger.h> + +#define STREAM_BASE_MD5_CHECK "f9efd82ec3123dcb9075ea916e3936aa" +#define STREAM_CONTROL_MD5_CHECK "f96fffb08330abde4e058db57ef63a43" +#define STREAM_ENTRY_MD5_CHECK "7dab86d65114ebe5438e85e0d008645d" +#define STREAM_INJECT_MD5_CHECK "3853e02b53c916979d1d4cba4df00d0b" +#define STREAM_PROJECT_MD5_CHECK "fc2b981f7e2c99e73d8857e206cb4977" +#define STREAM_PROXY_MD5_CHECK "25cec664c9b44a8cac29f6e3e117eaa6" +#define STREAM_RAWPKT_MD5_CHECK "7a2023151fe16d54dfefd2724413bcc6" +#define STREAM_TUNNEL_MD5_CHECK "d20aa6b4f5683b7b0040676547997be0" + +#define IP_PORT_UNION_VERSION (1) /* �Ƿ�IP-PORT�ϲ� */ +#define COMPAT_PAPP_FOR_BENCHMARK (0) /* 2015-01-07 lijia add, ͬpapp�ԱȽ��ʱ, ��ʱ�ر�Ƕ�ס���������Э��, ����һ�� */ +#define USE_RBTREE_INSTEAD_LIST (0) /* ��HASH��ͻʱ, ʹ�ú������������, ������������±��� */ +#define USE_LINUX_KERNEL_HASH_ALGO (1) /* ʹ��LINUX�ں�HASH�㷨 */ +//#define SAPP_AS_TARGET_SO (1) /* 2017-05-17 lijia add, sapp��so��ʽ, ��IP_ENTRY����ص���һ��ƽ̨������ */ + +#define SAPP_INSECTICIDE (0) /* ɱ��(DEBUG)��ʱ����, ������Ī�������BUGʱ��ʱ����, ���緢������, �����糬�� */ + +#define CYCLE_PKT_DUMP (1 && DEBUG) /* 2015-04-16 lijia add, �������������coredump, ���Ҳ���ԭ�� */ + +#define PCAP_CAP_FROM_IP (0) /* Ϊ��ģ��pag��������, ��pcapҲģ���IPv4ͷ����ʼ��ȡ */ + +#define NON_PKT_THREAD_SEND_QUEUE (64) /* �ǰ������߳�����Ͷ������� */ + +#ifndef likely +#define likely(x) __builtin_expect(!!(x), 1) +#endif + +#ifndef unlikely +#define unlikely(x) __builtin_expect(!!(x), 0) +#endif +#ifndef offsetof +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#endif + +#ifndef container_of +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) +#endif + +#define sapp_get_struct_header(ptr, type, member) container_of(ptr, type, member) + +#define RAW_PKT_MAGIC_NUM (0xF1E2D3C4) /* 1:���ڼ��ݾ���Ŀ, ��call_old��������ݽṹʱ,ʶ��ԭʼ������Դ; 2:��ȫ�Լ�� */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* ԭʼ���ṹ */ +typedef struct { + unsigned int magic_num; + int offset_to_raw_pkt_hdr; /* ����ص���Ӧ��������raw_pkt_data��ƫ���� */ + enum addr_type_t low_layer_type; /* ԭʼ����ײ�Э�������, ������MAC(pcap����), Ҳ������IPv4(pag����) */ + int __lib_raw_pkt_len; /* �ײ㲶�����ṩ�İ���ʵԭʼ���� */ + int raw_pkt_len; /* ���ϲ�Ӧ�ÿ���ԭʼ���ܳ���, �п��ܲ�����ʵ��, ����������Ethernet�� */ + unsigned int hd_hash; /* ����Ӳ���������Ԫ��HASH, ����ƽ̨CPU�������� */ + const void *__lib_raw_pkt_data; /* �ײ㲶�����ṩ����ʵԭʼ��ָ�� */ + const void *raw_pkt_data; /* ���ϲ�Ӧ�ÿ���ԭʼ��ͷָ��, �п���������Ethernet��, ����low_layer_type�ж�Э������ */ + struct timeval raw_pkt_ts; /* ԭʼ������ʱ���, ���ȫΪ0��֧�ִ˹���(��pagģʽ) */ + const void *io_lib_pkt_reference; /* ���õײ�I/O���ԭʼ�������ṹ, ����:����marsio��˵, ���ײ��mbuf�ṹ */ +}raw_pkt_t; + + +struct buf_unorder +{ + struct buf_unorder *next; + struct buf_unorder *prev; + //void *data; + //UINT32 len; + void *this_ip_hdr; /* ������Դ����ʵԭʼ��, Ҳ������Դ�������IP��Ƭ��,��'ip_reassemble_pkt'���� */ + struct mesa_tcp_hdr *this_tcp_hdr; + void *tcpdata; /* TCP�����ݲ���ָ��, ����TCPͷ */ + UINT16 tcpdatalen; /* TCP�����ݲ��ֳ��� */ + UINT16 urg_ptr; + char fin; + char urg; + char rst; + unsigned char ip_reassemble_pkt; + UINT32 seq; + UINT32 ack; + raw_ipfrag_list_t *ipfrag_list; /* �����ǰ����IP��Ƭ����İ�, ��Ҫ�洢����IP��Ƭ������ */ + raw_pkt_t raw_pkt; /* ������洢ԭʼ�� */ +}; + +/*�����ṹ�嶨�壺*/ +struct half_tcpstream +{ + UCHAR *data; + UINT32 offset; /*data�е�һ���ֽ���TCP�������е�ƫ����*/ + UINT32 count; /*�����ӽ���������Ϊֹ������������ܳ����ֽ���*/ + UINT32 count_new; /*�����µ��������ֽ���*/ + UINT32 count_ideal; /*�����ӽ���������Ϊֹ��������Ӧ�õ���������ܳ���*/ + UINT32 pktcout; /*�����ۼƵ���İ�����, �����ش���, ACK, ������SYN */ + UINT32 totallost; /*�����ۼƶ�������*/ + UINT32 seq; /*���������ڴ���seq���*/ + UINT32 first_data_seq; /*����������ʼ�ĵ�seq���*/ + + //UINT32 ack_seq; /*�����������ʹ�õ�Ӧ���, 2017-08-02 lijia modify , �ƶ���struct tcpdetail_private */ + + UINT16 window; /*�������ݻ������ڴ�С*/ + UCHAR __pad__; + UCHAR finstate; /*fin״̬*/ + UINT16 unorder_cnt;/* 2014-11-27 lijia modify, ijЩ����������, UCHAR�Ͳ���, ��չΪUINT16 */ + UINT16 maxunorder; /* 2014-11-27 lijia modify, ijЩ����������, UCHAR�Ͳ���, ��չΪUINT16 */ + struct buf_unorder *unorderlist; /*�����������*/ + struct buf_unorder *unorderlisttail; /*�����������β��ָ��*/ +}; + +struct streaminfo_private +{ + /* ����ṹ�����ڽṹ����ǰ, ָ���ַ���Ի���ǿת */ + struct streaminfo stream_public; + /* ���±���Ϊƽ̨�ڲ�˽��, ���ⲻ�ɼ� */ + void *pproject; //ÿ�����̿����Զ���ʹ�ã� + UCHAR layer_dir:2; /* ������Ч, ��ǰ��ĵ�ַ�Ƿ��Ĭ�Ϲ���"��˿��ǿͻ���"��ͬ */ + UCHAR stream_dir:2; /* ��������������Ч, ���Ĵ洢�ĵ�ַ�Ƿ��Ĭ�Ϲ���"��˿��ǿͻ���"��ͬ */ + UCHAR addr_use_as_hash:1; /* �����addr�Ƿ���ΪHASH����ͱȽϵIJ���, ��:MAC��ַ��������� */ + UCHAR addr_skip_for_layer_cmp:1;/*�����addr�Ƿ���Ϊ��ַ�ȽϵIJ㼶���磺MPLS��ַ����Ϊ�������㣬ֱ����������ֵĬ��Ϊ0������Ҫ�Ƚ�*/ + UCHAR need_update_opposite_addr:1;/*�����addr�Ƿ��ڶԲ�����ʱ���£��磺MPLS��ǩ�ǶԳ�ʱ��Ҫ��S2C���һ������¼�����ǩ,��ֵĬ��Ϊ0��������Ҫ����*/ + UCHAR stream_killed_flag:1; /* 2014-08-22 lijia add, ����ģʽ��, �Ѿ������Kill, ֮�������ֱ��Drop��Kill, �����ٸ��ϲ��� */ + UCHAR dirreverse; /* ��������ʱ�Ƿ������ip��ַ��ת, ����"��˿��ǿͻ���"�����෴ */ + UINT16 timeout;/* ÿ�����ӵĶ��г�ʱʱ��, ��ֵ��������;, 1:���ھ�����̭��ʱ���ް��������, �Խ�Լ�ڴ�; 2:���ڱ�����ʱ���ް�����, ����IM�ʱ���ް������Ӳ�δ����, �����ýϴ��timeout */ + unsigned short offset_to_raw_pkt_hdr; /* ����ͷ�����ԭʼ������ʼ��ַ��ƫ���� */ + unsigned short offset_to_ip_hdr; /* 2015-12-07 lijia add, UDP/TCP ��ͷ����ڳ��ص�IP��ͷƫ���� */ + const raw_pkt_t *raw_pkt; /* 2014-12-30 lijia add, ��Щ�ص�������֧��ԭʼ��, ����������Ҫ, �洢��private�ṹ�� */ + unsigned int hash_slave; /* 2015-12-14 lijia add, ʹ��linux_jhash����ʱ, ����ͬʱ�õ��������ϵ�HASHֵ, ��HASH����ȷ����HASH����SLOTλ��, slave_HASH���ڿ��ٱȽϵ�ַ�Ƿ���� */ + unsigned char hash_not_head_times;/* 2015-12-15 lijia add, ��ǰindex����HASH SLOT�ĵ�һλ�Ĵ��� */ + unsigned char cur_layer_raw_hdr_len; /* 2017-10-31 lijia add, ��ǰ���ԭʼ����ַ����, ��pppͷ��ѹ��, ԭ���汾���������״̬, ����ʱҲ����֪��ַ������ */ + char __pad; + unsigned char gdev_block_timer; + unsigned short stream_low_layer_tunnel_type; /* 2016-07-25 lijia add, ��¼�����ײ���������, 0Ϊ������, �������:enum stream_carry_tunnel_t */ + unsigned short stream_carry_up_layer_tunnel_type; /* ��ǰ���ϲ����������, ���統ǰ��ΪUDP, ���صĿ�����teredo������L2TP���� */ + + /* 2016-07-08 lijia add, for janus hijack, ��Ӧ�ô洢��half_stream, �����յ�SYNʱ, ��û�д���half_streamʵ��, �����ݴ���streaminfo_private */ + unsigned short syn_opt_num; + unsigned short synack_opt_num; + struct tcp_option *syn_opt_array; + struct tcp_option *synack_opt_array; +}; + +struct tcpdetail_private +{ + /* ����ṹ�����ڽṹ����ǰ, ָ���ַ���Ի���ǿת */ + struct tcpdetail tcpdetail_public; + UCHAR multisynflag:2; // multi syn + UCHAR ignore_rst_fin:2;//������rst, fin����, ֻ����ʱ��lru���� + UCHAR needackflag:2; //��Ҫ�ϴ�ack���� + UCHAR takeoverflag:2; + UCHAR tcpstateflag; // ���ڼ�¼tcp�ĻỰSYN���״̬ + UCHAR link_state; // ���ӵ�״̬ + UCHAR creat_mod; + UINT16 tcpoverlen; // modify by lqy 20150225, ��¼��ǰ������һ�������ص�tcp���ȣ� + UINT16 pad; + struct half_tcpstream *pclient; //��client��TCP������Ϣ + struct half_tcpstream *pserver; //�� server��TCP������Ϣ + UINT32 iserverseq; //���ӽ���ʱ��ʱ�洢seq, C2S, ISN+1 + UINT32 iclientseq; //���ӽ���ʱ��ʱ�洢seq, S2C, ISN+1 + /* NOTE: + Ϊ��first_ack_seq������half_tcpstream��? + ��half_tcpstream�����յ���һ�����и��صİ��ŷ����, + �����tcp_deal_ack()����������, �����S2C����, ��ô��һ��S2C���ݰ�����ʱ, �Ѿ�����first_ack_seq��. + �����Է�ֹDDOS����. + */ + UINT32 C2S_first_ack_seq; /* 2017-08-02 lijia modify, C2S���һ��ACK��, ��half_tcpstream->ack_seqһ����Լ�����������Բ���������� */ + UINT32 C2S_ack_seq; /* 2017-08-02 lijia add, C2S�൱ǰ��ACK�� */ + UINT32 S2C_first_ack_seq; /* 2017-08-02 lijia modify, C2S���һ��ACK��, ��half_tcpstream->ack_seqһ����Լ�����������Բ���������� */ + UINT32 S2C_ack_seq; /* 2017-08-02 lijia add, S2C�൱ǰ��ACK�� */ + void *apme; //Ӧ�ò������� + void *pAllpktpme; //��״̬��tcp���������� + struct tcp_flow_stat *flow_stat; /* 2016-07-14 lijia add, ���ڼ�¼TCP��data���ļ���, ʵ���ڴ������ͷ���projectģ�����, ����ΪNULL, */ + struct tcp_flow_stat *deduce_flow_stat; /* 2018-10-30 lijia add, ���ڼ�¼������ƶϳ������ϴ��������, ���������ij���; �Լ������������, �Զ�Ӧ���յ������� */ +}; + +struct udpdetail_private +{ + /* ����ṹ�����ڽṹ����ǰ, ָ���ַ���Ի���ǿת */ + struct udpdetail udpdetail_public; + void *apme; //Ӧ�ò������� + struct udp_flow_stat *flow_stat; /* 2015-12-28 lijia add, udpdetail�е���ϸ����(64bit), ʵ���ڴ������ͷ���projectģ�����, ����ΪNULL */ +}; + +/* 2015-02-26 lijia add, for stream-addr-list ntop, pton */ +typedef struct{ + struct streaminfo stream; + char addr_value[MAX_ADDR_BIN_VALUE_LEN]; /* Ϊ��paddr������malloc, �������, ��stream������һ�黺�� */ +}addr_continuous_bin_t; + +typedef struct{ + enum addr_type_t addr_type_bin; + UCHAR stream_type;/* ����ʶ��ADDR_TYPE_IPV4��TCP����UDP */ + const char *addr_type_str; + const char *addr_type_prefix; + const char *addr_type_prefix_with_delim; /* ���ָ�����ǰ, ���ڱȽ��ַ���, ��ֹIPv4��IPv4_TCP�ַ����Ƚ�ʱ����, �����IPv4:, IPv4_TCP:�Ͳ����ڻ������� */ + int (*addr_n2p_fun)(const struct layer_addr *paddr, char *buf, int buf_len); + int (*addr_p2n_fun)(char *addr_str, addr_continuous_bin_t *addr_bin_val); +}addr_convert_t; + +struct pptp_stream_key{ + UINT32 sip; /* TCP-SYN����ԴIP, ����ǰ�data������, ����pptp->message_type�ж�, REQ���������ԴIP��Ϊ��sip */ + UINT32 dip; /* sip,dip are network order */ + UINT16 sip_side_call_id; /* �������ݰ�����sip������GRE����callid, ʵ�������ʾpeer call id, network order */ + UINT16 dip_side_call_id; /* �������ݰ�����dip������GRE����callid, ʵ�������ʾpeer call id, network order */ + //struct streaminfo_private *stream_pr; /* TODO, ���ڱ����ַ����, Ŀǰ��������ײ��IP��call_id */ +}; + +/* PPTPЭ�����������ṹ, sapp�ڲ�ʹ�� */ +struct pptp_info_pri{ + struct MESA_tunnel_info tunnel_context; + struct pptp_stream_key pptp_key; + stSessionInfo ssinfo; /* ����ҵ�����������Ϣ */ + void *biz_pme; /* ҵ����Զ������� */ + struct streaminfo *my_stream; /* pptp�ײ�UDP�� */ + unsigned char threadnum; + char insert_hash_flag; /* ���������ѽ�key����HASH�� */ + char content_notify_biz_flag; /* �Ƿ��Ѿ����ù�ҵ����, ��֮content_type */ +}; + +/* ˽��pptp��ַ, pptp_addr�ṩ��ҵ����, ����gre_layer_len����FDʱ����ʶ�𱾲�ͷ������ */ +struct layer_addr_pptp_pri{ + struct layer_addr_pptp pptp_addr; + int gre_layer_len; +}; + +struct l2tp_stream_key{ + UINT32 sip; /* L2TP������������IP, ����������SCCRQ, ICRQ, SCCCN�Ȱ���ԴIP, network order */ + UINT32 dip; /* L2TP�����������˵�IP, network order */ + UINT16 sport; /* network order */ + UINT16 dport; /* network order */ + UINT16 sip_side_tunnel_id; /* sip�ⷢ������tunnelid, network order */ + UINT16 sip_side_session_id; /* sip�ⷢ������tunnelid, network order */ + UINT16 dip_side_tunnel_id; /* dip�ⷢ������tunnelid, network order */ + UINT16 dip_side_session_id; /* dip�ⷢ������tunnelid, network order */ + //struct streaminfo_private *stream_pr; /* TODO, ���ڱ����ַ����, Ŀǰ��������ײ��IP��call_id */ +}; + +struct l2tp_info_pri{ + struct MESA_tunnel_info tunnel_context; + struct l2tp_stream_key l2tp_key; + stSessionInfo ssinfo; /* ����ҵ��������� */ + void *biz_pme; + const struct streaminfo *my_stream; /* l2tp�ײ�UDP�� */ + unsigned char thread_seq; + char content_notify_biz_flag; /* �Ƿ��Ѿ����ù�ҵ����, ��֮content_type */ +}; + +/* ȫ��Ψһ��ȫ�ֱ���, ͨ���ǿ�����������, ���仯��ȫ�ֱ���, ����������, ������ֻ�� */ +struct sapp_global_single_t{ + int signal_take_over_sw; /* �Ƿ�ӹܳ���ϵͳ�ź�, ��SIGSEGV, SIGABRT�ȵ� */ + int ipentry_priority_over_ipfrag; /* IP_entry���ȼ�����IP_frag_entry, Ĭ��Ϊ0, ��ӦWY�����ض����� */ + int tuple4_reuse_time_interval; /* ��Ԫ�����õ���С���ʱ�� */ + void *fs2_handle; + const char *fs2_server_ip; + unsigned short fs2_server_port_host; + short __pad1; + int fs2_filed_id_array[COUNTER_NUM]; + int fs2_latency_id_array[COUNTER_NUM]; + int treat_vlan_as_mac_in_mac_sw; +}; + +#define TIMESTAMP_SINGLE_PKT_REGION (4) /* ������ʱͳ������ */ +#define TIMESTAMP_SINGLE_PKT_REGION_MAX (5) /* ��������ͳ�������ֵ */ +typedef struct{ + unsigned int runtime_pkt_max_delay; /* �����������ʱ */ + unsigned long long pkt_total_num[TIMESTAMP_SINGLE_PKT_REGION_MAX]; /* ��¼��������ʱ���ܰ���, ������ͳ�� */ + unsigned long long pkt_total_time; /* ���һ��ʱ�������ʱ, ���ڼ���ƽ������������ʱ */ +}timestamp_record_region_t; +/* ���߳�ȫ�ֱ���, �������ݰ�ͳ����Ϣ, ͨ��ֵ��ʵʱ�仯 */ +struct sapp_global_mthread_t{ + int ipv6_pkt_type_flag; /* ipv6 packet type, normal, or rebuild */ + int pptp_hash_max_search_times; + int l2tp_hash_max_search_times; + char __pad1[4]; + double pptp_hash_avg_search_times; + double l2tp_hash_avg_search_times; + int tcp_stream_special_timeout_num; /* ���õ�����ʱʱ�����, Ĭ�ϲ�����������������10% */ + int udp_stream_special_timeout_num;/* ���õ�����ʱʱ�����, Ĭ�ϲ�����������������10% */ + timestamp_record_region_t runtime_record; /* ƽ̨����������ʱ */ + timestamp_record_region_t runtime_record_driver; /* �����������õ���, ��ƽ̨�õ�������ʱ */ + char __pad2__[40]; +}; + +extern struct sapp_global_single_t sapp_global_single; +extern struct sapp_global_mthread_t sapp_global_mthread[MAX_THREAD_NUM]; + +extern int G_SKIP_NOT_IP_LAYER; +extern int g_sapp_log_level; +extern void *g_sapp_log_handle; + +#define sapp_runtime_log(log_level, format, args...) do{if(log_level>=g_sapp_log_level){MESA_handle_runtime_log(g_sapp_log_handle, log_level, "sapp", format, ##args);}}while(0) + + +int MESA_kill_tcp_remedy(struct streaminfo *stream, const void *ext_raw_pkt); + +int get_stream_carry_tunnel_type(const struct streaminfo *this_stream, const struct streaminfo *upper_stream, unsigned short *tunnel_type); + +void cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt); +char biz_retval_to_platform(char biz_ret); +char plat_state_to_biz(char plat_state); +void idle_polling_call(int thread_seq); +int sapp_identify_broad_multicast_pkt(const void *this_layer_data, const raw_pkt_t *raw_pkt); +int sapp_assistant_init(void); + +static inline long long sapp_get_cpu_cycle(void) +{ +#ifdef __x86_64 +#define X86_64_ENV 1 +#endif +#ifdef __x86_64__ +#define X86_64_ENV 1 +#endif + +#ifdef X86_64_ENV + long long l; + long long h; + + __asm__ volatile("rdtsc" : "=a"(l), "=d"(h)); + return (long long )l | ((long long )h<<32); +#else + return 0; +#endif +} +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/inner_plug/sapp_assistant.cpp b/inner_plug/sapp_assistant.cpp index ba2d333..a17fc40 100644 --- a/inner_plug/sapp_assistant.cpp +++ b/inner_plug/sapp_assistant.cpp @@ -1,506 +1,536 @@ -/*
- ���ļ���ƽ̨���Ĺ��ܺͺ���,
- ���ṩ������ҵ�����Ľӿ�, ��sapp_get_platform_opt��, ��Ϊһ��.so�ļ����ص�ƽ̨,
- ���ڸ��²���.
-*/
-#include "stream_internal.h"
-#include "stream_manage.h"
-#include "sysinfo.h"
-#include "packet_io.h"
-#include "packet_io_internal.h"
-#include "project_requirement.h"
-#include "project_internal.h"
-#include "dictator.h"
-#include "MESA_handle_logger.h"
-#include "field_stat2.h"
-#include "MESA_prof_load.h"
-#include <stdio.h>
-#include <unistd.h>
-#include <assert.h>
-#include <string.h>
-#include <arpa/inet.h>
-#include <pthread.h>
-#include <sys/select.h>
-#include <sys/types.h>
-#include <linux/version.h>
-#include <dlfcn.h>
-#if IOMODE_MARSIO
-#include "marsio.h"
-#include "mrtunnat.h"
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int sapp_assistant_version_VERSION=20180722;
-
-extern time_t g_CurrentTime;//add by lqy 20070606
-extern time_t g_sapp_start_time;
-extern volatile unsigned long long g_SysInputInfo[MAX_THREAD_NUM][COUNTER_NUM];
-extern volatile unsigned long long sysinfo_new[COUNTER_NUM];
-extern int g_packet_io_thread_num;
-extern int g_packet_io_cap_mode;
-extern int g_iThreadNum;
-static void *sapp_fs_handle;
-static pthread_mutex_t g_plug_Independent_thread_mutex;
-static pthread_t g_plug_Independent_thread_pid[NON_PKT_THREAD_SEND_QUEUE];
-
-typedef struct{
- int sapp_tot_pkt_fs_id;
- int sapp_tot_bytes_fs_id;
- int sapp_active_send_fs_id;
-}sapp_fs_id_t;
-
-//static sapp_fs_id_t g_sapp_fs_id;
-static const unsigned char G_BROADCAST_ADDR[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
-
-
-void * (*dl_marsio_buff_ctrlzone)(void *m, uint8_t id);
-
-
-static int sapp_identify_broad_multicast_init(void)
-{
-#if IOMODE_MARSIO
- void *io_lib_handle;
-
- if(CAP_MODEL_MARSIOV4 == g_packet_io_cap_mode){
- io_lib_handle = dlopen("./platform_lib/packet_io_marsio.so", RTLD_NOW | RTLD_GLOBAL);
- if(NULL == io_lib_handle){
- printf("\033[1;31;40m[Error]dlopen %s error, %s!\033[0m\n", "./platform_lib/packet_io_marsio.so", dlerror());
- return -1;
- }
- dl_marsio_buff_ctrlzone = (void * (*)(void *m, uint8_t id))dlsym(io_lib_handle, "marsio_buff_ctrlzone");
- if(NULL == dl_marsio_buff_ctrlzone){
- printf("\033[1;31;40m[Error]dlsym %s error, %s!\033[0m\n", "marsio_buff_ctrlzone", dlerror());
- return -1;
- }
-
- /* �˴���Ҫ��dlclose(), ��Ϊ������Ҫ��dlopen��packet_io_marsio.so, */
- }
-#endif
- return 0;
-}
-
-static int sapp_identify_datalink_mode_is_eth(const raw_pkt_t *raw_pkt)
-{
-#if IOMODE_MARSIO
- if(CAP_MODEL_MARSIOV4 == g_packet_io_cap_mode){
- const struct mr_tunnat_ctrlzone *mr_ctzone;
- if(dl_marsio_buff_ctrlzone){
- mr_ctzone = (const struct mr_tunnat_ctrlzone *)((*dl_marsio_buff_ctrlzone)((void *)raw_pkt->io_lib_pkt_reference, 0));
- if(mr_ctzone->__encap_len > 0){
- return 0;
- }
- }
- }
-#endif
-
- return 1;
-}
-
-int sapp_identify_broad_multicast_pkt(const void *this_layer_data, const raw_pkt_t *raw_pkt)
-{
- const struct mesa_ethernet_hdr *p_eth_hdr = (const struct mesa_ethernet_hdr *)this_layer_data;
- unsigned short eth_type = p_eth_hdr->ether_type; /* ʹ��������ֱ���ж�, ����ÿ�ζ�����ntohs() */
-
- if(sapp_identify_datalink_mode_is_eth(raw_pkt) == 0){
- return 0;
- }
-
- if(memcmp(G_BROADCAST_ADDR, p_eth_hdr->ether_dhost, 6) == 0){
- /* G����ģʽ�£���ARPЭ��Ĺ㲥����ֱ�Ӷ���! ��ֹ�ٻ�ע��������, ��ɹ㲥�籩���������� */
- if(eth_type != ETH_P_ARP_NET){
- return 1;
- }
- }else{
- /* 2017-10-10 lijia add, �鲥MAC��ַ, ͨ��Ϊ�������ڿ��������ݰ�, ��LLMNR, SPT��Э��, һ�����账�� */
- if((p_eth_hdr->ether_dhost[0] & 0x01) == 0x01){
- return 1;
- }
- }
-
- return 0;
-}
-
-/* 2017-09-04 lijia add, for ��������, �����ȡƽ̨�ڲ����� */
-int sapp_get_platform_opt(enum sapp_platform_opt opt, void *opt_val, int *opt_val_len)
-{
- int i, ret = 0;
- unsigned long long tmp_long;
-
- if((NULL == opt_val) || (opt_val_len <= 0)){
- return -1;
- }
-
- switch((int)opt){
- case SPO_TOTAL_RCV_PKT:
- {
- if(*opt_val_len != sizeof(long long)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
- unsigned long long *tot_pkt = (unsigned long long *)opt_val;
- tmp_long = 0;
- for(i = 0; i < g_packet_io_thread_num; i++){
- tmp_long += g_SysInputInfo[i][PKT_ETHERNET];
- }
- *tot_pkt = tmp_long;
- }
- break;
-
- case SPO_TOTAL_RCV_BYTE:
- {
- if(*opt_val_len != sizeof(long long)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
- unsigned long long *tot_byte = (unsigned long long *)opt_val;
- tmp_long = 0;
- for(i = 0; i < g_packet_io_thread_num; i++){
- tmp_long += g_SysInputInfo[i][PKT_ETHERNET_LEN];
- }
- *tot_byte = tmp_long;
- }
- break;
-
- case SPO_THREAD_COUNT:
- {
- if(*opt_val_len != sizeof(int)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
-
- int *tcnt = (int *)opt_val;
- *tcnt = g_packet_io_thread_num;
- }
- break;
-
- case SPO_CURTIME_TIMET:
- {
- if(*opt_val_len != sizeof(time_t)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
- time_t *curtime = (time_t *)opt_val;
- *curtime = g_CurrentTime;
- }
- break;
-
- case SPO_CURTIME_STRING:
- {
- if(*opt_val_len <= (int)strlen("1970-01-01 11:11:11")){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
- time_t cur_time = g_CurrentTime;
- struct tm date_loc;
- int ret;
-
- localtime_r(&cur_time, &date_loc);
- ret = snprintf((char *)opt_val, *opt_val_len, "%04d-%02d-%02d %02d:%02d:%02d",
- date_loc.tm_year+1900, date_loc.tm_mon+1,date_loc.tm_mday,
- date_loc.tm_hour, date_loc.tm_min, date_loc.tm_sec);
- *opt_val_len = ret;
- }
- break;
-
- case SPO_START_TIME:
- {
- if(*opt_val_len != sizeof(time_t)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
- time_t *stime = (time_t *)opt_val;
- *stime = g_sapp_start_time;
- }
- break;
-
- case SPO_RUN_TIME:
- {
- if(*opt_val_len != sizeof(time_t)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
- time_t *runtime = (time_t *)opt_val;
- *runtime = g_CurrentTime - g_sapp_start_time;
- }
- break;
-
- /* �ⲿ���Ƶ������rand�Ⱥ����Ƚ�����CPU, ʹ��CPU��ǰ��ʱ��������, �͵�ǰƽ̨�����İ���ƴ��һ������� */
- case SPO_RAND_NUMBER:
- {
- if(*opt_val_len != sizeof(long long)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
-
- long long *rnum = (long long *)opt_val;
-
- *rnum = ((sapp_get_cpu_cycle() & 0xFFFF) << 48)
- | ((g_SysInputInfo[0][PKT_TCPLEN] & 0xFFFF) << 32)
- | ((g_SysInputInfo[0][PKT_IPLEN] & 0xFFFF) << 16)
- | ((g_SysInputInfo[0][PKT_ETHERNET_LEN] & 0xFFFF));
- }
- break;
-
- case SPO_FIELD_STAT_HANDLE:
- {
- if(*opt_val_len != sizeof(long long)){
- ret = -1;
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
- break;
- }
- void **out_handle = (void **)opt_val;
- *out_handle = sapp_fs_handle;
- }
- break;
-
- case SPO_INDEPENDENT_THREAD_ID:
- {
- int id_index = -1;
- int *opt_int = (int *)opt_val;
- int i;
- pthread_t this_pid = pthread_self(); /* ��ȡ��ǰ�߳�id */
-
- pthread_mutex_lock(&g_plug_Independent_thread_mutex);
-
- /* Ϊ����һ���̶߳�ε��ô˽ӿ�, �Ȳ���֮ǰ�Ƿ�� */
- for(i = 0; i < NON_PKT_THREAD_SEND_QUEUE; i++){
- if(g_plug_Independent_thread_pid[i] == this_pid){
- id_index = i;
- break;
- }
- }
-
- if(-1 == id_index){ /* ��ǰ�̵߳�һ�δ���, û�ҵ� */
- for(i = 0; i < NON_PKT_THREAD_SEND_QUEUE; i++){
- if(0 == g_plug_Independent_thread_pid[i]){
- g_plug_Independent_thread_pid[i] = this_pid;
- break;
- }
- }
- if((i+ g_iThreadNum >= NON_PKT_THREAD_SEND_QUEUE ) /* �����߳����64�� */
- || (i >= NON_PKT_THREAD_SEND_QUEUE)){
- sapp_runtime_log(RLOG_LV_FATAL, "Independent_thread num over limit, only support max %d!\n", NON_PKT_THREAD_SEND_QUEUE);
- *opt_int = -1;
- ret = -1;
- }else{
- *opt_int = g_iThreadNum + i;
- }
- }else{
- *opt_int = g_iThreadNum + id_index;
- }
-
- pthread_mutex_unlock(&g_plug_Independent_thread_mutex);
- }
- break;
-
- default:
- sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_type:%d not support!\n", (int)opt);
- ret = -1;
- }
-
- return ret;
-}
-
-#if 0
-int sapp_fileld_stat_init(void)
-{
- int opt;
- char cfg_server_ip[16];
- int cfg_server_port_host;
-
- sapp_fs_handle = FS_create_handle();
-
- FS_set_para(sapp_fs_handle, OUTPUT_DEVICE, "./log/sapp_field_stat.log", strlen("./log/sapp_field_stat.log"));
-
- opt = 1;
- FS_set_para(sapp_fs_handle, PRINT_MODE, &opt, sizeof(int));
-
- opt = 1;
- FS_set_para(sapp_fs_handle, CREATE_THREAD, &opt, sizeof(int));
-
- opt = 1;
- FS_set_para(sapp_fs_handle, STAT_CYCLE, &opt, sizeof(int));
-
- FS_set_para(sapp_fs_handle, APP_NAME, "sapp", strlen("sapp") + 1);
-
- MESA_load_profile_string_def("conf/sapp_assistant.conf", "field_stat", "remote_server_ip", cfg_server_ip, 16, "");
- if('\0' == cfg_server_ip[0]){
- printf("\033[41msapp_fileld_stat_init()->remote_server_ip error!\033[0m\n");
- return -1;
- }
- FS_set_para(sapp_fs_handle, STATS_SERVER_IP, cfg_server_ip, strlen(cfg_server_ip) + 1);
-
- MESA_load_profile_int_def("conf/sapp_assistant.conf", "field_stat", "remote_server_port", &cfg_server_port_host, 0);
- if(0 == cfg_server_port_host || cfg_server_port_host > 65535){
- printf("\033[41msapp_fileld_stat_init()->remote_server_port error!\033[0m\n");
- return -1;
- }
- FS_set_para(sapp_fs_handle, STATS_SERVER_PORT, &cfg_server_port_host, sizeof(cfg_server_port_host));
-
- g_sapp_fs_id.sapp_tot_pkt_fs_id = FS_register(sapp_fs_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ethernet_tot_pkt");
- if(g_sapp_fs_id.sapp_tot_pkt_fs_id < 0){
- printf("\033[41msapp_fileld_stat_init()->FS_register %s error!\033[0m\n", "Ethernet_tot_pkt");
- return -1;
- }
- g_sapp_fs_id.sapp_tot_bytes_fs_id = FS_register(sapp_fs_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ethernet_tot_len");
- if(g_sapp_fs_id.sapp_tot_bytes_fs_id < 0){
- printf("\033[41msapp_fileld_stat_init()->FS_register %s error!\033[0m\n", "Ethernet_tot_len");
- return -1;
- }
-
- return 0;
-}
-#endif
-
-/*
- ��Ϊsapp_fs_handle�����ʼ����, ���ܻ������������Ҫʹ��FS_registerע���µ��ֶ�,
- ��ʱ�����ȵ���FS_start(sapp_fs_handle),
- ����Ϊƽ̨����, ����֪ʲôʱ��ƽ̨����������Ѿ�ȫ����ʼ����, ����������.
-
- ʹ��һ��С����, ƽ̨��ʼ�հ���, �϶����ǿ�ʼ������.
- Ȼ���ٵ���FS_start(sapp_fs_handle);
-*/
-
-#if 0
-static void *sapp_delay_start(void *arg)
-{
- //time_t last_time;
-
- while(0 == g_SysInputInfo[0][PKT_IP]){
- usleep(100);
- }
-
- if(sapp_global_single.fs2_handle != NULL){
- FS_start(sapp_fs_handle);
- }
-
- //last_time = time(NULL);
- while(1){
- usleep(1000);
- }
-
- return NULL;
-}
-#endif
-
-void sapp_fs2_update(int field_index, unsigned long long value)
-{
- if(sapp_global_single.fs2_handle != NULL){
- FS_operate(sapp_global_single.fs2_handle, sapp_global_single.fs2_filed_id_array[field_index], 0, FS_OP_SET, (long long)value);
- }
-}
-
-static int sapp_fs2_init(void)
-{
- int module_switch;
- char cfg_ip_str[32];
- int cfg_port;
- int fs2_opt;
-
- MESA_load_profile_int_def("conf/main.conf", "ShowStatInfo", "FS_switch", &module_switch, 0);
- if(0 == module_switch){
- sapp_global_single.fs2_handle = NULL;
- return 0;
- }
-
- sapp_global_single.fs2_handle = FS_create_handle();
- if(NULL == sapp_global_single.fs2_handle){
- sapp_runtime_log(RLOG_LV_FATAL, "FS_create_handle() error: %s!\n", strerror(errno));
- return -1;
- }
-
- MESA_load_profile_string_def("conf/main.conf", "ShowStatInfo", "FS_server_ip", cfg_ip_str,32, "#");
- if('#' == cfg_ip_str[0]){
- sapp_runtime_log(RLOG_LV_FATAL, "FS_switch is enable, but not found FS_server_ip config\n");
- return -1;
- }
-
- sapp_global_single.fs2_server_ip = strdup(cfg_ip_str);
-
- MESA_load_profile_int_def("conf/main.conf", "ShowStatInfo", "FS_server_port", &cfg_port, -1);
- if(cfg_port <= 0 || cfg_port > 65535){
- sapp_runtime_log(RLOG_LV_FATAL, "FS_server_port %d invalid!\n", cfg_port);
- return -1;
- }
-
- sapp_global_single.fs2_server_port_host = (unsigned short)cfg_port;
-
- fs2_opt = 1;
- FS_set_para(sapp_global_single.fs2_handle, STAT_CYCLE, &fs2_opt, sizeof(int));
-
- fs2_opt = 1; /* 1:Rewrite ,2: Append. */
- FS_set_para(sapp_global_single.fs2_handle, PRINT_MODE, &fs2_opt, sizeof(int));
-
- MESA_load_profile_int_def("conf/main.conf", "ShowStatInfo", "FS_print_switch", &fs2_opt, 0);
- FS_set_para(sapp_global_single.fs2_handle, PRINT_TRIGGER, &fs2_opt, sizeof(int));
-
- FS_set_para(sapp_global_single.fs2_handle, OUTPUT_DEVICE, "fs2_sysinfo.log", strlen("fs2_sysinfo.log")+1);
-
- FS_set_para(sapp_global_single.fs2_handle, APP_NAME, "sapp", strlen("sapp")+1);
-
- FS_set_para(sapp_global_single.fs2_handle, STATS_SERVER_IP, (void *)sapp_global_single.fs2_server_ip, strlen(sapp_global_single.fs2_server_ip)+1);
-
- FS_set_para(sapp_global_single.fs2_handle, STATS_SERVER_PORT, &sapp_global_single.fs2_server_port_host, sizeof(short));
-
- sapp_global_single.fs2_filed_id_array[PKT_ETHERNET] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "Ethernet_pps");
- sapp_global_single.fs2_filed_id_array[PKT_ETHERNET_LEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "Ethernet_bps");
-
- sapp_global_single.fs2_filed_id_array[PKT_IP] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv4_pps");
- sapp_global_single.fs2_filed_id_array[PKT_IPLEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv4_bps");
-
- sapp_global_single.fs2_filed_id_array[PKT_IPV6] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv6_pps");
- sapp_global_single.fs2_filed_id_array[PKT_IPV6LEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv6_bps");
-
- sapp_global_single.fs2_filed_id_array[PKT_TCP] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_pps");
- sapp_global_single.fs2_filed_id_array[PKT_TCPLEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_bps");
-
- sapp_global_single.fs2_filed_id_array[PKT_UDP] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "UDP_pps");
- sapp_global_single.fs2_filed_id_array[PKT_UDPLEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "UDP_bps");
-
- sapp_global_single.fs2_filed_id_array[SYS_TCP_LINK_NEW] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_LINK_NEW");
- sapp_global_single.fs2_filed_id_array[SYS_TCP_LINK_DEL] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_LINK_DEL");
- sapp_global_single.fs2_filed_id_array[SYS_TCP_LINK_DATA] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_CONCURRENT");
-
- sapp_global_single.fs2_filed_id_array[SEND_TCP_RST] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "SEND_TCP_RST");
- sapp_global_single.fs2_filed_id_array[SEND_TCP_SYN_ACK] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "SEND_TCP_S/A");
- sapp_global_single.fs2_filed_id_array[SEND_UDP_PKT] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "SEND_UDP");
-
- FS_start(sapp_global_single.fs2_handle);
-
- return 0;
-}
-
-
-int sapp_assistant_init(void)
-{
- ///pthread_t pid;
-
- if(sapp_fs2_init() < 0){
- return -1;
- }
-
- sapp_identify_broad_multicast_init();
-
- pthread_mutex_init(&g_plug_Independent_thread_mutex, NULL);
-
- ///pthread_create(&pid, NULL, sapp_delay_start, NULL);
- ///pthread_detach(pid);
-
- return 1;
-}
-
-#ifdef __cplusplus
-}
-#endif
-
+/* + ���ļ���ƽ̨���Ĺ��ܺͺ���, + ���ṩ������ҵ�����Ľӿ�, ��sapp_get_platform_opt��, ��Ϊһ��.so�ļ����ص�ƽ̨, + ���ڸ��²���. +*/ +#include "stream_internal.h" +#include "stream_manage.h" +#include "sysinfo.h" +#include "packet_io.h" +#include "packet_io_internal.h" +#include "project_requirement.h" +#include "project_internal.h" +#include "dictator.h" +#include "MESA_handle_logger.h" +#include "field_stat2.h" +#include "MESA_prof_load.h" +#include <stdio.h> +#include <unistd.h> +#include <assert.h> +#include <string.h> +#include <arpa/inet.h> +#include <pthread.h> +#include <sys/select.h> +#include <sys/types.h> +#include <linux/version.h> +#include <dlfcn.h> +#if IOMODE_MARSIO +#include "marsio.h" +#include "mrtunnat.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +int sapp_assistant_version_VERSION=20180722; + +extern time_t g_CurrentTime;//add by lqy 20070606 +extern time_t g_sapp_start_time; +extern volatile unsigned long long g_SysInputInfo[MAX_THREAD_NUM][COUNTER_NUM]; +extern volatile unsigned long long sysinfo_new[COUNTER_NUM]; +extern int g_packet_io_thread_num; +extern int g_packet_io_cap_mode; +extern int g_iThreadNum; +static void *sapp_fs_handle; +static pthread_mutex_t g_plug_Independent_thread_mutex; +static pthread_t g_plug_Independent_thread_pid[NON_PKT_THREAD_SEND_QUEUE]; + +typedef struct{ + int sapp_tot_pkt_fs_id; + int sapp_tot_bytes_fs_id; + int sapp_active_send_fs_id; +}sapp_fs_id_t; + +//static sapp_fs_id_t g_sapp_fs_id; +static const unsigned char G_BROADCAST_ADDR[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + +void * (*dl_marsio_buff_ctrlzone)(void *m, uint8_t id); + + +static int sapp_identify_broad_multicast_init(void) +{ +#if IOMODE_MARSIO + void *io_lib_handle; + + if(CAP_MODEL_MARSIOV4 == g_packet_io_cap_mode){ + io_lib_handle = dlopen("./platform_lib/packet_io_marsio.so", RTLD_NOW | RTLD_GLOBAL); + if(NULL == io_lib_handle){ + printf("\033[1;31;40m[Error]dlopen %s error, %s!\033[0m\n", "./platform_lib/packet_io_marsio.so", dlerror()); + return -1; + } + dl_marsio_buff_ctrlzone = (void * (*)(void *m, uint8_t id))dlsym(io_lib_handle, "marsio_buff_ctrlzone"); + if(NULL == dl_marsio_buff_ctrlzone){ + printf("\033[1;31;40m[Error]dlsym %s error, %s!\033[0m\n", "marsio_buff_ctrlzone", dlerror()); + return -1; + } + + /* �˴���Ҫ��dlclose(), ��Ϊ������Ҫ��dlopen��packet_io_marsio.so, */ + } +#endif + return 0; +} + +static int sapp_identify_datalink_mode_is_eth(const raw_pkt_t *raw_pkt) +{ +#if IOMODE_MARSIO + if(CAP_MODEL_MARSIOV4 == g_packet_io_cap_mode){ + const struct mr_tunnat_ctrlzone *mr_ctzone; + if(dl_marsio_buff_ctrlzone){ + mr_ctzone = (const struct mr_tunnat_ctrlzone *)((*dl_marsio_buff_ctrlzone)((void *)raw_pkt->io_lib_pkt_reference, 0)); + if(mr_ctzone->__encap_len > 0){ + return 0; + } + } + } +#endif + + return 1; +} + +int sapp_identify_broad_multicast_pkt(const void *this_layer_data, const raw_pkt_t *raw_pkt) +{ + const struct mesa_ethernet_hdr *p_eth_hdr = (const struct mesa_ethernet_hdr *)this_layer_data; + unsigned short eth_type = p_eth_hdr->ether_type; /* ʹ��������ֱ���ж�, ����ÿ�ζ�����ntohs() */ + + if(sapp_identify_datalink_mode_is_eth(raw_pkt) == 0){ + return 0; + } + + if(memcmp(G_BROADCAST_ADDR, p_eth_hdr->ether_dhost, 6) == 0){ + /* G����ģʽ�£���ARPЭ��Ĺ㲥����ֱ�Ӷ���! ��ֹ�ٻ�ע��������, ��ɹ㲥�籩���������� */ + if(eth_type != ETH_P_ARP_NET){ + return 1; + } + }else{ + /* 2017-10-10 lijia add, �鲥MAC��ַ, ͨ��Ϊ�������ڿ��������ݰ�, ��LLMNR, SPT��Э��, һ�����账�� */ + if((p_eth_hdr->ether_dhost[0] & 0x01) == 0x01){ + return 1; + } + } + + return 0; +} + +/* 2017-09-04 lijia add, for ��������, �����ȡƽ̨�ڲ����� */ +int sapp_get_platform_opt(enum sapp_platform_opt opt, void *opt_val, int *opt_val_len) +{ + int i, ret = 0; + unsigned long long tmp_long; + + if((NULL == opt_val) || (opt_val_len <= 0)){ + return -1; + } + + switch((int)opt){ + case SPO_TOTAL_RCV_PKT: + { + if(*opt_val_len != sizeof(long long)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + unsigned long long *tot_pkt = (unsigned long long *)opt_val; + tmp_long = 0; + for(i = 0; i < g_packet_io_thread_num; i++){ + tmp_long += g_SysInputInfo[i][PKT_ETHERNET]; + } + *tot_pkt = tmp_long; + } + break; + + case SPO_TOTAL_RCV_BYTE: + { + if(*opt_val_len != sizeof(long long)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + unsigned long long *tot_byte = (unsigned long long *)opt_val; + tmp_long = 0; + for(i = 0; i < g_packet_io_thread_num; i++){ + tmp_long += g_SysInputInfo[i][PKT_ETHERNET_LEN]; + } + *tot_byte = tmp_long; + } + break; + + case SPO_THREAD_COUNT: + { + if(*opt_val_len != sizeof(int)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + + int *tcnt = (int *)opt_val; + *tcnt = g_packet_io_thread_num; + } + break; + + case SPO_CURTIME_TIMET: + { + if(*opt_val_len != sizeof(time_t)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + time_t *curtime = (time_t *)opt_val; + *curtime = g_CurrentTime; + } + break; + + case SPO_CURTIME_STRING: + { + if(*opt_val_len <= (int)strlen("1970-01-01 11:11:11")){ + + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + time_t cur_time = g_CurrentTime; + struct tm date_loc; + int ret; + + localtime_r(&cur_time, &date_loc); + ret = snprintf((char *)opt_val, *opt_val_len, "%04d-%02d-%02d %02d:%02d:%02d", + date_loc.tm_year+1900, date_loc.tm_mon+1,date_loc.tm_mday, + date_loc.tm_hour, date_loc.tm_min, date_loc.tm_sec); + *opt_val_len = ret; + } + break; + + case SPO_START_TIME: + { + if(*opt_val_len != sizeof(time_t)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + time_t *stime = (time_t *)opt_val; + *stime = g_sapp_start_time; + } + break; + + case SPO_RUN_TIME: + { + if(*opt_val_len != sizeof(time_t)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + time_t *runtime = (time_t *)opt_val; + *runtime = g_CurrentTime - g_sapp_start_time; + } + break; + + /* �ⲿ���Ƶ������rand�Ⱥ����Ƚ�����CPU, ʹ��CPU��ǰ��ʱ��������, �͵�ǰƽ̨�����İ���ƴ��һ������� */ + case SPO_RAND_NUMBER: + { + if(*opt_val_len != sizeof(long long)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + + long long *rnum = (long long *)opt_val; + + *rnum = ((sapp_get_cpu_cycle() & 0xFFFF) << 48) + | ((g_SysInputInfo[0][PKT_TCPLEN] & 0xFFFF) << 32) + | ((g_SysInputInfo[0][PKT_IPLEN] & 0xFFFF) << 16) + | ((g_SysInputInfo[0][PKT_ETHERNET_LEN] & 0xFFFF)); + } + break; + + case SPO_FIELD_STAT_HANDLE: + { + if(*opt_val_len != sizeof(long long)){ + ret = -1; + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len); + break; + } + void **out_handle = (void **)opt_val; + *out_handle = sapp_fs_handle; + } + break; + + case SPO_INDEPENDENT_THREAD_ID: + { + int id_index = -1; + int *opt_int = (int *)opt_val; + int i; + pthread_t this_pid = pthread_self(); /* ��ȡ��ǰ�߳�id */ + + pthread_mutex_lock(&g_plug_Independent_thread_mutex); + + /* Ϊ����һ���̶߳�ε��ô˽ӿ�, �Ȳ���֮ǰ�Ƿ�� */ + for(i = 0; i < NON_PKT_THREAD_SEND_QUEUE; i++){ + if(g_plug_Independent_thread_pid[i] == this_pid){ + id_index = i; + break; + } + } + + if(-1 == id_index){ /* ��ǰ�̵߳�һ�δ���, û�ҵ� */ + for(i = 0; i < NON_PKT_THREAD_SEND_QUEUE; i++){ + if(0 == g_plug_Independent_thread_pid[i]){ + g_plug_Independent_thread_pid[i] = this_pid; + break; + } + } + if((i+ g_iThreadNum >= NON_PKT_THREAD_SEND_QUEUE ) /* �����߳����64�� */ + || (i >= NON_PKT_THREAD_SEND_QUEUE)){ + sapp_runtime_log(RLOG_LV_FATAL, "Independent_thread num over limit, only support max %d!\n", NON_PKT_THREAD_SEND_QUEUE); + *opt_int = -1; + ret = -1; + }else{ + *opt_int = g_iThreadNum + i; + } + }else{ + *opt_int = g_iThreadNum + id_index; + } + + pthread_mutex_unlock(&g_plug_Independent_thread_mutex); + } + break; + + default: + sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_type:%d not support!\n", (int)opt); + ret = -1; + } + + return ret; +} + +#if 0 +int sapp_fileld_stat_init(void) +{ + int opt; + char cfg_server_ip[16]; + int cfg_server_port_host; + + sapp_fs_handle = FS_create_handle(); + + FS_set_para(sapp_fs_handle, OUTPUT_DEVICE, "./log/sapp_field_stat.log", strlen("./log/sapp_field_stat.log")); + + opt = 1; + FS_set_para(sapp_fs_handle, PRINT_MODE, &opt, sizeof(int)); + + opt = 1; + FS_set_para(sapp_fs_handle, CREATE_THREAD, &opt, sizeof(int)); + + opt = 1; + FS_set_para(sapp_fs_handle, STAT_CYCLE, &opt, sizeof(int)); + + FS_set_para(sapp_fs_handle, APP_NAME, "sapp", strlen("sapp") + 1); + + MESA_load_profile_string_def("conf/sapp_assistant.conf", "field_stat", "remote_server_ip", cfg_server_ip, 16, ""); + if('\0' == cfg_server_ip[0]){ + printf("\033[41msapp_fileld_stat_init()->remote_server_ip error!\033[0m\n"); + return -1; + } + FS_set_para(sapp_fs_handle, STATS_SERVER_IP, cfg_server_ip, strlen(cfg_server_ip) + 1); + + MESA_load_profile_int_def("conf/sapp_assistant.conf", "field_stat", "remote_server_port", &cfg_server_port_host, 0); + if(0 == cfg_server_port_host || cfg_server_port_host > 65535){ + printf("\033[41msapp_fileld_stat_init()->remote_server_port error!\033[0m\n"); + return -1; + } + FS_set_para(sapp_fs_handle, STATS_SERVER_PORT, &cfg_server_port_host, sizeof(cfg_server_port_host)); + + g_sapp_fs_id.sapp_tot_pkt_fs_id = FS_register(sapp_fs_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ethernet_tot_pkt"); + if(g_sapp_fs_id.sapp_tot_pkt_fs_id < 0){ + printf("\033[41msapp_fileld_stat_init()->FS_register %s error!\033[0m\n", "Ethernet_tot_pkt"); + return -1; + } + g_sapp_fs_id.sapp_tot_bytes_fs_id = FS_register(sapp_fs_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ethernet_tot_len"); + if(g_sapp_fs_id.sapp_tot_bytes_fs_id < 0){ + printf("\033[41msapp_fileld_stat_init()->FS_register %s error!\033[0m\n", "Ethernet_tot_len"); + return -1; + } + + return 0; +} +#endif + +/* + ��Ϊsapp_fs_handle�����ʼ����, ���ܻ������������Ҫʹ��FS_registerע���µ��ֶ�, + ��ʱ�����ȵ���FS_start(sapp_fs_handle), + ����Ϊƽ̨����, ����֪ʲôʱ��ƽ̨����������Ѿ�ȫ����ʼ����, ����������. + + ʹ��һ��С����, ƽ̨��ʼ�հ���, �϶����ǿ�ʼ������. + Ȼ���ٵ���FS_start(sapp_fs_handle); +*/ + +#if 0 +static void *sapp_delay_start(void *arg) +{ + //time_t last_time; + + while(0 == g_SysInputInfo[0][PKT_IP]){ + usleep(100); + } + + if(sapp_global_single.fs2_handle != NULL){ + FS_start(sapp_fs_handle); + } + + //last_time = time(NULL); + while(1){ + usleep(1000); + } + + return NULL; +} +#endif + +void sapp_fs2_update(int field_index, unsigned long long value) +{ + if(sapp_global_single.fs2_handle != NULL){ + FS_operate(sapp_global_single.fs2_handle, sapp_global_single.fs2_filed_id_array[field_index], 0, FS_OP_SET, (long long)value); + } +} + +static int sapp_fs2_init(void) +{ + int module_switch; + char cfg_ip_str[32]; + int cfg_port; + int fs2_opt; + int cycle; + + MESA_load_profile_int_def("conf/main.conf", "ShowStatInfo", "FS_switch", &module_switch, 0); + if(0 == module_switch){ + sapp_global_single.fs2_handle = NULL; + return 0; + } + + sapp_global_single.fs2_handle = FS_create_handle(); + if(NULL == sapp_global_single.fs2_handle){ + sapp_runtime_log(RLOG_LV_FATAL, "FS_create_handle() error: %s!\n", strerror(errno)); + return -1; + } + + MESA_load_profile_string_def("conf/main.conf", "ShowStatInfo", "FS_server_ip", cfg_ip_str,32, "#"); + if('#' == cfg_ip_str[0]){ + sapp_runtime_log(RLOG_LV_FATAL, "FS_switch is enable, but not found FS_server_ip config\n"); + return -1; + } + + sapp_global_single.fs2_server_ip = strdup(cfg_ip_str); + + MESA_load_profile_int_def("conf/main.conf", "ShowStatInfo", "FS_server_port", &cfg_port, -1); + if(cfg_port <= 0 || cfg_port > 65535){ + sapp_runtime_log(RLOG_LV_FATAL, "FS_server_port %d invalid!\n", cfg_port); + return -1; + } + + MESA_load_profile_int_def("conf/main.conf", "ShowStatInfo", "FS_cycle", &cycle, 3); + + sapp_global_single.fs2_server_port_host = (unsigned short)cfg_port; + + fs2_opt = 1; + FS_set_para(sapp_global_single.fs2_handle, STAT_CYCLE, &fs2_opt, sizeof(int)); + + fs2_opt = 1; /* 1:Rewrite ,2: Append. */ + FS_set_para(sapp_global_single.fs2_handle, PRINT_MODE, &fs2_opt, sizeof(int)); + + MESA_load_profile_int_def("conf/main.conf", "ShowStatInfo", "FS_print_switch", &fs2_opt, 0); + FS_set_para(sapp_global_single.fs2_handle, PRINT_TRIGGER, &fs2_opt, sizeof(int)); + + FS_set_para(sapp_global_single.fs2_handle, OUTPUT_DEVICE, "fs2_sysinfo.log", strlen("fs2_sysinfo.log")+1); + + FS_set_para(sapp_global_single.fs2_handle, APP_NAME, "sapp", strlen("sapp")+1); + + FS_set_para(sapp_global_single.fs2_handle, STATS_SERVER_IP, (void *)sapp_global_single.fs2_server_ip, strlen(sapp_global_single.fs2_server_ip)+1); + + FS_set_para(sapp_global_single.fs2_handle, STATS_SERVER_PORT, &sapp_global_single.fs2_server_port_host, sizeof(short)); + + sapp_global_single.fs2_filed_id_array[PKT_ETHERNET] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ethernet_pps"); + sapp_global_single.fs2_filed_id_array[PKT_ETHERNET_LEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ethernet_bps"); + + sapp_global_single.fs2_filed_id_array[PKT_IP] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv4_pps"); + sapp_global_single.fs2_filed_id_array[PKT_IPLEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv4_bps"); + + sapp_global_single.fs2_filed_id_array[PKT_IPV6] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv6_pps"); + sapp_global_single.fs2_filed_id_array[PKT_IPV6LEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "IPv6_bps"); + + sapp_global_single.fs2_filed_id_array[PKT_TCP] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_pps"); + sapp_global_single.fs2_filed_id_array[PKT_TCPLEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_bps"); + + sapp_global_single.fs2_filed_id_array[PKT_UDP] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "UDP_pps"); + sapp_global_single.fs2_filed_id_array[PKT_UDPLEN] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "UDP_bps"); + + sapp_global_single.fs2_filed_id_array[SYS_TCP_LINK_NEW] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_LINK_NEW"); + sapp_global_single.fs2_filed_id_array[SYS_TCP_LINK_DEL] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_LINK_DEL"); + sapp_global_single.fs2_filed_id_array[SYS_TCP_LINK_DATA] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_CONCURRENT"); + + sapp_global_single.fs2_filed_id_array[LINK_DOUBLE] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, + FS_CALC_CURRENT, "TCP_LINK_DOUBLE"); + sapp_global_single.fs2_filed_id_array[LINK_SINGLE_C2S] = FS_register(sapp_global_single.fs2_handle, + FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_LINK_C2S"); + sapp_global_single.fs2_filed_id_array[LINK_SINGLE_S2C] = FS_register(sapp_global_single.fs2_handle, + FS_STYLE_FIELD, FS_CALC_CURRENT, "TCP_LINK_S2C"); + + + sapp_global_single.fs2_filed_id_array[SEND_TCP_RST] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "SEND_TCP_RST"); + sapp_global_single.fs2_filed_id_array[SEND_TCP_SYN_ACK] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "SEND_TCP_S/A"); + sapp_global_single.fs2_filed_id_array[SEND_UDP_PKT] = FS_register(sapp_global_single.fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "SEND_UDP"); + + for(int i = 0; i < g_packet_io_thread_num; i++) + { + char histogram_name[16]; + sprintf(histogram_name, "tid_%d", i); + sapp_global_single.fs2_latency_id_array[i] = FS_register_histogram(sapp_global_single.fs2_handle, //Field Stat��� + FS_CALC_SPEED, //����ۼ�ֵ��˲ʱֵ + histogram_name, //ͳ�������ƣ��ַ��� + 1, //���ٵ���Сֵ + 100000000, //���ٵ����ֵ + 2); //���ȣ���С�����λ����Χ1~4 + + } + FS_start(sapp_global_single.fs2_handle); + + return 0; +} + +void sapp_fs2_set_latency(int thead_seq, long long time_cost) +{ + if(sapp_global_single.fs2_handle != NULL){ + FS_operate(sapp_global_single.fs2_handle, sapp_global_single.fs2_latency_id_array[thead_seq], 0, FS_OP_SET, time_cost); + } +} + +int sapp_assistant_init(void) +{ + ///pthread_t pid; + + if(sapp_fs2_init() < 0){ + return -1; + } + + sapp_identify_broad_multicast_init(); + + pthread_mutex_init(&g_plug_Independent_thread_mutex, NULL); + + ///pthread_create(&pid, NULL, sapp_delay_start, NULL); + ///pthread_detach(pid); + + return 1; +} + +#ifdef __cplusplus +} +#endif + diff --git a/packet_io/packet_io.c b/packet_io/packet_io.c index d110a05..9bee7c7 100644 --- a/packet_io/packet_io.c +++ b/packet_io/packet_io.c @@ -1,999 +1,1001 @@ -#include "common_io_dll.h"
-#include "packet_io.h"
-#include "packet_io_internal.h"
-#include "stream_manage.h"
-#include "stream_internal.h"
-#include "mesa_net.h"
-#include "sendpacket.h"
-#include "iknow.h"
-#include <MESA/MESA_handle_logger.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <assert.h>
-#include <errno.h>
-#include <pthread.h>
-#include <sched.h>
-#include "MESA_feedback.h"
-
-/*
-2015-04-15 , TODO
- ƽ̨ʵʱ������������ݰ�, ��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
-
-
-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���õ� */
-
-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_packet_io_ipv6_switch;
-int g_packet_io_ipv6_raw_socket = 0; /* ���ڷ��ʹ�Ipv6 rst�� */
-extern int g_timestamp_record_sw;
-int g_encapsulate_with_ddp = 1; /* ʹ��DDPЭ���װ��ԭʼ�� */
- /*
- 2015-12-10 lijia add, ����eth��, ���账��MAC��ַ, ���贴����ʱCOPY�����ṹ�� ,
- ԭʼ����IP�㿪ʼ.
- */
-int g_skip_ethernet_layer_sw = 1;
-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);
-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]);
-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);
-
-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_packet_io_ipv6_switch = op_switch;
- return 0;
-}
-
-int packet_io_set_ipv6_raw_socket_enable(int op_switch)
-{
- g_packet_io_ipv6_raw_socket = 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");
- }
-
- 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");
- }
-
- 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;
- 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_NET == 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;
-}
-
-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, timecost;
-
- before_call = sapp_get_cpu_cycle();
-#if CYCLE_PKT_DUMP
- cycle_pkt_dump(thread_num, p_raw_pkt);
-#endif
-
-#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)){
- ret = packet_io_strip_ddp_hdr(p_raw_pkt, dir, thread_num);
- }else{
- 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();
- if(after_call > before_call){ //cross second
- timecost = after_call - before_call;
- timestamp_region_update(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){
- ret = g_platform_action_cb_fun(g_topology_config_raw_mode, ret);
- }
-
- return ret;
-}
-
-
-void packet_io_exit(void)
-{
- /* ����ģʽ��, �������߳�һЩʱ��, ����δ�������, ����MESA_tcp�첽�������ݵȵ�. */
- 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 == SAPP_AS_TARGET_SO)
- if(CAP_MODEL_PCAP_DUMPFILE == g_packet_io_cap_mode){
- dl_io_fun_list.dl_io_register_exit_cb(packet_io_exit);
- }
-#endif
-
- 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");
- }
-
- packet_io_status_init();
-
- packet_io_device_alias_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(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);
- }
-
- app_function_rationality_check();
-
- pthread_create(&pid, NULL, time_event_thread ,NULL);
-
- dl_io_fun_list.dl_io_run();
-
- while(1){
- pause();
- }
-}
-
-extern volatile int update_packet_io_status_sw;
-void packet_io_clean_thread_context(int thread_seq)
-{
- static volatile int exit_thread_num = 0;
-
- while(++exit_thread_num < g_packet_io_thread_num){
- usleep(10);
- }
-
- if(0 == thread_seq){
- 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_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;
-
- switch(g_packet_io_cap_level){
- case CAP_LEVEL_IPV4:
- offset = 0;
- break;
- case CAP_LEVEL_MAC:
- offset = sizeof(struct mesa_ethernet_hdr);
- break;
-
- default:
- sapp_runtime_log(RLOG_LV_FATAL, "MESA_kill_tcp: Invalid cap_mode:%d\n", g_packet_io_cap_level);
- //exit(1);
- 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;
-
- ret = sendto(send_handle->raw_ipv4_fd, actual_data_ptr, datalen, 0,
- (struct sockaddr *)&sock_addr_v4, sizeof(struct sockaddr));
- if(ret < 0){
- 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;
-
- if(0 == g_packet_io_ipv6_raw_socket){
- printf("IPv6 module is not support! Please check 'main.conf->IPv6_raw_socket'.\n");
- sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: IPv6 module is not support! Please check 'main.conf->IPv6_raw_socket'.\n", __FILE__, __LINE__);
- return -1;
- }
-
- /* ʹ��ϵͳ·��ʱ, MAC��ַ��Э��ջ���, ָ��ͳ���Ҫ��ȥETHͷ�� */
- actual_data_ptr = (char *)send_handle->send_buf + sizeof(struct mesa_ethernet_hdr);
- datalen -= sizeof(struct mesa_ethernet_hdr);
-
- 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);
-
- ret = sendto(send_handle->raw_ipv6_fd, actual_data_ptr, datalen, 0,
- (struct sockaddr *)&sock_addr_v6, sizeof(struct sockaddr_in6));
- if(ret < 0){
- 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_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
-
- sendpacket_build_ethernet(g_send_gateway_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){
- 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);
- }
-
- 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;
-}
-
-#if 0
-static int parse_send_raw_pkt_conf(void)
-{
- FILE *fp;
- char conf_buf[1024];
- const char *delim = "\t ";
- char *saveptr;
- char *section;
-
- fp = fopen("conf/send_raw_pkt.conf", "r");
- if(NULL == fp){
- printf("open %s error!\n", "send_raw_pkt.conf");
- return -1;
- }
-
- 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);
-
- /* target_id */
- section = strtok_r(conf_buf, delim, &saveptr);
- if(NULL == section){
- return -1;
- }
- G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].target_id = strtoul(section, NULL, 10);
-
- /* PAG args, net_card name */
- section = strtok_r(NULL, delim, &saveptr);
- if(NULL == section){
- return -1;
- }
- memcpy(G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_PAG].dev_name, section, 128);
-
- /* pcap args, device name */
- section = strtok_r(NULL, delim, &saveptr);
- if(NULL == section){
- return -1;
- }
- memcpy(G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_PCAP_ONLINE].dev_name, section, 128);
-
- /* PFRING args, net_card index */
- section = strtok_r(NULL, delim, &saveptr);
- if(NULL == section){
- return -1;
- }
- memcpy(G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_PFRING].dev_name, section, 128);
-
- /* DPDK args, port number */
- section = strtok_r(NULL, delim, &saveptr);
- if(NULL == section){
- return -1;
- }
- G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_DPDK].port_id = strtoul(section, NULL, 10);
-
- /* TODO, PPF args, port number */
- section = strtok_r(NULL, delim, &saveptr);
-
- /* TODO, PPF args, port number */
- section = strtok_r(NULL, delim, &saveptr);
-
- /* TODO, npacket args, port number */
- section = strtok_r(NULL, delim, &saveptr);
-
- /* TODO, qnf args, port number */
- section = strtok_r(NULL, delim, &saveptr);
-
- /* TODO, n95 args, port number */
- section = strtok_r(NULL, delim, &saveptr);
-
- while(strtok_r(NULL, delim, &saveptr)); /* clear line buf */
-
- G_SND_RAW_PKT_INFO_NUM++;
- }
-
- fclose(fp);
-
- return 0;
-}
-#endif
-
-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
-
-
+#include "common_io_dll.h" +#include "packet_io.h" +#include "packet_io_internal.h" +#include "stream_manage.h" +#include "stream_internal.h" +#include "mesa_net.h" +#include "sendpacket.h" +#include "iknow.h" +#include <MESA/MESA_handle_logger.h> +#include <stdio.h> +#include <unistd.h> +#include <assert.h> +#include <errno.h> +#include <pthread.h> +#include <sched.h> +#include "MESA_feedback.h" + +/* +2015-04-15 , TODO + ƽ̨ʵʱ������������ݰ�, ��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 + + +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���õ� */ + +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_packet_io_ipv6_switch; +int g_packet_io_ipv6_raw_socket = 0; /* ���ڷ��ʹ�Ipv6 rst�� */ +extern int g_timestamp_record_sw; +int g_encapsulate_with_ddp = 1; /* ʹ��DDPЭ���װ��ԭʼ�� */ + /* + 2015-12-10 lijia add, ����eth��, ���账��MAC��ַ, ���贴����ʱCOPY�����ṹ�� , + ԭʼ����IP�㿪ʼ. + */ +int g_skip_ethernet_layer_sw = 1; +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); +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]); +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_packet_io_ipv6_switch = op_switch; + return 0; +} + +int packet_io_set_ipv6_raw_socket_enable(int op_switch) +{ + g_packet_io_ipv6_raw_socket = 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"); + } + + 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"); + } + + 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; + 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_NET == 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; +} + +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, timecost; + + before_call = sapp_get_cpu_cycle(); +#if CYCLE_PKT_DUMP + cycle_pkt_dump(thread_num, p_raw_pkt); +#endif + +#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)){ + ret = packet_io_strip_ddp_hdr(p_raw_pkt, dir, thread_num); + }else{ + 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(); + if(after_call > before_call){ //cross second + timecost = after_call - before_call; + 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){ + ret = g_platform_action_cb_fun(g_topology_config_raw_mode, ret); + } + + return ret; +} + + +void packet_io_exit(void) +{ + /* ����ģʽ��, �������߳�һЩʱ��, ����δ�������, ����MESA_tcp�첽�������ݵȵ�. */ + 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 == SAPP_AS_TARGET_SO) + if(CAP_MODEL_PCAP_DUMPFILE == g_packet_io_cap_mode){ + dl_io_fun_list.dl_io_register_exit_cb(packet_io_exit); + } +#endif + + 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"); + } + + packet_io_status_init(); + + packet_io_device_alias_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(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); + } + + app_function_rationality_check(); + + pthread_create(&pid, NULL, time_event_thread ,NULL); + + dl_io_fun_list.dl_io_run(); + + while(1){ + pause(); + } +} + +extern volatile int update_packet_io_status_sw; +void packet_io_clean_thread_context(int thread_seq) +{ + static volatile int exit_thread_num = 0; + + while(++exit_thread_num < g_packet_io_thread_num){ + usleep(10); + } + + if(0 == thread_seq){ + 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_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; + + switch(g_packet_io_cap_level){ + case CAP_LEVEL_IPV4: + offset = 0; + break; + case CAP_LEVEL_MAC: + offset = sizeof(struct mesa_ethernet_hdr); + break; + + default: + sapp_runtime_log(RLOG_LV_FATAL, "MESA_kill_tcp: Invalid cap_mode:%d\n", g_packet_io_cap_level); + //exit(1); + 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; + + ret = sendto(send_handle->raw_ipv4_fd, actual_data_ptr, datalen, 0, + (struct sockaddr *)&sock_addr_v4, sizeof(struct sockaddr)); + if(ret < 0){ + 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; + + if(0 == g_packet_io_ipv6_raw_socket){ + printf("IPv6 module is not support! Please check 'main.conf->IPv6_raw_socket'.\n"); + sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: IPv6 module is not support! Please check 'main.conf->IPv6_raw_socket'.\n", __FILE__, __LINE__); + return -1; + } + + /* ʹ��ϵͳ·��ʱ, MAC��ַ��Э��ջ���, ָ��ͳ���Ҫ��ȥETHͷ�� */ + actual_data_ptr = (char *)send_handle->send_buf + sizeof(struct mesa_ethernet_hdr); + datalen -= sizeof(struct mesa_ethernet_hdr); + + 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); + + ret = sendto(send_handle->raw_ipv6_fd, actual_data_ptr, datalen, 0, + (struct sockaddr *)&sock_addr_v6, sizeof(struct sockaddr_in6)); + if(ret < 0){ + 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_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 + + sendpacket_build_ethernet(g_send_gateway_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){ + 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); + } + + 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; +} + +#if 0 +static int parse_send_raw_pkt_conf(void) +{ + FILE *fp; + char conf_buf[1024]; + const char *delim = "\t "; + char *saveptr; + char *section; + + fp = fopen("conf/send_raw_pkt.conf", "r"); + if(NULL == fp){ + printf("open %s error!\n", "send_raw_pkt.conf"); + return -1; + } + + 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); + + /* target_id */ + section = strtok_r(conf_buf, delim, &saveptr); + if(NULL == section){ + return -1; + } + G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].target_id = strtoul(section, NULL, 10); + + /* PAG args, net_card name */ + section = strtok_r(NULL, delim, &saveptr); + if(NULL == section){ + return -1; + } + memcpy(G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_PAG].dev_name, section, 128); + + /* pcap args, device name */ + section = strtok_r(NULL, delim, &saveptr); + if(NULL == section){ + return -1; + } + memcpy(G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_PCAP_ONLINE].dev_name, section, 128); + + /* PFRING args, net_card index */ + section = strtok_r(NULL, delim, &saveptr); + if(NULL == section){ + return -1; + } + memcpy(G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_PFRING].dev_name, section, 128); + + /* DPDK args, port number */ + section = strtok_r(NULL, delim, &saveptr); + if(NULL == section){ + return -1; + } + G_SND_RAW_PKT_INFO[G_SND_RAW_PKT_INFO_NUM].send_args[CAP_MODEL_DPDK].port_id = strtoul(section, NULL, 10); + + /* TODO, PPF args, port number */ + section = strtok_r(NULL, delim, &saveptr); + + /* TODO, PPF args, port number */ + section = strtok_r(NULL, delim, &saveptr); + + /* TODO, npacket args, port number */ + section = strtok_r(NULL, delim, &saveptr); + + /* TODO, qnf args, port number */ + section = strtok_r(NULL, delim, &saveptr); + + /* TODO, n95 args, port number */ + section = strtok_r(NULL, delim, &saveptr); + + while(strtok_r(NULL, delim, &saveptr)); /* clear line buf */ + + G_SND_RAW_PKT_INFO_NUM++; + } + + fclose(fp); + + return 0; +} +#endif + +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 + + diff --git a/packet_io/packet_io_marsio.c b/packet_io/packet_io_marsio.c index 7f9e0ca..4271020 100644 --- a/packet_io/packet_io_marsio.c +++ b/packet_io/packet_io_marsio.c @@ -231,7 +231,7 @@ unsigned char *dl_io_get_sendbuf(void *phandle, int thread_seq) if(ret < 0){ return NULL; } - + g_SysInputInfo[thread_index][PKT_MARSIO_MALLOC]++; data_buf = (unsigned char *)marsio_buff_mtod(mr4_handle->send_mbuf[0]); #else /* ����ʱ, ��Ҫmbuf�ṹ, ���˺����ӿ���ֱ�ӷ���mbuf, ���ǵ���marsio_buff_mtod()֮�����ʵ���ݻ�����, ��ʱʹ��malloc */ @@ -249,6 +249,7 @@ void dl_io_free_sendbuf(void *phandle, int thread_seq) marsio4_io_handle *mr4_handle = (marsio4_io_handle *)phandle; marsio_buff_free(sapp_marsio4_instance, mr4_handle->send_mbuf, 1, -1, thread_seq); + g_SysInputInfo[tid][PKT_MARSIO_FREE]++; #else free(mr4_handle->send_mbuf_mtod_ptr); #endif @@ -509,6 +510,7 @@ static int marsio4_process_packet(int tid, raw_pkt_t *raw_pkt) for(i = 0; i < g_mr4_device_num; i++){ rcv_res = marsio_recv_burst(g_mr4_dev_up_handle_set[i], tid, rx_buff, marsio4_burst_process_pkt_num); if(rcv_res > 0){ + g_SysInputInfo[tid][PKT_MARSIO_RCV]+=(unsigned long long)rcv_res; for(j = 0; j < rcv_res; j++){ marsio4_pkt_hand(tid, rx_buff[j], raw_pkt, DIR_ROUTE_UP); /* ����ģʽ�����ķ���ֵ */ @@ -516,6 +518,8 @@ static int marsio4_process_packet(int tid, raw_pkt_t *raw_pkt) } pkt_num += rcv_res; marsio_buff_free(sapp_marsio4_instance, rx_buff, rcv_res, MARSIO_SOCKET_ID_ANY, tid); /* �������漰����, ������ҵ����������free */ + g_SysInputInfo[tid][PKT_MARSIO_FREE]+=rcv_res; + } } }else if(NET_CONN_SERIAL_GDEV == g_marsio4_topology_mode){ @@ -561,28 +565,34 @@ static int marsio4_process_packet(int tid, raw_pkt_t *raw_pkt) /* ˫��������ģʽ��, Ϊ�˷�ֹ�������������, ֻ������������ͨ��, syn->syn/ack->ack, ���ֵ���ģʽ, û�е�һ�����Ͳ����к����İ�, һ��ֻ��ȡһ���� */ rcv_res = marsio_recv_burst(g_mr4_dev_up_handle_set[i], tid, rx_buff, 1); if(rcv_res > 0){ + g_SysInputInfo[tid][PKT_MARSIO_RCV]+=(unsigned long long)rcv_res; for(j = 0; j < rcv_res; j++){ hand_res = marsio4_pkt_hand(tid, rx_buff[j], raw_pkt, DIR_ROUTE_UP); if((g_marsio4_topology_mode & __NET_CONN_SERIAL) && (PASS == hand_res)){ //marsio4_pkt_forward(tid, rx_buff[j]); marsio_send_burst(g_mr4_sendpath_down_set[i], tid, &rx_buff[j], 1); + g_SysInputInfo[tid][PKT_MARSIO_SND]++; }else{ /* ����ģʽ�µ���free, �п�����Ҫ��ע/���� */ marsio_buff_free(sapp_marsio4_instance, &rx_buff[j], 1, MARSIO_SOCKET_ID_ANY, tid); + g_SysInputInfo[tid][PKT_MARSIO_FREE]++; } } pkt_num += rcv_res; } rcv_res = marsio_recv_burst(g_mr4_dev_down_handle_set[i], tid, rx_buff, 1); if(rcv_res > 0){ + g_SysInputInfo[tid][PKT_MARSIO_RCV]+=(unsigned long long)rcv_res; for(j = 0; j < rcv_res; j++){ hand_res = marsio4_pkt_hand(tid, rx_buff[j], raw_pkt, DIR_ROUTE_DOWN); if((g_marsio4_topology_mode & __NET_CONN_SERIAL) && (PASS == hand_res)){ //marsio4_pkt_forward(tid, rx_buff[j]); marsio_send_burst(g_mr4_sendpath_up_set[i], tid, &rx_buff[j], 1); + g_SysInputInfo[tid][PKT_MARSIO_SND]++; }else{ /* ����ģʽ�µ���free, �п�����Ҫ��ע/���� */ marsio_buff_free(sapp_marsio4_instance, &rx_buff[j], 1, MARSIO_SOCKET_ID_ANY, tid); + g_SysInputInfo[tid][PKT_MARSIO_FREE]++; } } pkt_num += rcv_res; diff --git a/packet_io/packet_io_status.cpp b/packet_io/packet_io_status.cpp index adaa76e..68dcc6d 100644 --- a/packet_io/packet_io_status.cpp +++ b/packet_io/packet_io_status.cpp @@ -33,7 +33,8 @@ extern void sapp_statsd_add_para(const char *key, unsigned long long value); extern void pkt_runtime_output(void); extern void sapp_statsd_flush_buf(void); extern void sapp_fs2_update(int field_index, unsigned long long value); - +extern int top_mode; +extern int cap_mode; /* ת���������������������ַ���, ��123bps, 34.5Mbps */ static char *byte_convert_human(unsigned long long bytes, int interval, int multiple, char * byte_str) { @@ -162,6 +163,27 @@ void sysinfo_output(void) fprintf(fp,"%-10s %12ld\n", "app", packet_io_get_app_drop_num_tot()); fprintf(fp,"%-10s %12ld\n\n", "lib", packet_io_get_lib_drop_num()); + if(cap_mode == CAP_MODEL_MARSIOV4) + { + fprintf(fp,"\n------------------------------- MARSIO mbuf statistics detail -----------------------------\n"); + fprintf(fp,"%3s %-27s %-19s %-27s\n", "", "|-----------Current---------|", "|------Realtime-----|", "|--------From-of-startup---------|"); + fprintf(fp,"%3s %8s %9s %10s %10s %10s %10s %9s %13s\n", "tid", "mbuf-recv", "mbuf-alloc", "mbuf-send", + "mbuf-free", + "mbuf-recv/s", "mbuf-alloc/s", "mbuf-send/s", "mbuf-free/s"); + for(i = 0; i < g_packet_io_thread_num; i++){ + fprintf(fp, "%3d %8llu %9llu %10llu %10llu %10llu %10llu %9llu %13llu\n", + i, + g_SysInputInfo[i][PKT_MARSIO_RCV], + g_SysInputInfo[i][PKT_MARSIO_MALLOC], + g_SysInputInfo[i][PKT_MARSIO_SND], + g_SysInputInfo[i][PKT_MARSIO_FREE], + (g_SysInputInfo[i][PKT_MARSIO_RCV]-g_SysInputInfo_old[i][PKT_MARSIO_RCV]), + (g_SysInputInfo[i][PKT_MARSIO_MALLOC]-g_SysInputInfo_old[i][PKT_MARSIO_MALLOC]), + (g_SysInputInfo[i][PKT_MARSIO_SND]-g_SysInputInfo_old[i][PKT_MARSIO_SND]), + (g_SysInputInfo[i][PKT_MARSIO_FREE]-g_SysInputInfo_old[i][PKT_MARSIO_FREE])); + } + } + fprintf(fp,"\n------------------------------- TCP statistics detail -----------------------------\n"); fprintf(fp,"%3s %-27s %-19s %-27s\n", "", "|-----------Current---------|", "|------Realtime-----|", "|--------From-of-startup---------|"); fprintf(fp,"%3s %8s %9s %10s %10s %10s %10s %9s %13s\n", "tid", "link-syn", "link-data", "link-nouse", "link-new/s", "link-del/s", "stream-num", "reset-num", "hash-list-max"); @@ -209,7 +231,23 @@ void sysinfo_output(void) fprintf(fp,"%-10s %12llu %12s %12llu %12s\n", "UDP", sysinfo_new[SEND_UDP_PKT], byte_convert_human(sysinfo_new[SEND_UDP_PKT_LEN],1,1,s1),(sysinfo_new[SEND_UDP_PKT]-sysinfo_old[SEND_UDP_PKT]), byte_convert_human((sysinfo_new[SEND_UDP_PKT_LEN]-sysinfo_old[SEND_UDP_PKT_LEN]),1,8,s2)); fprintf(fp,"%-10s %12llu %12s %12llu %12s\n", "SEND-ERR", sysinfo_new[SEND_PKT_ERR], byte_convert_human(sysinfo_new[SEND_PKT_ERR_LEN],1,1,s1),(sysinfo_new[SEND_PKT_ERR]-sysinfo_old[SEND_PKT_ERR]), byte_convert_human((sysinfo_new[SEND_PKT_ERR_LEN]-sysinfo_old[SEND_PKT_ERR_LEN]),1,8,s2)); - fprintf(fp,"#################################################################\n\n"); + fprintf(fp,"#################################################################\n"); + + fprintf(fp,"%-10s %12s %12s\n","TCP_LINK", "total_link", "link/s"); + fprintf(fp,"%-10s %12llu %12llu\n", "TCP_NEW", sysinfo_new[SYS_TCP_LINK_NEW], + sysinfo_new[SYS_TCP_LINK_NEW] - sysinfo_old[SYS_TCP_LINK_NEW]); + //fprintf(fp,"%-10s %12llu %12llu\n", "TCP_DATA", sysinfo_new[SYS_TCP_LINK_DATA], + // sysinfo_new[SYS_TCP_LINK_DATA] - sysinfo_old[SYS_TCP_LINK_DATA]); + fprintf(fp,"%-10s %12llu %12llu\n", "TCP_DEL", sysinfo_new[SYS_TCP_LINK_DEL], + sysinfo_new[SYS_TCP_LINK_DEL] - sysinfo_old[SYS_TCP_LINK_DEL]); + fprintf(fp,"%-10s %12llu %12llu\n", "TCP_DOU", sysinfo_new[LINK_DOUBLE], + sysinfo_new[LINK_DOUBLE] - sysinfo_old[LINK_DOUBLE]); + fprintf(fp,"%-10s %12llu %12llu\n", "TCP_C2S", sysinfo_new[LINK_SINGLE_C2S], + sysinfo_new[LINK_SINGLE_C2S] - sysinfo_old[LINK_SINGLE_C2S]); + fprintf(fp,"%-10s %12llu %12llu\n", "TCP_S2C", sysinfo_new[LINK_SINGLE_S2C], + sysinfo_new[LINK_SINGLE_S2C] - sysinfo_old[LINK_SINGLE_S2C]); + + fprintf(fp,"#################################################################\n\n"); /* 2018-08-29 lijia add field statsd */ #if 0 @@ -275,6 +313,10 @@ void sysinfo_output(void) sapp_fs2_update(SYS_TCP_LINK_DEL, sysinfo_new[SYS_TCP_LINK_DEL]); sapp_fs2_update(SYS_TCP_LINK_DATA, sysinfo_new[SYS_TCP_LINK_DATA]); + sapp_fs2_update(LINK_DOUBLE,sysinfo_new[LINK_DOUBLE]); + sapp_fs2_update(LINK_SINGLE_C2S, sysinfo_new[LINK_SINGLE_C2S]); + sapp_fs2_update(LINK_SINGLE_S2C, sysinfo_new[LINK_SINGLE_S2C]); + sapp_fs2_update(SEND_TCP_RST, sysinfo_new[SEND_TCP_RST]); sapp_fs2_update(SEND_TCP_SYN_ACK, sysinfo_new[SEND_TCP_SYN_ACK]); sapp_fs2_update(SEND_UDP_PKT, sysinfo_new[SEND_UDP_PKT]); |
