From 0bb8539ed21224627d9feebfccfef82ac0d1f62e Mon Sep 17 00:00:00 2001 From: 李佳 Date: Fri, 2 Apr 2021 07:03:13 +0000 Subject: Fix v4.2 mpls vlan addr --- CMakeLists.txt | 3 + include/private/duplicate_pkt_distinguish.h | 8 +- include/private/mesa_net.h | 7 +- include/private/mesa_pkt_dump.h | 3 +- include/private/stream_internal.h | 1 + include/public/stream_inc/stream_base.h | 61 +++- include/public/stream_inc/stream_entry.h | 1 + src/common/net_common.c | 10 +- src/common/stream_addr_inet.c | 32 +- src/dealpkt/deal_mpls.c | 25 +- src/dealpkt/deal_tcp.c | 5 +- src/dealpkt/deal_udp.c | 6 +- src/dealpkt/deal_vlan.c | 24 +- src/dealpkt/stream_manage.c | 102 +++--- src/entry/CMakeLists.txt | 35 +- src/entry/sapp_init.c | 12 +- src/entry/sapp_main.c | 100 +----- src/entry/sapp_plug.c | 70 ---- .../cycle_pkt_dump_through_write_offset.c | 8 +- src/packet_io/packet_io.c | 9 +- src/packet_io/sendpacket.c | 296 +++++++++++++---- src/plugin/src/plugin.c | 4 +- src/sapp_dev/CMakeLists.txt | 20 ++ src/sapp_dev/sapp_global_val.c | 222 +++++++++++++ src/sapp_dev/sapp_init.c | 367 +++++++++++++++++++++ src/sapp_dev/sapp_plug.c | 123 +++++++ 26 files changed, 1198 insertions(+), 356 deletions(-) delete mode 100644 src/entry/sapp_plug.c create mode 100644 src/sapp_dev/CMakeLists.txt create mode 100644 src/sapp_dev/sapp_global_val.c create mode 100644 src/sapp_dev/sapp_init.c create mode 100644 src/sapp_dev/sapp_plug.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e8ea661..6e77057 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,6 +175,7 @@ add_subdirectory(./src/common) add_subdirectory(./src/config) add_subdirectory(./src/inner_plug) add_subdirectory(./src/timer) +add_subdirectory(./src/sapp_dev) add_subdirectory(./src/entry) add_subdirectory(./test) add_subdirectory(./service) @@ -268,6 +269,8 @@ install(FILES ${PROJECT_SOURCE_DIR}/include/public/stream_inc/stream_rawpkt.h DE install(FILES ${PROJECT_SOURCE_DIR}/include/public/stream_inc/stream_tunnel.h DESTINATION /opt/MESA/include/MESA/stream_inc COMPONENT HEADER) install(FILES ${PROJECT_SOURCE_DIR}/include/public/stream_inc/sapp_inject.h DESTINATION /opt/MESA/include/MESA/stream_inc COMPONENT HEADER) +install(FILES ${CMAKE_BINARY_DIR}/src/entry/libsapp_dev.a DESTINATION . COMPONENT HEADER) + install(FILES ${CMAKE_BINARY_DIR}/service/sapp.service DESTINATION /usr/lib/systemd/system/ COMPONENT PROFILE) include(Package) diff --git a/include/private/duplicate_pkt_distinguish.h b/include/private/duplicate_pkt_distinguish.h index d76e23a..1d0f1ee 100644 --- a/include/private/duplicate_pkt_distinguish.h +++ b/include/private/duplicate_pkt_distinguish.h @@ -4,6 +4,7 @@ #include "mesa_net.h" #include "stream_internal.h" +#define SAPP_DUP_FIRST_PKT_NUM (3) /* 通过前N个包识别是否有重复流量 */ /* 网络序, network order */ @@ -26,7 +27,7 @@ struct __sapp_dup_pkt_key_v4{ unsigned int tcp_ack; unsigned short sport; unsigned short dport; - unsigned short ip_id; /* TCP层SYN重传时, ipid是不一样的, 其实不能算为重复包 */ + unsigned short ip_id; /* TCP层SYN重传时, 或者应用层重传数据, ipid是不一样的, 其实不能算为重复包, 所以ipid也作为Key */ unsigned short checksum; /* tcp or udp */ }__attribute__((packed, aligned(1))); @@ -46,7 +47,10 @@ typedef struct __sapp_dup_pkt_key_v6 sapp_dup_pkt_key_v6_t; int sapp_dup_pkt_init(void); -void sapp_dup_pkt_mark_tcp(int tid, const struct streaminfo *pstream, const void *ip_hdr, const struct mesa_tcp_hdr *tcp_h); +void sapp_dup_pkt_mark_tcp(int tid, unsigned char addr_type, const void *ip_hdr, const struct mesa_tcp_hdr *tcp_h); +void sapp_dup_pkt_mark_udp(int tid, unsigned char addrtype, const void *ip_hdr, const struct mesa_udp_hdr *udp_h); +void sendpkt_dup_pkt_mark_stream(const struct streaminfo *top_stream, const char *ip_hdr); + int sapp_dup_pkt_identify_tcp(int tid, struct streaminfo_private *pstream_pr, int tot_pkt, const void *ip_hdr, const struct mesa_tcp_hdr *tcp_h); diff --git a/include/private/mesa_net.h b/include/private/mesa_net.h index e61b3a3..aab3c0b 100644 --- a/include/private/mesa_net.h +++ b/include/private/mesa_net.h @@ -513,9 +513,10 @@ struct mesa_vlan_hdr{ }; struct mesa_vlan_detail_hdr{ - unsigned int priority:3; - unsigned int del_flag:1; - unsigned int vlan_id:12; + unsigned char vlan_id_high:4; + unsigned char del_flag:1; + unsigned char priority:3; + unsigned char vlan_id_low; unsigned short type; }; diff --git a/include/private/mesa_pkt_dump.h b/include/private/mesa_pkt_dump.h index 3992c5b..7a87fcc 100644 --- a/include/private/mesa_pkt_dump.h +++ b/include/private/mesa_pkt_dump.h @@ -1,7 +1,7 @@ #ifndef _MESA_PKT_DUMP_H_ #define _MESA_PKT_DUMP_H_ 1 -#define MESA_PKT_DUM_H_VER (20201102) +#define MESA_PKT_DUM_H_VER (20210330) /* DPDK-3.0, PFRINGģʽ, ռʱ, ͨƽ̨ʵʱ. @@ -16,6 +16,7 @@ enum _pkt_classify{ PKT_CLASSIFY_INJECT = 0x04, /* DMAC asciiֵ: J */ PKT_CLASSIFY_DROP = 0x08, /* DMAC asciiֵ: D */ PKT_CLASSIFY_ERROR = 0x10, /* DMAC asciiֵ: E */ + PKT_CLASSIFY_REPEAT = 0x20, /* DMAC asciiֵ: R */ }; enum pkt_dump_opt_t{ diff --git a/include/private/stream_internal.h b/include/private/stream_internal.h index 84c28f7..7b4315d 100644 --- a/include/private/stream_internal.h +++ b/include/private/stream_internal.h @@ -389,6 +389,7 @@ int sapp_identify_broad_multicast_init(void); int plugin_manage_init(void); void *plugin_get_plug_entry(const char *plug_name, const char *plug_entry_type); +int libsapp_setup_env(int argc, char *argv[]); #ifdef __cplusplus } diff --git a/include/public/stream_inc/stream_base.h b/include/public/stream_inc/stream_base.h index 9d97aa7..e357322 100644 --- a/include/public/stream_inc/stream_base.h +++ b/include/public/stream_inc/stream_base.h @@ -1,7 +1,7 @@ #ifndef _APP_STREAM_BASE_H_ #define _APP_STREAM_BASE_H_ -#define STREAM_BASE_H_VERSION (20201009) +#define STREAM_BASE_H_VERSION (20210331) #include #include @@ -24,7 +24,7 @@ typedef unsigned short UINT16; #endif #ifndef UINT32 -typedef unsigned int UINT32; +typedef unsigned int UINT32; #endif #ifndef UINT64 typedef unsigned long long UINT64; @@ -192,17 +192,37 @@ struct layer_addr_gre #define VLAN_ID_MASK (0x0FFF) #define VLAN_TAG_LEN (4) -#define MAX_VLAN_ADDR_LAYER (3) +#define MAX_VLAN_ADDR_LAYER (2) + +struct single_layer_vlan_addr{ /* refer to https://en.wikipedia.org/wiki/IEEE_802.1Q */ + unsigned short TPID; /* Tag protocol identifier, network order */ + unsigned char PCP; /* Priority code point */ + unsigned char DEI; /* Drop eligible indicator */ + unsigned short VID; /* VLAN identifier, network order */ +}; + +#if 0 struct layer_addr_vlan { UINT16 vlan_id; /* network order, old value, to be obsoleted */ - UCHAR src_vlan_layer_num; - UCHAR dst_vlan_layer_num; - UINT32 src_vlan_pkt[MAX_VLAN_ADDR_LAYER]; /* network order */ - UINT32 dst_vlan_pkt[MAX_VLAN_ADDR_LAYER]; /* network order */ + UCHAR c2s_layer_num; + UCHAR s2c_layer_num; + UINT32 c2s_addr_array[MAX_VLAN_ADDR_LAYER]; /* network order */ + UINT32 s2c_addr_array[MAX_VLAN_ADDR_LAYER]; /* network order */ +}; +#else +struct layer_addr_vlan +{ + struct single_layer_vlan_addr c2s_addr_array[MAX_VLAN_ADDR_LAYER]; + struct single_layer_vlan_addr s2c_addr_array[MAX_VLAN_ADDR_LAYER]; + UCHAR c2s_layer_num; + UCHAR s2c_layer_num; }; + +#endif + struct layer_addr_pppoe_session { #if __BYTE_ORDER == __LITTLE_ENDIAN @@ -295,6 +315,7 @@ struct layer_addr_l2tp }; #define MAX_MPLS_ADDR_LAYER 4 +#if 0 struct layer_addr_mpls { unsigned int src_mpls_pkt[MAX_MPLS_ADDR_LAYER]; @@ -306,6 +327,32 @@ struct layer_addr_mpls unsigned int src_mpls_ctrl_word; /* refer to RFC4623 */ unsigned int dst_mpls_ctrl_word; /* refer to RFC4623 */ }; +#else + +struct single_layer_mpls_addr{ /* refer to RFC3032 */ + unsigned int label; /* network order */ + unsigned char experimental; + unsigned char bottom; + unsigned char ttl; +}; + +/* + MPLSпǶǶ, sappѶϲ, Ŀǰ֧4, ⵽, 0ʾ, 3ʾڲ + һڲTCP/UDP˵, ײMPLSĵַܲһ, ֱΪcs2_addr, s2c_addr. +*/ +struct layer_addr_mpls +{ + struct single_layer_mpls_addr c2s_addr_array[MAX_MPLS_ADDR_LAYER]; + struct single_layer_mpls_addr s2c_addr_array[MAX_MPLS_ADDR_LAYER]; + char c2s_layer_num; /* ʵmpls */ + char s2c_layer_num; /* ʵmpls */ + char c2s_has_ctrl_word; + char s2c_has_ctrl_word; + unsigned int c2s_mpls_ctrl_word; /* refer to RFC4623 */ + unsigned int s2c_mpls_ctrl_word; /* refer to RFC4623 */ +}; + +#endif struct layer_addr_pptp { diff --git a/include/public/stream_inc/stream_entry.h b/include/public/stream_inc/stream_entry.h index 09738c2..01ec865 100644 --- a/include/public/stream_inc/stream_entry.h +++ b/include/public/stream_inc/stream_entry.h @@ -85,6 +85,7 @@ char POLLING_ENTRY(struct streaminfo *stream, void **pme, int thread_seq,void * char PROT_PROCESS(stSessionInfo* session_info, void **pme, int thread_seq,struct streaminfo *a_stream,const void *a_packet); +int libsapp_setup_env(int argc, char *argv[]); #ifdef __cplusplus diff --git a/src/common/net_common.c b/src/common/net_common.c index bcf1f29..0f3342d 100644 --- a/src/common/net_common.c +++ b/src/common/net_common.c @@ -401,8 +401,8 @@ static int mpls_jump_to_layer(const char *raw_data, int raw_layer_type, int exp } set_mpls_addr(&mpls_addr, (unsigned char *)raw_data); - mpls_layer_len = mpls_addr.src_mpls_layer_num * sizeof(int); - if(mpls_addr.src_has_ctrl_word){ + mpls_layer_len = mpls_addr.c2s_layer_num * sizeof(struct mesa_mpls_hdr); + if(mpls_addr.c2s_has_ctrl_word){ mpls_layer_len += sizeof(int); } @@ -499,10 +499,10 @@ static int vlan8021q_jump_to_layer(const char *raw_data, int raw_layer_type, in } set_vlan_addr(&vlan_addr, (const unsigned char *)raw_data); - vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr.src_vlan_layer_num; + vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr.c2s_layer_num; next_layer_data = raw_data + vlan_layer_len; - vhdr = (struct mesa_vlan_hdr *)&vlan_addr.src_vlan_pkt[vlan_addr.src_vlan_layer_num-1]; - next_layer_type = ntohs(vhdr->type); + //vhdr = (struct mesa_vlan_hdr *)&vlan_addr.c2s_addr_array[vlan_addr.c2s_layer_num-1]; + next_layer_type = ntohs(vlan_addr.c2s_addr_array[vlan_addr.c2s_layer_num-1].TPID); switch(next_layer_type){ case ETH_P_ARP: diff --git a/src/common/stream_addr_inet.c b/src/common/stream_addr_inet.c index ba925f0..e6978c9 100644 --- a/src/common/stream_addr_inet.c +++ b/src/common/stream_addr_inet.c @@ -1339,19 +1339,19 @@ static int __addr_vlan_n2p_fun(const struct layer_addr *paddr, char *buf, int bu char vlan_addr_string[128] = {}; /* ע: зǶԳvlan */ - int max_layer_num = vlan_addr->src_vlan_layer_num > vlan_addr->dst_vlan_layer_num ? vlan_addr->src_vlan_layer_num:vlan_addr->dst_vlan_layer_num; + int max_layer_num = vlan_addr->c2s_layer_num > vlan_addr->s2c_layer_num ? vlan_addr->c2s_layer_num:vlan_addr->s2c_layer_num; for(i = 0; i < max_layer_num; i++){ - if(vlan_addr->src_vlan_layer_num >= i+1){ - vhdr = (struct mesa_vlan_hdr *)&vlan_addr->src_vlan_pkt[i]; + if(vlan_addr->c2s_layer_num >= i+1){ + vhdr = (struct mesa_vlan_hdr *)&vlan_addr->c2s_addr_array[i]; vlan_id = ntohs(vhdr->pri_cfi_id) & VLAN_ID_MASK; snprintf(tmp_buf, 32, "%d", vlan_id); strcat(vlan_addr_string, tmp_buf); }else{ strcat(vlan_addr_string, "N/A"); } - if(vlan_addr->dst_vlan_layer_num >= i+1){ - vhdr = (struct mesa_vlan_hdr *)&vlan_addr->dst_vlan_pkt[i]; + if(vlan_addr->s2c_layer_num >= i+1){ + vhdr = (struct mesa_vlan_hdr *)&vlan_addr->s2c_addr_array[i]; vlan_id = ntohs(vhdr->pri_cfi_id) & VLAN_ID_MASK; snprintf(tmp_buf, 32, "%d", vlan_id); strcat(vlan_addr_string, tmp_buf); @@ -1470,25 +1470,25 @@ static int __addr_mpls_n2p_fun(const struct layer_addr *paddr, char *buf, int bu int mpls_label; char tmp_buf[32]; /* עзǶԳmpls */ - int max_layer_num = mpls_addr->src_mpls_layer_num > mpls_addr->dest_mpls_layer_num ? mpls_addr->src_mpls_layer_num:mpls_addr->dest_mpls_layer_num; + int max_layer_num = mpls_addr->c2s_layer_num > mpls_addr->s2c_layer_num ? mpls_addr->c2s_layer_num:mpls_addr->s2c_layer_num; char mpls_addr_string[128] = {}; for(i = 0; i < max_layer_num; i++){ - if(mpls_addr->src_mpls_layer_num >= i+1){ - mpls_label = 0; - net_mpls_hdr = (struct mesa_mpls_hdr *)&mpls_addr->src_mpls_pkt[i]; - mpls_label = (net_mpls_hdr->mpls_label_low<<12) | (net_mpls_hdr->mpls_label_mid<<4) | net_mpls_hdr->mpls_label_high; /* network order */ - snprintf(tmp_buf, 32, "%d", mpls_label); + if(mpls_addr->c2s_layer_num >= i+1){ + //mpls_label = 0; + //net_mpls_hdr = (struct mesa_mpls_hdr *)&mpls_addr->src_mpls_pkt[i]; + //mpls_label = (net_mpls_hdr->mpls_label_low<<12) | (net_mpls_hdr->mpls_label_mid<<4) | net_mpls_hdr->mpls_label_high; /* network order */ + snprintf(tmp_buf, 32, "%d", ntohl(mpls_addr->c2s_addr_array[i].label)); strcat(mpls_addr_string, tmp_buf); }else{ strcat(mpls_addr_string, "N/A"); } - if(mpls_addr->dest_mpls_layer_num >= i+1){ - mpls_label = 0; - net_mpls_hdr = (struct mesa_mpls_hdr *)&mpls_addr->dest_mpls_pkt[i]; - mpls_label = (net_mpls_hdr->mpls_label_low<<12) | (net_mpls_hdr->mpls_label_mid<<4) | net_mpls_hdr->mpls_label_high; /* network order */ - snprintf(tmp_buf, 32, "-%d", mpls_label); + if(mpls_addr->s2c_layer_num >= i+1){ + //mpls_label = 0; + //net_mpls_hdr = (struct mesa_mpls_hdr *)&mpls_addr->dest_mpls_pkt[i]; + //mpls_label = (net_mpls_hdr->mpls_label_low<<12) | (net_mpls_hdr->mpls_label_mid<<4) | net_mpls_hdr->mpls_label_high; /* network order */ + snprintf(tmp_buf, 32, "-%d", ntohl(mpls_addr->s2c_addr_array[i].label)); strcat(mpls_addr_string, tmp_buf); }else{ strcat(mpls_addr_string, "-N/A"); diff --git a/src/dealpkt/deal_mpls.c b/src/dealpkt/deal_mpls.c index bdadac8..8a6466b 100644 --- a/src/dealpkt/deal_mpls.c +++ b/src/dealpkt/deal_mpls.c @@ -14,6 +14,7 @@ extern int ipv6_entry(struct streaminfo_private *pfstream, const void *this_laye extern int eth_entry(struct streaminfo_private *fstream_pr, const void *this_layer_hdr,int thread_num, unsigned char dir, const raw_pkt_t *raw_pkt, int offset_to_raw_pkt_hdr); +extern int mpls_addr_net_to_mem(const struct mesa_mpls_hdr *net_mpls_hdr, struct single_layer_mpls_addr *mem_mpls_hdr); static int guess_mpls_with_control_word(const unsigned char *maybe_eth_hdr) { @@ -21,7 +22,11 @@ static int guess_mpls_with_control_word(const unsigned char *maybe_eth_hdr) /* MPLSûֶαʾصЭ, !! - RFC4623, or https://wiki.mikrotik.com/wiki/Manual:VPLS_Control_Word + + https://tools.ietf.org/html/rfc4623 + https://wiki.mikrotik.com/wiki/Manual:VPLS_Control_Word + https://www.ciscopress.com/articles/article.asp?p=386788&seqNum=2 + ׼ipv4, ipv6֮, пethernet, пǴPW Ethernet Control Word, ʽ: 0 1 2 3 @@ -67,8 +72,10 @@ int set_mpls_addr(struct layer_addr_mpls *addr, const unsigned char *raw_mpls_pk for(i = 0; i < MAX_MPLS_ADDR_LAYER; i++) { this_mpls_hdr = (const struct mesa_mpls_hdr *)raw_mpls_pkt_data; - memcpy(&addr->src_mpls_pkt[i], raw_mpls_pkt_data, sizeof(struct mesa_mpls_hdr)); - addr->src_mpls_layer_num += 1; + //memcpy(&addr->src_mpls_pkt[i], raw_mpls_pkt_data, sizeof(struct mesa_mpls_hdr)); + mpls_addr_net_to_mem(this_mpls_hdr, &addr->c2s_addr_array[i]); /* ջĵַ, ÿݴc2s, TCP/UDPٸת */ + + addr->c2s_layer_num += 1; if(1 == this_mpls_hdr->mpls_bls){ raw_mpls_pkt_data += sizeof(struct mesa_mpls_hdr); /* Ϊ˺淽жǷctrl word */ break; @@ -78,14 +85,14 @@ int set_mpls_addr(struct layer_addr_mpls *addr, const unsigned char *raw_mpls_pk if(1 != this_mpls_hdr->mpls_bls) /* MAX_MPLS_ADDR_LAYER, MPLSûн */ { - sapp_runtime_log(30, "MPLS layer number over load, only support %d\n", MAX_MPLS_ADDR_LAYER); + sapp_runtime_log(RLOG_LV_FATAL, "MPLS layer number over load, only support %d\n", MAX_MPLS_ADDR_LAYER); return -1; } if(((*raw_mpls_pkt_data & 0xF0) != 0x40) && ((*raw_mpls_pkt_data & 0xF0) != 0x60)){ //VPLS, MPLS with Control Word if(guess_mpls_with_control_word(raw_mpls_pkt_data) > 0){ - memcpy(&addr->src_mpls_ctrl_word, raw_mpls_pkt_data, sizeof(int)); - addr->src_has_ctrl_word = 1; + memcpy(&addr->c2s_mpls_ctrl_word, raw_mpls_pkt_data, sizeof(int)); + addr->c2s_has_ctrl_word = 1; } } @@ -121,8 +128,8 @@ int mpls_uc_entry(struct streaminfo_private *pfstream_pr,const void *this_layer_ return PASS; } - next_layer_offset = offset_to_raw_pkt_hdr + sizeof(struct mesa_mpls_hdr)*addr.src_mpls_layer_num; - next_layer_hdr = (char *)this_layer_data+ sizeof(struct mesa_mpls_hdr)*addr.src_mpls_layer_num; + next_layer_offset = offset_to_raw_pkt_hdr + sizeof(struct mesa_mpls_hdr)*addr.c2s_layer_num; + next_layer_hdr = (char *)this_layer_data+ sizeof(struct mesa_mpls_hdr)*addr.c2s_layer_num; #if 0 this_mpls_hdr.mpls_label = (ntohl(*mpls_pkt_data) & MPLS_LABEL_MASK) >> 12; @@ -218,7 +225,7 @@ int mpls_uc_entry(struct streaminfo_private *pfstream_pr,const void *this_layer_ } if(inject_to_eth_flag != 0){ - if(addr.src_has_ctrl_word != 0){ + if(addr.c2s_has_ctrl_word != 0){ ret = eth_entry(pstream_pr,next_layer_hdr+4,thread_num,routedir,raw_pkt, next_layer_offset+4); }else{ int left_pkt_len = raw_pkt->raw_pkt_len - next_layer_offset; diff --git a/src/dealpkt/deal_tcp.c b/src/dealpkt/deal_tcp.c index 27edfef..dd96301 100644 --- a/src/dealpkt/deal_tcp.c +++ b/src/dealpkt/deal_tcp.c @@ -2517,7 +2517,7 @@ static int deal_tcp_stream(struct streamindex *pindex, const void *this_iphdr, s if(unlikely(!pindex_tcp)){ /* ÿװǷظ, Ҫbloom filter */ if(sapp_global_val->config.packet_io.dup_pkt_para.dup_pkt_distinguish_enable != 0){ - sapp_dup_pkt_mark_tcp(pstream->threadnum,pstream, this_iphdr, this_tcphdr); + sapp_dup_pkt_mark_tcp(pstream->threadnum,pstream->addr.addrtype, this_iphdr, this_tcphdr); } if(this_tcphdr->th_flags & TH_SYN) @@ -2644,7 +2644,8 @@ static int deal_tcp_stream(struct streamindex *pindex, const void *this_iphdr, s && (sapp_dup_pkt_identify_tcp(pstream->threadnum, pstream_pr, pdetail->clientpktnum+pdetail->serverpktnum, this_iphdr, this_tcphdr) != 0)){ local_sys_stat = &sapp_global_val->mthread_volatile[pstream->threadnum]->sys_stat; local_sys_stat->count[SAPP_STAT_RCV_DUP_TCP]++; - local_sys_stat->length[SAPP_STAT_RCV_DUP_TCP] += tcplen; + local_sys_stat->length[SAPP_STAT_RCV_DUP_TCP] += tcplen; + cycle_pkt_dump_by_classify(pstream->threadnum, raw_pkt, PKT_CLASSIFY_REPEAT); return PASS; } diff --git a/src/dealpkt/deal_udp.c b/src/dealpkt/deal_udp.c index fffd2ec..4c3df4b 100644 --- a/src/dealpkt/deal_udp.c +++ b/src/dealpkt/deal_udp.c @@ -552,7 +552,8 @@ int dealipv4udppkt(struct streamindex *pindex, const struct mesa_ip4_hdr * this_ if((sapp_global_val->config.packet_io.dup_pkt_para.dup_pkt_distinguish_enable != 0) && (sapp_dup_pkt_identify_udp_v4(thread_num, pstream_pr, pdetail_pr->udpdetail_public.clientpktnum+pdetail_pr->udpdetail_public.serverpktnum, this_iphdr, udph) != 0)){ local_sys_stat->count[SAPP_STAT_RCV_DUP_UDP]++; - local_sys_stat->length[SAPP_STAT_RCV_DUP_UDP] += ulen; + local_sys_stat->length[SAPP_STAT_RCV_DUP_UDP] += ulen; + cycle_pkt_dump_by_classify(pstream->threadnum, raw_pkt, PKT_CLASSIFY_REPEAT); return PASS; } @@ -751,7 +752,8 @@ int dealipv6udppkt(struct streamindex *pindex,const struct mesa_ip6_hdr *a_packe if((sapp_global_val->config.packet_io.dup_pkt_para.dup_pkt_distinguish_enable != 0) && (sapp_dup_pkt_identify_udp_v6(thread_num, pstream_pr, pdetail_pr->udpdetail_public.clientpktnum+pdetail_pr->udpdetail_public.serverpktnum, a_packet, udph) != 0)){ local_sys_stat->count[SAPP_STAT_RCV_DUP_UDP]++; - local_sys_stat->length[SAPP_STAT_RCV_DUP_UDP] += ulen; + local_sys_stat->length[SAPP_STAT_RCV_DUP_UDP] += ulen; + cycle_pkt_dump_by_classify(pstream->threadnum, raw_pkt, PKT_CLASSIFY_REPEAT); return PASS; } diff --git a/src/dealpkt/deal_vlan.c b/src/dealpkt/deal_vlan.c index 3dc12ad..1eab146 100644 --- a/src/dealpkt/deal_vlan.c +++ b/src/dealpkt/deal_vlan.c @@ -5,6 +5,8 @@ extern "C" { #include "sapp_api.h" #include "sapp_private_api.h" +extern int vlan_addr_net_to_mem(const struct mesa_vlan_detail_hdr *net_vlan_hdr, struct single_layer_vlan_addr *mem_vlan_hdr); + #define VLAN_ID_MAX (4096) /* vlan id is 12bit, max is 4096 */ #if DEBUG @@ -14,20 +16,20 @@ static inline void packet_io_vlan_status_update(int thread_seq, const struct lay int set_vlan_addr(struct layer_addr_vlan *addr, const unsigned char *vlan_tag) { int i; - const struct mesa_vlan_hdr *vhdr; + const struct mesa_vlan_detail_hdr *net_vhdr; int vlan_layer_len = 0; memset(addr, 0, sizeof(struct layer_addr_vlan)); - addr->vlan_id = ntohs(*(unsigned short *)vlan_tag) & VLAN_ID_MASK; //compat old value - for(i = 0; i < MAX_VLAN_ADDR_LAYER; i++){ - vhdr = (struct mesa_vlan_hdr *)vlan_tag; - memcpy(&addr->src_vlan_pkt[i], vlan_tag, sizeof(struct mesa_vlan_hdr)); - vlan_tag += sizeof(struct mesa_vlan_hdr); - addr->src_vlan_layer_num++; - vlan_layer_len += sizeof(struct mesa_vlan_hdr); - if(ETH_P_8021Q != ntohs(vhdr->type)){ + net_vhdr = (struct mesa_vlan_detail_hdr *)vlan_tag; + //memcpy(&addr->c2s_addr_array[i], vlan_tag, sizeof(struct mesa_vlan_hdr)); + + vlan_addr_net_to_mem(net_vhdr, &addr->c2s_addr_array[i]); + vlan_tag += sizeof(struct mesa_vlan_detail_hdr); + vlan_layer_len += sizeof(struct mesa_vlan_detail_hdr); + addr->c2s_layer_num++; + if(ETH_P_8021Q != ntohs(net_vhdr->type)){ break; } } @@ -93,8 +95,8 @@ int IEEE_8021_entry(struct streaminfo_private *pfstream_pr,const void *this_laye pstream_pr = pfstream_pr; /* 2015-07-03 lijia add, skip this layer */ } - vhdr = (struct mesa_vlan_hdr *)&addr.src_vlan_pkt[addr.src_vlan_layer_num-1]; - next_layer_type = ntohs(vhdr->type); + //vhdr = (struct mesa_vlan_hdr *)&addr.c2s_addr_array[addr.c2s_layer_num-1]; + next_layer_type = ntohs(addr.c2s_addr_array[addr.c2s_layer_num-1].TPID); //ȡһTPID next_layer_hdr = (char *)this_layer_data + vlan_layer_len; next_layer_offset = offset_to_raw_pkt_hdr + vlan_layer_len; diff --git a/src/dealpkt/stream_manage.c b/src/dealpkt/stream_manage.c index 7de8860..b90a8a3 100644 --- a/src/dealpkt/stream_manage.c +++ b/src/dealpkt/stream_manage.c @@ -923,11 +923,11 @@ static int cmpaddr_mpls(const struct layer_addr_mpls *addr_heap, const struct la int i, ret = 0; int same_dir_diff = 0, reverse_dir_diff = 0; - if(0 == addr_stack->src_mpls_layer_num){ + if(0 == addr_stack->c2s_layer_num){ return 0; /* İmplsǿղ, ñȽ */ } - if((addr_heap->src_mpls_layer_num == 0) || (addr_heap->dest_mpls_layer_num == 0)){ + if((addr_heap->c2s_layer_num == 0) || (addr_heap->s2c_layer_num == 0)){ /* ĵַijһ0, Ҳǵ, ûյԲ; Ҳijһǿղ, ûmpls, ʱûжһԲݰ״ε, Ǹһһ״ε, @@ -937,11 +937,11 @@ static int cmpaddr_mpls(const struct layer_addr_mpls *addr_heap, const struct la } /* Ƚϵַ */ - if(addr_heap->src_mpls_layer_num != addr_stack->src_mpls_layer_num){ + if(addr_heap->c2s_layer_num != addr_stack->c2s_layer_num){ same_dir_diff = 1; }else{ - for(i = 0; i < addr_heap->src_mpls_layer_num; i++){ - if(addr_heap->src_mpls_pkt[i] != addr_stack->src_mpls_pkt[i]){ + for(i = 0; i < addr_heap->c2s_layer_num; i++){ + if(addr_heap->c2s_addr_array[i].label != addr_stack->c2s_addr_array[i].label){ same_dir_diff = 1; break; } @@ -949,11 +949,11 @@ static int cmpaddr_mpls(const struct layer_addr_mpls *addr_heap, const struct la } /* Ƚϵַ */ - if(addr_heap->dest_mpls_layer_num != addr_stack->src_mpls_layer_num){ + if(addr_heap->s2c_layer_num != addr_stack->c2s_layer_num){ reverse_dir_diff = 1; }else{ - for(i = 0; i < addr_heap->dest_mpls_layer_num; i++){ - if(addr_heap->dest_mpls_pkt[i] != addr_stack->src_mpls_pkt[i]){ + for(i = 0; i < addr_heap->s2c_layer_num; i++){ + if(addr_heap->s2c_addr_array[i].label != addr_stack->s2c_addr_array[i].label){ reverse_dir_diff = 1; break; } @@ -2367,8 +2367,8 @@ void addr_reverse_memcpy(const struct streaminfo_private *stack_stream_pr, void memset(dst_vlan, 0, sizeof(struct layer_addr_vlan)); /* ǰstackİַϢ̶洢src, copyǽstackַĵַ */ - memcpy(dst_vlan->dst_vlan_pkt, src_vlan->src_vlan_pkt, sizeof(src_vlan->src_vlan_pkt)); - dst_vlan->dst_vlan_layer_num = src_vlan->src_vlan_layer_num; + memcpy(dst_vlan->s2c_addr_array, src_vlan->c2s_addr_array, sizeof(src_vlan->c2s_addr_array)); + dst_vlan->s2c_layer_num = src_vlan->c2s_layer_num; } break; @@ -2392,11 +2392,11 @@ void addr_reverse_memcpy(const struct streaminfo_private *stack_stream_pr, void memset(dst_mpls, 0, sizeof(struct layer_addr_mpls)); /* ǰstackİַϢ̶洢src, copyǽstackַĵַ */ - memcpy(dst_mpls->dest_mpls_pkt, current_stack_addr->src_mpls_pkt, sizeof(current_stack_addr->src_mpls_pkt)); - dst_mpls->dest_mpls_layer_num = current_stack_addr->src_mpls_layer_num; - if(current_stack_addr->src_has_ctrl_word){ - dst_mpls->dst_mpls_ctrl_word = current_stack_addr->src_mpls_ctrl_word; - dst_mpls->dst_has_ctrl_word = 1; + memcpy(dst_mpls->s2c_addr_array, current_stack_addr->c2s_addr_array, sizeof(current_stack_addr->c2s_addr_array)); + dst_mpls->s2c_layer_num = current_stack_addr->c2s_layer_num; + if(current_stack_addr->c2s_has_ctrl_word){ + dst_mpls->s2c_mpls_ctrl_word = current_stack_addr->c2s_mpls_ctrl_word; + dst_mpls->s2c_has_ctrl_word = 1; } } break; @@ -2744,26 +2744,26 @@ void update_opposite_addr_info(struct streaminfo_private *pstream_pr, case ADDR_TYPE_MPLS: if(ADDR_TYPE_MPLS == pstream_pr->stream_public.addr.addrtype){ if(cur_dir == DIR_C2S){ - memcpy(pstream_pr->stream_public.addr.mpls->src_mpls_pkt, - p_stack->stream_public.addr.mpls->src_mpls_pkt, - sizeof(p_stack->stream_public.addr.mpls->src_mpls_pkt)); - pstream_pr->stream_public.addr.mpls->src_mpls_layer_num = p_stack->stream_public.addr.mpls->src_mpls_layer_num; - if(p_stack->stream_public.addr.mpls->src_has_ctrl_word){//ע, ̶жsrc!! - pstream_pr->stream_public.addr.mpls->src_mpls_ctrl_word = p_stack->stream_public.addr.mpls->src_mpls_ctrl_word; - pstream_pr->stream_public.addr.mpls->src_has_ctrl_word = 1; + memcpy(pstream_pr->stream_public.addr.mpls->c2s_addr_array, + p_stack->stream_public.addr.mpls->c2s_addr_array, + sizeof(p_stack->stream_public.addr.mpls->c2s_addr_array)); + pstream_pr->stream_public.addr.mpls->c2s_layer_num = p_stack->stream_public.addr.mpls->c2s_layer_num; + if(p_stack->stream_public.addr.mpls->c2s_has_ctrl_word){//ע, ̶жsrc!! + pstream_pr->stream_public.addr.mpls->c2s_mpls_ctrl_word = p_stack->stream_public.addr.mpls->c2s_mpls_ctrl_word; + pstream_pr->stream_public.addr.mpls->c2s_has_ctrl_word = 1; } //if(p_stack->fake_layer_dir_for_asymmetric_cmp != 0){ // pstream_pr->fake_layer_dir_for_asymmetric_cmp |= DIR_C2S; //} } else{ - memcpy(pstream_pr->stream_public.addr.mpls->dest_mpls_pkt, - p_stack->stream_public.addr.mpls->src_mpls_pkt, - sizeof(p_stack->stream_public.addr.mpls->src_mpls_pkt)); - pstream_pr->stream_public.addr.mpls->dest_mpls_layer_num = p_stack->stream_public.addr.mpls->src_mpls_layer_num; - if(p_stack->stream_public.addr.mpls->src_has_ctrl_word){ //ע, ̶жsrc!! - pstream_pr->stream_public.addr.mpls->dst_mpls_ctrl_word = p_stack->stream_public.addr.mpls->src_mpls_ctrl_word; - pstream_pr->stream_public.addr.mpls->dst_has_ctrl_word = 1; + memcpy(pstream_pr->stream_public.addr.mpls->s2c_addr_array, + p_stack->stream_public.addr.mpls->c2s_addr_array, + sizeof(p_stack->stream_public.addr.mpls->c2s_addr_array)); + pstream_pr->stream_public.addr.mpls->s2c_layer_num = p_stack->stream_public.addr.mpls->c2s_layer_num; + if(p_stack->stream_public.addr.mpls->c2s_has_ctrl_word){ //ע, ̶жsrc!! + pstream_pr->stream_public.addr.mpls->s2c_mpls_ctrl_word = p_stack->stream_public.addr.mpls->c2s_mpls_ctrl_word; + pstream_pr->stream_public.addr.mpls->s2c_has_ctrl_word = 1; } //if(p_stack->fake_layer_dir_for_asymmetric_cmp != 0){ // pstream_pr->fake_layer_dir_for_asymmetric_cmp |= DIR_S2C; @@ -2777,16 +2777,16 @@ void update_opposite_addr_info(struct streaminfo_private *pstream_pr, case ADDR_TYPE_VLAN: if(ADDR_TYPE_VLAN == pstream_pr->stream_public.addr.addrtype){ if(cur_dir == DIR_C2S){ - memcpy(pstream_pr->stream_public.addr.vlan->src_vlan_pkt, - p_stack->stream_public.addr.vlan->src_vlan_pkt, - sizeof(p_stack->stream_public.addr.vlan->src_vlan_pkt)); - pstream_pr->stream_public.addr.vlan->src_vlan_layer_num = p_stack->stream_public.addr.vlan->src_vlan_layer_num; + memcpy(pstream_pr->stream_public.addr.vlan->c2s_addr_array, + p_stack->stream_public.addr.vlan->c2s_addr_array, + sizeof(p_stack->stream_public.addr.vlan->c2s_addr_array)); + pstream_pr->stream_public.addr.vlan->c2s_layer_num = p_stack->stream_public.addr.vlan->c2s_layer_num; } else{ - memcpy(pstream_pr->stream_public.addr.vlan->dst_vlan_pkt, - p_stack->stream_public.addr.vlan->src_vlan_pkt, //ע, ̶src!! - sizeof(p_stack->stream_public.addr.vlan->dst_vlan_pkt)); - pstream_pr->stream_public.addr.vlan->dst_vlan_layer_num = p_stack->stream_public.addr.vlan->src_vlan_layer_num; //ע, ̶src!! + memcpy(pstream_pr->stream_public.addr.vlan->s2c_addr_array, + p_stack->stream_public.addr.vlan->c2s_addr_array, //ע, ̶src!! + sizeof(p_stack->stream_public.addr.vlan->s2c_addr_array)); + pstream_pr->stream_public.addr.vlan->s2c_layer_num = p_stack->stream_public.addr.vlan->c2s_layer_num; //ע, ̶src!! } }else{ sapp_runtime_log(RLOG_LV_INFO, "update_opposite_addr_info() error, current addrtype is vlan, but pstream->addrtype is not vlan!"); @@ -3147,12 +3147,12 @@ int sapp_dup_pkt_identify_udp_v6(int tid, struct streaminfo_private *pstream_pr, return is_dup_pkt; } -void sapp_dup_pkt_mark_tcp(int tid, const struct streaminfo *pstream, const void *ip_hdr, const struct mesa_tcp_hdr *tcp_h) +void sapp_dup_pkt_mark_tcp(int tid, unsigned char addrtype, const void *ip_hdr, const struct mesa_tcp_hdr *tcp_h) { sapp_dup_pkt_key_v4_t dup_bloom_key_v4; sapp_dup_pkt_key_v6_t dup_bloom_key_v6; - if(ADDR_TYPE_IPV4 == pstream->addr.addrtype){ + if(ADDR_TYPE_IPV4 == addrtype){ sapp_get_dup_pkt_key_tcp_v4((struct mesa_ip4_hdr *)ip_hdr, tcp_h, &dup_bloom_key_v4); sapp_dup_pkt_mark_v4(tid, &dup_bloom_key_v4); }else{ @@ -3162,7 +3162,23 @@ void sapp_dup_pkt_mark_tcp(int tid, const struct streaminfo *pstream, const void return; } - + +void sapp_dup_pkt_mark_udp(int tid, unsigned char addrtype, const void *ip_hdr, const struct mesa_udp_hdr *udp_h) +{ + sapp_dup_pkt_key_v4_t dup_bloom_key_v4; + sapp_dup_pkt_key_v6_t dup_bloom_key_v6; + + if(ADDR_TYPE_IPV4 == addrtype){ + sapp_get_dup_pkt_key_udp_v4((struct mesa_ip4_hdr *)ip_hdr, udp_h, &dup_bloom_key_v4); + sapp_dup_pkt_mark_v4(tid, &dup_bloom_key_v4); + }else{ + sapp_get_dup_pkt_key_udp_v6((struct mesa_ip6_hdr *)ip_hdr, udp_h, &dup_bloom_key_v6); + sapp_dup_pkt_mark_v6(tid, &dup_bloom_key_v6); + } + + return; +} + int sapp_dup_pkt_identify_tcp(int tid, struct streaminfo_private *pstream_pr, int tot_pkt, const void *ip_hdr, const struct mesa_tcp_hdr *tcp_h) @@ -3171,7 +3187,7 @@ int sapp_dup_pkt_identify_tcp(int tid, struct streaminfo_private *pstream_pr, in sapp_dup_pkt_key_v4_t dup_bloom_key_v4; sapp_dup_pkt_key_v6_t dup_bloom_key_v6; - if((0 == pstream_pr->has_duplicate_pkt) && (tot_pkt >= 3)){ //TODO, ֻжǰN, ˴ǷҪͨļָ + if((0 == pstream_pr->has_duplicate_pkt) && (tot_pkt >= SAPP_DUP_FIRST_PKT_NUM)){ //TODO, ֻжǰN, ˴ǷҪͨļָ return 0; } @@ -3181,7 +3197,7 @@ int sapp_dup_pkt_identify_tcp(int tid, struct streaminfo_private *pstream_pr, in if(is_dup_pkt != 0){ pstream_pr->has_duplicate_pkt = 1; }else{ - sapp_dup_pkt_mark_v4(tid, &dup_bloom_key_v4); + sapp_dup_pkt_mark_v4(tid, &dup_bloom_key_v4); /* ظ,bloom filter */ } }else{ sapp_get_dup_pkt_key_tcp_v6((struct mesa_ip6_hdr *)ip_hdr, tcp_h, &dup_bloom_key_v6); @@ -3189,7 +3205,7 @@ int sapp_dup_pkt_identify_tcp(int tid, struct streaminfo_private *pstream_pr, in if(is_dup_pkt != 0){ pstream_pr->has_duplicate_pkt = 1; }else{ - sapp_dup_pkt_mark_v6(tid, &dup_bloom_key_v6); + sapp_dup_pkt_mark_v6(tid, &dup_bloom_key_v6); /* ظ,bloom filter */ } } diff --git a/src/entry/CMakeLists.txt b/src/entry/CMakeLists.txt index 9097444..aeffba2 100644 --- a/src/entry/CMakeLists.txt +++ b/src/entry/CMakeLists.txt @@ -8,7 +8,6 @@ include_directories(${MESA_SDK_PREFIX}/include) include_directories(${MESA_SDK_PREFIX}/include/MESA) - LINK_DIRECTORIES(/opt/MESA/lib) LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/lib) @@ -16,7 +15,7 @@ if(CAPTURE_MODE MATCHES "MARSIO") LINK_DIRECTORIES(/opt/mrzcpd/lib) endif() -add_executable(sapp sapp_init.c sapp_main.c sapp_global_val.c) +add_executable(sapp sapp_main.c libsapp_phony.a) target_compile_options(sapp PUBLIC ${MEM_POOL_DEFINITIONS}) target_link_libraries(sapp nsl pthread dl m pcap) target_link_libraries(sapp MESA_handle_logger MESA_prof_load) @@ -25,13 +24,35 @@ target_link_libraries(sapp breakpad_mini) target_include_directories(sapp PRIVATE ${SYSTEMD_INCLUDE_DIRS}) -#set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin) -#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) -#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/bin) -#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/bin) +add_custom_command(OUTPUT libsapp_phony.a + COMMAND rm -f *.o + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/dealpkt/libdealpkt.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/packet_io/libpacket_io.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/common/libcommon.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/config/libconfig.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/inner_plug/libinner_plug.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/inner_plug/libgdev_assistant.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/inner_plug/libsapp_assistant.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/plugin/libplugctrl.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/project/libproject.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/timer/libtimer.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/dictator2/src/libdictator2.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/md5/libmd5.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/symbol_check/libsymbol_check.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/tomlc99_wrap/libtomlc99_wrap.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/MESA_socket_wrap/src/libMESA_socket_wrap.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/timestamp_record/libtimestamp_record.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/MESA_sleep/libMESA_sleep.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/support/iknow/libiknow.a + COMMAND ar -x ${CMAKE_BINARY_DIR}/src/sapp_dev/libsapp_dev.a + COMMAND ar -rc libsapp_dev.a *.o + COMMAND cp libsapp_dev.a ${CMAKE_BINARY_DIR}/src/sapp_dev/ + #COMMAND rm -f *.o + ) -set(SAPP_MODULES iknow timestamp_record md5 symbol_check MESA_sleep MESA_socket_wrap packet_io dealpkt project plugctrl common config timer tomlc99_wrap libevent-static gdev_assistant inner_plug libdabloom-static) +#set(SAPP_MODULES iknow timestamp_record md5 symbol_check MESA_sleep MESA_socket_wrap packet_io dealpkt project plugctrl common config timer tomlc99_wrap libevent-static gdev_assistant inner_plug libdabloom-static sapp_dev) +set(SAPP_MODULES libdabloom-static libevent-static sapp_dev) if(MEM_POOL MATCHES "DICTATOR_DEBUG") set(SAPP_MODULES ${SAPP_MODULES} dictator2_debug) diff --git a/src/entry/sapp_init.c b/src/entry/sapp_init.c index 5227140..92acf2f 100644 --- a/src/entry/sapp_init.c +++ b/src/entry/sapp_init.c @@ -161,7 +161,6 @@ int MESA_platform_init(int argc, char *argv[]) return -1; } -#if (0 == SAPP_AS_TARGET_SO) /* Ϊ.soģʽ, dlopenio.so */ if(packet_io_lib_load(ABBR_INTERFACE_TYPE) < 0){ return -1; } @@ -233,7 +232,6 @@ int MESA_platform_init(int argc, char *argv[]) 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(g_iThreadNum); timestamp_record_init(); @@ -314,11 +312,10 @@ int MESA_platform_init(int argc, char *argv[]) sapp_set_current_state(SAPP_STATE_PKT_IO_INITING); -#if (0 == SAPP_AS_TARGET_SO) /* Ϊ.soģʽ, ʼpacket_io */ + if(packet_io_init(argc, argv) < 0){ return -1; } -#endif sapp_dup_pkt_init(); @@ -361,13 +358,6 @@ void MESA_platform_run(void) sleep(1); } - -#if (0 == SAPP_AS_TARGET_SO) - while(1){ - pause(); - } -#endif - } #ifdef __cplusplus diff --git a/src/entry/sapp_main.c b/src/entry/sapp_main.c index 9835518..55dc473 100644 --- a/src/entry/sapp_main.c +++ b/src/entry/sapp_main.c @@ -5,112 +5,18 @@ extern "C" { #endif -static const unsigned char MESA_art_log[] = -{ - 0x50, 0x72, 0x6F, 0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x62, 0x79, 0x0D, 0x0A, 0x20, 0x20, 0x20, - 0x20, 0x5F, 0x5F, 0x20, 0x20, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, - 0x5F, 0x5F, 0x5F, 0x20, 0x5F, 0x5F, 0x5F, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x2F, 0x20, 0x20, 0x7C, - 0x2F, 0x20, 0x20, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x5F, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x2F, 0x2F, - 0x20, 0x20, 0x20, 0x7C, 0x0D, 0x0A, 0x20, 0x20, 0x2F, 0x20, 0x2F, 0x7C, 0x5F, 0x2F, 0x20, 0x2F, - 0x20, 0x5F, 0x5F, 0x2F, 0x20, 0x20, 0x5C, 0x5F, 0x5F, 0x20, 0x5C, 0x2F, 0x20, 0x2F, 0x7C, 0x20, - 0x7C, 0x0D, 0x0A, 0x20, 0x2F, 0x20, 0x2F, 0x20, 0x20, 0x2F, 0x20, 0x2F, 0x20, 0x2F, 0x5F, 0x5F, - 0x5F, 0x20, 0x5F, 0x5F, 0x5F, 0x2F, 0x20, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x20, 0x7C, 0x0D, 0x0A, - 0x2F, 0x5F, 0x2F, 0x20, 0x20, 0x2F, 0x5F, 0x2F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x2F ,0x2F, 0x5F, - 0x5F, 0x5F, 0x5F, 0x2F, 0x5F, 0x2F, 0x20, 0x20, 0x7C, 0x5F, 0x7C, 0x0D, 0x0A, 0x00/* EOF */ -}; -int MESA_platform_init(int argc, char *argv[]); -void MESA_platform_run(void); -extern int sapp_args_v; -int dpdk_init(int argc, char **argv); - - -static void signal_user_handler(int signo) -{ - //time_t last_time = time(NULL); - //while(time(NULL) < last_time + 10); /* wait 10 second, for DPDK IO module detect this process is not running */ - signal(signo, SIG_DFL); - kill(getpid(), signo); -} - -static void signal_hup_handler(int signo) -{ - printf("SIGHUP recviced!\n"); - MESA_handle_runtime_log_reconstruction(NULL); -} - -static void signal_take_over(void) -{ - sapp_config_t *pconfig = &sapp_global_val->config; - signal(SIGHUP, signal_hup_handler); - if (0 == sapp_global_single.signal_take_over_sw && 0 == pconfig->tools.signal_handler.signal) - { - return; - } - - signal(SIGUSR1, signal_user_handler); - signal(SIGUSR2, signal_user_handler); - //signal(pconfig->tools.signal_handler.signal, signal_handler); -} - - -static void show_mesa_log(void) -{ - int i; - - for(i = 0; (i < sizeof(MESA_art_log)) && (MESA_art_log[i] != 0); i ++){ - //putchar(MESA_log[i]); - printf("%c", MESA_art_log[i]); - } - printf("\n"); - sapp_runtime_log(30, "\n\n%s", MESA_art_log); -} int main(int argc, char *argv[]) { - int ret; - - sapp_gval_init(); - sapp_set_current_state(SAPP_STATE_JUST_START); - sapp_set_current_state(SAPP_STATE_CONFIG_PARSE); - - if(argc >= 2){ - /* в, Ƚcla, Ϊʹ-h, -vȰϢ */ - if(sapp_parse_cmd_args(argc, argv) < 0){ - return -1; - } - - ret = sapp_parse_config(); - }else{ - ret = sapp_parse_config(); - } - if(ret < 0){ + if(libsapp_setup_env(argc,argv) < 0){ return -1; } - - sapp_cla_override_cfg_file(); - show_mesa_log(); - -#if SAPP_INSECTICIDE - printf("\033[33m[Warning]This sapp is a temp version for solve confounded bug!\033[0m\n"); -#endif - -#if COMPAT_PAPP_FOR_BENCHMARK - printf("\033[33m[Warning]This sapp is a emasculate version for compare with papp, in other word, it's papp!\033[0m\n"); - sleep(1); -#endif - - sapp_init_breakpad_mini(); - signal_take_over(); - - if(MESA_platform_init(argc, argv) < 0){ - exit(1); + while(1){ + pause(); } - - MESA_platform_run(); - return 0; } diff --git a/src/entry/sapp_plug.c b/src/entry/sapp_plug.c deleted file mode 100644 index 74c0765..0000000 --- a/src/entry/sapp_plug.c +++ /dev/null @@ -1,70 +0,0 @@ -#include "sapp_api.h" -#include "sapp_private_api.h" - - -#ifdef __cplusplus -extern "C" { -#endif - -#if SAPP_AS_TARGET_SO -extern int MESA_platform_init(int argc, char *argv[]); -extern int ipv4_entry(struct streaminfo_private *pfstream_pr, const void *this_layer_data, int thread_num, - unsigned char routedir, const raw_pkt_t *raw_pkt, int offset_to_raw_pkt_hdr); -extern void packet_io_clean_thread_context(int thread_seq); -extern void MESA_platform_run(void); -extern int sapp_parse_config(void); - -#define DUMPFILE_EOF_FLAG (void*)0xF1E2D3C4B5A6 /* һرʾǰdumpfileģʽѽ, ڴ */ - -char MESA_IP_PLUG_ENTRY(const struct streaminfo *pstream,unsigned char routedir,int thread_seq, const struct mesa_ip4_hdr *ipv4_hdr) -{ - char ret = PASS; - raw_pkt_t raw_pkt; - - if(DUMPFILE_EOF_FLAG == pstream){ - packet_io_clean_thread_context(thread_seq); - sleep(3); - return PASS; - } - - raw_pkt.magic_num = RAW_PKT_MAGIC_NUM; - raw_pkt.offset_to_raw_pkt_hdr = 0; - raw_pkt.low_layer_type = (enum addr_type_t)CAP_LEVEL_IPV4; - raw_pkt.raw_pkt_len = ntohs(ipv4_hdr->ip_len); - raw_pkt.raw_pkt_data = ipv4_hdr; - raw_pkt.hd_hash = 0; - - ret = ipv4_entry(NULL, ipv4_hdr, thread_seq, 0, &raw_pkt, 0); - - return ret; -} - -int MESA_PLUG_INIT(void) -{ - int i; - pthread_t pid; - - sapp_gval_init(); - sapp_set_current_state(SAPP_STATE_JUST_START); - sapp_set_current_state(SAPP_STATE_CONFIG_PARSE); - - sapp_parse_config(); - - if(MESA_platform_init(0, NULL) < 0){ - sapp_runtime_log(30, "IIE plug init error!\n"); - return -1; - } - - g_packet_io_cap_level = CAP_LEVEL_IPV4; /* صIP, ǿָΪIPv4 */ - - MESA_platform_run(); - - return 1; -} - -#endif - -#ifdef __cplusplus -} -#endif - diff --git a/src/packet_io/cycle_pkt_dump_through_write_offset.c b/src/packet_io/cycle_pkt_dump_through_write_offset.c index 5547b4d..0666f01 100644 --- a/src/packet_io/cycle_pkt_dump_through_write_offset.c +++ b/src/packet_io/cycle_pkt_dump_through_write_offset.c @@ -450,15 +450,19 @@ static void pkt_dump_with_watermark(int tid, const void *rawpkt, int pktlen, enu case PKT_CLASSIFY_DROP: watermaker = 'D'; break; + case PKT_CLASSIFY_ERROR: watermaker = 'E'; - break; + break; + + case PKT_CLASSIFY_REPEAT: + watermaker = 'R'; + break; } memset(ehdr->ether_shost, watermaker, ETH_ALEN); memset(ehdr->ether_dhost, watermaker, ETH_ALEN); - /* bingo, match filter, sendto */ ret = sendto(pkt_dump_udp_pkt_sd[tid], (void *)ehdr, /* ʱʹڲݰͷ, Ҫԭʼ */ diff --git a/src/packet_io/packet_io.c b/src/packet_io/packet_io.c index 49ee468..df11a5b 100644 --- a/src/packet_io/packet_io.c +++ b/src/packet_io/packet_io.c @@ -739,8 +739,7 @@ int packet_io_init(int argc, char *argv[]) return -1; } } - -#if (0 == SAPP_AS_TARGET_SO) + if(0 == use_custom_pkt_cb){ packet_io_register_cb(mesa_default_pkt_cb); } @@ -753,7 +752,6 @@ int packet_io_init(int argc, char *argv[]) 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); @@ -796,21 +794,18 @@ extern int app_function_rationality_check(void); void packet_io_run(void) { pthread_t pid; -#if (0 == SAPP_AS_TARGET_SO) + if(NULL == dl_io_fun_list.dl_io_run){ printf("Error, packet io library must support 'dl_io_run' !\n"); sapp_runtime_log(RLOG_LV_FATAL, "%s:%d: Error, packet io library must support 'dl_io_run' !\n", __FILE__, __LINE__); assert(0); } -#endif app_function_rationality_check(); pthread_create(&pid, NULL, sapp_time_event_thread ,NULL); -#if (0 == SAPP_AS_TARGET_SO) dl_io_fun_list.dl_io_run(); -#endif } extern volatile int update_packet_io_status_sw; diff --git a/src/packet_io/sendpacket.c b/src/packet_io/sendpacket.c index 5937763..03f1c81 100644 --- a/src/packet_io/sendpacket.c +++ b/src/packet_io/sendpacket.c @@ -827,39 +827,39 @@ static int calc_mpls_hdr_len(struct streaminfo_private * stream_pr, UCHAR send_s if(g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_MPLS][stream_pr->layer_index] != 0){ if(send_stream_dir == DIR_C2S){ - mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->src_mpls_layer_num; - if(mpls_addr->src_has_ctrl_word){ + mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->c2s_layer_num; + if(mpls_addr->c2s_has_ctrl_word){ mpls_layer_len += sizeof(int); } }else{ - mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->dest_mpls_layer_num; - if(mpls_addr->dst_has_ctrl_word){ + mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->s2c_layer_num; + if(mpls_addr->s2c_has_ctrl_word){ mpls_layer_len += sizeof(int); } } }else{ /* ǿҪǶԳƵַ, ûеַ, ʹöԲַϢ */ if(send_stream_dir == DIR_C2S){ - if(mpls_addr->src_mpls_layer_num > 0){ - mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->src_mpls_layer_num; - if(mpls_addr->src_has_ctrl_word){ + if(mpls_addr->c2s_layer_num > 0){ + mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->c2s_layer_num; + if(mpls_addr->c2s_has_ctrl_word){ mpls_layer_len += sizeof(int); } }else{ - mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->dest_mpls_layer_num; - if(mpls_addr->dst_has_ctrl_word){ + mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->s2c_layer_num; + if(mpls_addr->s2c_has_ctrl_word){ mpls_layer_len += sizeof(int); } } }else{ - if(mpls_addr->dest_mpls_layer_num > 0){ - mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->dest_mpls_layer_num; - if(mpls_addr->dst_has_ctrl_word){ + if(mpls_addr->s2c_layer_num > 0){ + mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->s2c_layer_num; + if(mpls_addr->s2c_has_ctrl_word){ mpls_layer_len += sizeof(int); } }else{ - mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->src_mpls_layer_num; - if(mpls_addr->src_has_ctrl_word){ + mpls_layer_len = sizeof(struct mesa_mpls_hdr) * mpls_addr->c2s_layer_num; + if(mpls_addr->c2s_has_ctrl_word){ mpls_layer_len += sizeof(int); } } @@ -876,23 +876,23 @@ static int calc_vlan_hdr_len(struct streaminfo_private * stream_pr, UCHAR send_s if(g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_VLAN][stream_pr->layer_index] != 0){ if(send_stream_dir == DIR_C2S){ - vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->src_vlan_layer_num; + vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->c2s_layer_num; }else{ - vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->dst_vlan_layer_num; + vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->s2c_layer_num; } }else{ /* ǿҪǶԳƵַ, û, ʹöԲַϢ */ if(send_stream_dir == DIR_C2S){ - if(vlan_addr->src_vlan_layer_num != 0){ - vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->src_vlan_layer_num; + if(vlan_addr->c2s_layer_num != 0){ + vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->c2s_layer_num; }else{ - vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->dst_vlan_layer_num; + vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->s2c_layer_num; } }else{ - if(vlan_addr->dst_vlan_layer_num != 0){ - vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->dst_vlan_layer_num; + if(vlan_addr->s2c_layer_num != 0){ + vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->s2c_layer_num; }else{ - vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->src_vlan_layer_num; + vlan_layer_len = sizeof(struct mesa_vlan_hdr) * vlan_addr->c2s_layer_num; } } } @@ -1444,6 +1444,43 @@ static int build_net_layer_pppoe_ses(struct streaminfo_private *stream_pr, int c return sizeof(struct mesa_pppoe_session_hdr); } +/* 紫ĵַתΪڴṹ, ҵ */ +int vlan_addr_net_to_mem(const struct mesa_vlan_detail_hdr *net_vlan_hdr, struct single_layer_vlan_addr *mem_vlan_hdr) +{ + mem_vlan_hdr->VID = htons(net_vlan_hdr->vlan_id_high << 8 | net_vlan_hdr->vlan_id_low); + mem_vlan_hdr->TPID = net_vlan_hdr->type; + mem_vlan_hdr->PCP = net_vlan_hdr->priority; + mem_vlan_hdr->DEI = net_vlan_hdr->del_flag; + + return 0; +} + +/* ڴַṹתΪRFC׼ͷʽ, ͵ */ +int vlan_addr_mem_to_net(const struct single_layer_vlan_addr *mem_vlan_hdr, struct mesa_vlan_detail_hdr *net_vlan_hdr) +{ + net_vlan_hdr->vlan_id_high = (ntohs(mem_vlan_hdr->VID) & 0xF00) >> 8; + net_vlan_hdr->vlan_id_low = (ntohs(mem_vlan_hdr->VID) & 0xFF); + net_vlan_hdr->type = mem_vlan_hdr->TPID; + net_vlan_hdr->priority = mem_vlan_hdr->PCP; + net_vlan_hdr->del_flag = mem_vlan_hdr->DEI; + + return 0; +} + + +static int build_net_multilayer_vlan(const struct single_layer_vlan_addr *layer_array, int layer_num, struct mesa_vlan_detail_hdr *net_vlan_hdr) +{ + int i; + + for( i = 0; i < layer_num; i++){ + vlan_addr_mem_to_net(&layer_array[i], net_vlan_hdr); + net_vlan_hdr++; + } + + return 0; +} + + static int build_net_layer_vlan(struct streaminfo_private *stream_pr, int carry_layer_type, int carry_layer_len, unsigned char *buf, UCHAR send_stream_dir, const raw_pkt_t *raw_pkt) { @@ -1453,25 +1490,35 @@ static int build_net_layer_vlan(struct streaminfo_private *stream_pr, int carry_ in_stream_addr = (struct layer_addr_vlan *)(stream_pr->stream_public.addr.vlan); if(DIR_C2S == send_stream_dir){ - if(in_stream_addr->src_vlan_layer_num > 0){ - layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->src_vlan_layer_num; - memcpy(buf, in_stream_addr->src_vlan_pkt, layer_len); + if(in_stream_addr->c2s_layer_num > 0){ + layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->c2s_layer_num; + //memcpy(buf, in_stream_addr->c2s_addr_array, layer_len); + build_net_multilayer_vlan(in_stream_addr->c2s_addr_array, in_stream_addr->c2s_layer_num, (struct mesa_vlan_detail_hdr *)buf); }else{ /* ûеַ, ȡļasymmetric_addr_layerǷöԲַ */ if(0 == g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_VLAN][stream_pr->layer_index]){ - layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->dst_vlan_layer_num; - memcpy(buf, in_stream_addr->dst_vlan_pkt, layer_len); + layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->s2c_layer_num; + build_net_multilayer_vlan(in_stream_addr->s2c_addr_array, in_stream_addr->s2c_layer_num, (struct mesa_vlan_detail_hdr *)buf); + ///memcpy(buf, in_stream_addr->s2c_addr_array, layer_len); + }else{ + sapp_runtime_log(RLOG_LV_FATAL, "build_net_layer_vlan() error, no c2s vlan addr, but asymmetric_addr_layer is set!\n"); + return -1; } } }else{ - if(in_stream_addr->dst_vlan_layer_num > 0){ - layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->dst_vlan_layer_num; - memcpy(buf, in_stream_addr->dst_vlan_pkt, layer_len); + if(in_stream_addr->s2c_layer_num > 0){ + layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->s2c_layer_num; + //memcpy(buf, in_stream_addr->s2c_addr_array, layer_len); + build_net_multilayer_vlan(in_stream_addr->s2c_addr_array, in_stream_addr->s2c_layer_num, (struct mesa_vlan_detail_hdr *)buf); }else{ /* ûеַ, ȡļasymmetric_addr_layerǷöԲַ */ if(0 == g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_VLAN][stream_pr->layer_index]){ - layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->src_vlan_layer_num; - memcpy(buf, in_stream_addr->src_vlan_pkt, layer_len); + layer_len = sizeof(struct mesa_vlan_hdr) * in_stream_addr->c2s_layer_num; + //memcpy(buf, in_stream_addr->c2s_addr_array, layer_len); + build_net_multilayer_vlan(in_stream_addr->c2s_addr_array, in_stream_addr->c2s_layer_num, (struct mesa_vlan_detail_hdr *)buf); + }else{ + sapp_runtime_log(RLOG_LV_FATAL, "build_net_layer_vlan() error, no s2c vlan addr, but asymmetric_addr_layer is set!\n"); + return -1; } } } @@ -1622,6 +1669,46 @@ static int build_net_layer_gre(struct streaminfo_private *stream_pr, int carry_l return gre_hdr_len; } +/* 紫ĵַתΪڴṹ, ҵ */ +int mpls_addr_net_to_mem(const struct mesa_mpls_hdr *net_mpls_hdr, struct single_layer_mpls_addr *mem_mpls_hdr) +{ + memset(mem_mpls_hdr, 0, sizeof(struct single_layer_mpls_addr)); + + mem_mpls_hdr->label = htonl( (net_mpls_hdr->mpls_label_low<<12) | (net_mpls_hdr->mpls_label_mid<<4) | net_mpls_hdr->mpls_label_high ); /* network order */ + mem_mpls_hdr->experimental = net_mpls_hdr->mpls_exp; + mem_mpls_hdr->bottom = net_mpls_hdr->mpls_bls; + mem_mpls_hdr->ttl = net_mpls_hdr->mpls_ttl; + + return 0; +} + +/* ڴַṹתΪRFC׼ͷʽ, ͵ */ +int mpls_addr_mem_to_net(const struct single_layer_mpls_addr *mem_mpls_hdr, struct mesa_mpls_hdr *net_mpls_hdr) +{ + memset(net_mpls_hdr, 0, sizeof(struct mesa_mpls_hdr)); + + net_mpls_hdr->mpls_label_low = (ntohl(mem_mpls_hdr->label) & 0xFF000) >> 12; + net_mpls_hdr->mpls_label_mid = (ntohl(mem_mpls_hdr->label) & 0xFF0) >> 4; + net_mpls_hdr->mpls_label_high = ntohl(mem_mpls_hdr->label) & 0xF; + net_mpls_hdr->mpls_exp = mem_mpls_hdr->experimental; + net_mpls_hdr->mpls_bls = mem_mpls_hdr->bottom; + net_mpls_hdr->mpls_ttl = mem_mpls_hdr->ttl; + + return 0; +} + + +static int build_net_multilayer_mpls(const struct single_layer_mpls_addr *layer_array, int layer_num, struct mesa_mpls_hdr *net_mpls_hdr) +{ + int i; + + for( i = 0; i < layer_num; i++){ + mpls_addr_mem_to_net(&layer_array[i], net_mpls_hdr); + net_mpls_hdr++; + } + + return 0; +} static int build_net_layer_mpls(struct streaminfo_private *stream_pr, int carry_layer_type, @@ -1633,41 +1720,52 @@ static int build_net_layer_mpls(struct streaminfo_private *stream_pr, int carry_ in_stream_addr = (struct layer_addr_mpls *)(stream_pr->stream_public.addr.mpls); if(DIR_C2S == send_stream_dir){ - if(in_stream_addr->src_mpls_layer_num > 0){ - layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->src_mpls_layer_num; - memcpy(buf, in_stream_addr->src_mpls_pkt, layer_len); - if(in_stream_addr->src_has_ctrl_word){ - memcpy(buf + layer_len, &in_stream_addr->src_mpls_ctrl_word, sizeof(int)); + if(in_stream_addr->c2s_layer_num > 0){ + layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->c2s_layer_num; + //memcpy(buf, in_stream_addr->src_mpls_pkt, layer_len); + build_net_multilayer_mpls(in_stream_addr->c2s_addr_array, in_stream_addr->c2s_layer_num, (struct mesa_mpls_hdr *)buf); + + if(in_stream_addr->c2s_has_ctrl_word){ + memcpy(buf + layer_len, &in_stream_addr->c2s_mpls_ctrl_word, sizeof(int)); layer_len += sizeof(int); } }else{ /* ûеַ, ȡļasymmetric_addr_layerǷöԲַ */ if(0 == g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_MPLS][stream_pr->layer_index]){ - layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->dest_mpls_layer_num; - memcpy(buf, in_stream_addr->dest_mpls_pkt, layer_len); - if(in_stream_addr->dst_has_ctrl_word){ - memcpy(buf + layer_len, &in_stream_addr->dst_mpls_ctrl_word, sizeof(int)); + layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->s2c_layer_num; + //memcpy(buf, in_stream_addr->dest_mpls_pkt, layer_len); + build_net_multilayer_mpls(in_stream_addr->s2c_addr_array, in_stream_addr->s2c_layer_num, (struct mesa_mpls_hdr *)buf); + if(in_stream_addr->s2c_has_ctrl_word){ + memcpy(buf + layer_len, &in_stream_addr->s2c_mpls_ctrl_word, sizeof(int)); layer_len += sizeof(int); } + }else{ + sapp_runtime_log(RLOG_LV_FATAL, "build_net_layer_mpls() error, no c2s mpls addr, but asymmetric_addr_layer is set!\n"); + return -1; } } }else{ - if(in_stream_addr->dest_mpls_layer_num > 0){ - layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->dest_mpls_layer_num; - memcpy(buf, in_stream_addr->dest_mpls_pkt, layer_len); - if(in_stream_addr->dst_has_ctrl_word){ - memcpy(buf + layer_len, &in_stream_addr->dst_mpls_ctrl_word, sizeof(int)); + if(in_stream_addr->s2c_layer_num > 0){ + layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->s2c_layer_num; + //memcpy(buf, in_stream_addr->dest_mpls_pkt, layer_len); + build_net_multilayer_mpls(in_stream_addr->s2c_addr_array, in_stream_addr->s2c_layer_num, (struct mesa_mpls_hdr *)buf); + if(in_stream_addr->s2c_has_ctrl_word){ + memcpy(buf + layer_len, &in_stream_addr->s2c_mpls_ctrl_word, sizeof(int)); layer_len += sizeof(int); } }else{ /* ûеַ, ȡļasymmetric_addr_layerǷöԲַ */ if(0 == g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_MPLS][stream_pr->layer_index]){ - layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->src_mpls_layer_num; - memcpy(buf, in_stream_addr->src_mpls_pkt, layer_len); - if(in_stream_addr->src_has_ctrl_word){ - memcpy(buf + layer_len, &in_stream_addr->src_mpls_ctrl_word, sizeof(int)); + layer_len = sizeof(struct mesa_mpls_hdr) * in_stream_addr->c2s_layer_num; + //memcpy(buf, in_stream_addr->src_mpls_pkt, layer_len); + build_net_multilayer_mpls(in_stream_addr->c2s_addr_array, in_stream_addr->c2s_layer_num, (struct mesa_mpls_hdr *)buf); + if(in_stream_addr->c2s_has_ctrl_word){ + memcpy(buf + layer_len, &in_stream_addr->c2s_mpls_ctrl_word, sizeof(int)); layer_len += sizeof(int); } + }else{ + sapp_runtime_log(RLOG_LV_FATAL, "build_net_layer_mpls() error, no s2c mpls addr, but asymmetric_addr_layer is set!\n"); + return -1; } } } @@ -1845,6 +1943,59 @@ static int build_net_layer_l2tp(struct streaminfo_private *stream_pr, int carry_ return l2tp_raw_hdr_len; } + +/* ֮ǰظ, ÿǰNҪbloom filter */ +static void sendpkt_dup_pkt_mark(const struct streaminfo *top_stream, unsigned char addrtype, const char *this_iphdr, const char *this_transport_hdr) +{ + const struct streaminfo_private *top_stream_pr; + int need_process_dup_pkt_flag = 0; + + top_stream_pr = (struct streaminfo_private *)top_stream; + if(top_stream_pr->has_duplicate_pkt){ + need_process_dup_pkt_flag = 1; + }else{ + if(STREAM_TYPE_TCP == top_stream->type){ + if(top_stream->ptcpdetail->clientpktnum + top_stream->ptcpdetail->serverpktnum < SAPP_DUP_FIRST_PKT_NUM){ + need_process_dup_pkt_flag = 1; + } + }else{ + if(top_stream->pudpdetail->clientpktnum + top_stream->pudpdetail->serverpktnum < SAPP_DUP_FIRST_PKT_NUM){ + need_process_dup_pkt_flag = 1; + } + } + } + + if(need_process_dup_pkt_flag != 0){ + if(STREAM_TYPE_TCP == top_stream->type){ + sapp_dup_pkt_mark_tcp(top_stream->threadnum, addrtype, this_iphdr, (const struct mesa_tcp_hdr *)(this_transport_hdr)); + }else{ + sapp_dup_pkt_mark_udp(top_stream->threadnum, addrtype, this_iphdr, (const struct mesa_udp_hdr *)(this_transport_hdr)); + } + } + + return; +} + +void sendpkt_dup_pkt_mark_stream(const struct streaminfo *top_stream, const char *ip_hdr) +{ + const struct mesa_ip4_hdr *ip4hdr; + const struct mesa_ip6_hdr *ip6hdr; + sapp_dup_pkt_key_v4_t dup_bloom_key_v4; + sapp_dup_pkt_key_v6_t dup_bloom_key_v6; + const char *transport_layer_hdr; + + if(ADDR_TYPE_IPV4 == top_stream->addr.addrtype){ + ip4hdr = (struct mesa_ip4_hdr *)ip_hdr; + transport_layer_hdr = ip_hdr + ip4hdr->ip_hl * 4; + sendpkt_dup_pkt_mark(top_stream, ADDR_TYPE_IPV4, ip_hdr, transport_layer_hdr); + }else{ + ip6hdr = (struct mesa_ip6_hdr *)ip_hdr; + transport_layer_hdr = ip_hdr + sizeof(struct mesa_ip6_hdr); + sendpkt_dup_pkt_mark(top_stream, ADDR_TYPE_IPV6, ip_hdr, transport_layer_hdr); + } +} + + /* buf: ִӦò㸺صַ, skip hdr. reverse: ӦòĽǶdir, ʾ뵱ǰraw_pkt, Ƿһ, зԵĵַ, 跴. @@ -1863,6 +2014,7 @@ static int build_net_layer_pkt(const struct streaminfo *top_stream, const struct char debug_print_buf[256]; const struct streaminfo *carry_layer_stream; + if(NULL == stream_pr){ return carry_layer_len; } @@ -1903,6 +2055,11 @@ static int build_net_layer_pkt(const struct streaminfo *top_stream, const struct sapp_runtime_log(RLOG_LV_DEBUG, "build_net_layer_pkt()->IPV4, %s, send_stream_dir:%d, ipid:%u, ip_checksum:0x%x", sapp_raw_ipv4_ntop(ip4hdr, debug_print_buf, sizeof(debug_print_buf)), send_stream_dir, ntohs(ip4hdr->ip_id), ntohs(ip4hdr->ip_sum)); + + /* ظʶ, ӵΪ, ܻж, Ҳپjump_layerתڲudp/tcp */ + if(sapp_global_val->config.packet_io.dup_pkt_para.dup_pkt_distinguish_enable != 0){ + sendpkt_dup_pkt_mark(top_stream, ADDR_TYPE_IPV4, (char *)buf, buf+sizeof(struct mesa_ip4_hdr)); + } } break; @@ -1928,6 +2085,11 @@ static int build_net_layer_pkt(const struct streaminfo *top_stream, const struct /* TCPУҪIPַϢ, ҪڹIPϢ, ټchecksum */ checksum_for_carry_layer(buf, __ADDR_TYPE_IP_PAIR_V6, carry_layer_type, carry_layer_len+transport_layer_len); carry_layer_type = __ADDR_TYPE_IP_PAIR_V6; + + /* ظʶ, ӵΪ, ܻж, Ҳپjump_layerתڲudp/tcp */ + if(sapp_global_val->config.packet_io.dup_pkt_para.dup_pkt_distinguish_enable != 0){ + sendpkt_dup_pkt_mark(top_stream, ADDR_TYPE_IPV6, (char *)buf, buf+sizeof(struct mesa_ip6_hdr)); + } break; case __ADDR_TYPE_IP_PAIR_V4: @@ -1956,7 +2118,11 @@ static int build_net_layer_pkt(const struct streaminfo *top_stream, const struct /* ÷ַ, ¼㳤, vlanַ */ this_layer_len = calc_vlan_hdr_len(stream_pr, send_stream_dir ^ DIR_DOUBLE); buf -= this_layer_len; - build_net_layer_vlan(stream_pr, carry_layer_type, carry_layer_len, buf, send_stream_dir ^ DIR_DOUBLE, raw_pkt); + ret = build_net_layer_vlan(stream_pr, carry_layer_type, carry_layer_len, buf, send_stream_dir ^ DIR_DOUBLE, raw_pkt); + if(ret < 0){ + sapp_runtime_log(RLOG_LV_FATAL, "build_net_layer_vlan() error!\n"); + return -1; + } carry_layer_type = ADDR_TYPE_VLAN; sapp_runtime_log(10, "build_net_layer_vlan() build vlan layer, but no addr of dir:%d, use opposite_addr.\n", send_stream_dir); }else{ @@ -1965,7 +2131,11 @@ static int build_net_layer_pkt(const struct streaminfo *top_stream, const struct } }else{ buf -= this_layer_len; - build_net_layer_vlan(stream_pr, carry_layer_type, carry_layer_len, buf, send_stream_dir, raw_pkt); + ret = build_net_layer_vlan(stream_pr, carry_layer_type, carry_layer_len, buf, send_stream_dir, raw_pkt); + if(ret < 0){ + sapp_runtime_log(RLOG_LV_FATAL, "build_net_layer_vlan() error!\n"); + return -1; + } carry_layer_type = ADDR_TYPE_VLAN; } break; @@ -2019,7 +2189,11 @@ static int build_net_layer_pkt(const struct streaminfo *top_stream, const struct break; }else{ buf -= this_layer_len; - build_net_layer_mpls(stream_pr, carry_layer_type, carry_layer_len, buf, send_stream_dir, raw_pkt); + ret = build_net_layer_mpls(stream_pr, carry_layer_type, carry_layer_len, buf, send_stream_dir, raw_pkt); + if(ret < 0){ + sapp_runtime_log(RLOG_LV_FATAL, "build_net_layer_mpls() error!\n"); + return -1; + } carry_layer_type = ADDR_TYPE_MPLS; } @@ -2939,14 +3113,14 @@ static int skip_asymmetric_presence_layer(const struct streaminfo *stream, UCHAR /* ղ, пǵ, Ҫʵûеַ */ if(DIR_C2S == send_stream_dir){ - if(0 == vaddr->src_vlan_layer_num){ + if(0 == vaddr->c2s_layer_num){ /* ûеַ, ǷǿҪǶԳԭ, , ʵ߲,ʱʧ; ǿԸöԲַ */ if(g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_VLAN][stream_pr->layer_index] != 0){ need_skip = 1; } } }else{ - if(0 == vaddr->dst_vlan_layer_num){ + if(0 == vaddr->s2c_layer_num){ /* ûеַ, ǷǿҪǶԳԭ, , ʵ߲,ʱʧ; ǿԸöԲַ */ if(g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_VLAN][stream_pr->layer_index] != 0){ need_skip = 1; @@ -2961,14 +3135,14 @@ static int skip_asymmetric_presence_layer(const struct streaminfo *stream, UCHAR const struct layer_addr_mpls *maddr = stream->addr.mpls; if(DIR_C2S == send_stream_dir){ - if(0 == maddr->src_mpls_layer_num){ + if(0 == maddr->c2s_layer_num){ /* ûеַ, ǷǿҪǶԳԭ, , ʵ߲,ʱʧ; ǿԸöԲַ */ if(g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_MPLS][stream_pr->layer_index] != 0){ need_skip = 1; } } }else{ - if(0 == maddr->dest_mpls_layer_num){ + if(0 == maddr->s2c_layer_num){ if(g_asymmetric_addr_layer_set.layer_type_index[ADDR_TYPE_MPLS][stream_pr->layer_index] != 0){ need_skip = 1; } @@ -3055,6 +3229,11 @@ int __sapp_inject_pkt(struct streaminfo *raw_stream, enum sapp_inject_opt sio, /* ǰ */ inject_stream = raw_stream->pfather; inject_stream_pr = (struct streaminfo_private *)inject_stream; + /* ʱָSIO_EXCLUDE_THIS_LAYER_HDR, ˵ڲԪɲ, + һ㲻ߵbuild_pktĺ, ˴Ҫipbloom filter */ + if(sapp_global_val->config.packet_io.dup_pkt_para.dup_pkt_distinguish_enable != 0){ + sendpkt_dup_pkt_mark_stream(raw_stream, (char *)payload); + } }else{ if((ADDR_TYPE_IPV4 == inject_stream->addr.addrtype) || (ADDR_TYPE_TCP == inject_stream->addr.addrtype)){ @@ -3230,13 +3409,12 @@ static int send_handle_init(int tot_thread_num) } g_send_handle[i].threadnum = i; -#if (0 == SAPP_AS_TARGET_SO) g_send_handle[i].low_level_send_handle = dl_io_fun_list.dl_io_get_send_handle(i); if(NULL == g_send_handle[i].low_level_send_handle){ printf("dl_io_get_send_handle error !\n"); return -1; } -#endif + g_packet_dl_send_handle[i] = g_send_handle[i].low_level_send_handle; /* 20161117 lijia move from packet_io_init() */ } diff --git a/src/plugin/src/plugin.c b/src/plugin/src/plugin.c index 7595ec0..73a1bbe 100644 --- a/src/plugin/src/plugin.c +++ b/src/plugin/src/plugin.c @@ -695,9 +695,9 @@ int process_confelem_sofilename(char* filename,int plugtype,stProtocolPlugInfo* fun_init = (int (*)())(dlsym(filepoint,buf_funname)); //add by yw 20150318, reset cwd after init plugin -#if (0 == SAPP_AS_TARGET_SO) + reset_cwd(); -#endif + if(fun_init == NULL) { MESA_handle_runtime_log(g_plugin_runtime_handle,RLOG_LV_FATAL, PLUGIN_LOGNAME,"%s no function %s error!",buf_sofilename,buf_funname); diff --git a/src/sapp_dev/CMakeLists.txt b/src/sapp_dev/CMakeLists.txt new file mode 100644 index 0000000..0b36d69 --- /dev/null +++ b/src/sapp_dev/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 2.8) + +include_directories(${CMAKE_SOURCE_DIR}/include) +include_directories(${CMAKE_SOURCE_DIR}/include/private) +include_directories(${CMAKE_SOURCE_DIR}/include/public) +include_directories(${CMAKE_SOURCE_DIR}/include/support) +include_directories(${MESA_SDK_PREFIX}/include) +include_directories(${MESA_SDK_PREFIX}/include/MESA) + +add_definitions(-D_BSD_SOURCE -D_BSD_SOURCE -D__BSD_SOURCE -D__FAVOR_BSD -DHAVE_NET_ETHERNET_H) + +add_library(sapp_dev STATIC sapp_plug.c sapp_global_val.c sapp_init.c) + +#add_custom_target(libsapp_plug.a +#j. COMMAND rm -f *.o + #COMMAND ar -x ${CMAKE_BINARY_DIR}/src/dealpkt/libdealpkt.a + #COMMAND ar -rc libsapp_plug.a *.o + #) + +#add_library(sapp_plug STATIC IMPORTED GLOBAL) diff --git a/src/sapp_dev/sapp_global_val.c b/src/sapp_dev/sapp_global_val.c new file mode 100644 index 0000000..717bb6d --- /dev/null +++ b/src/sapp_dev/sapp_global_val.c @@ -0,0 +1,222 @@ +#ifdef __cplusplus +extern "C" { +#endif + +#include "sapp_api.h" +#include "sapp_private_api.h" + +sapp_global_t *sapp_global_val; + +struct sapp_global_single_t sapp_global_single; +struct sapp_global_mthread_t sapp_global_mthread[MAX_THREAD_NUM]; + +//extern int g_packet_io_thread_num; +extern int tcpstate_num[MAX_TCP_STATE]; +extern int udpstate_num[MAX_UDP_STATE]; + +/* ʱʹȫֱ, Ժ̬ȡļ, + overlay㶨, vxlanĻ, overlayIJ, eth[0], ipv4[1], udp[2], vxlan[3], Щ㲻ԭʼ, Ϊ˱ڷIJ, ܵò, + ʵʻעʱҲҪЯ, + + TODO, vxlanô? + gdev_keepaliveģƵsappڲ, Ϊ.so, ʹsapp.tomlؿƶǿplug conflist. + + TODO: ƶȫֱsapp_global_val. +*/ +int g_overlay_layer_set[__ADDR_TYPE_MAX][SAPP_SUPPORT_LAYER_NUM_MAX]; + +/* + ע޼㶨, ʱ˲, + mirrorģʽ, ע㲻һʱ, rstҪvlan, mpls, gtpȲ, + Ϊoverlay, overlayʽ϶Ԥȹ涨õ, ʵһԤָijЭIJ, + ֧ģ, *ʾ, vlan[*], gtp[*], ʾʱ˲϶, һ, + ip-in-ip, ip-gre-ip, 4over6, 6over4, ipֱעڲipԪ, ǷdzΣյ, + Ϊдip[*], Ҳдip[1], ip[2], ѲĴip/tcp, + + óһַ, ͬʱָв, . +*/ + +/* + ʱͬʱһprune, ˳Ǵinternalexternal, + ֧N, mirrorģʽ, ײethernetдд, + + ĿgtpԭʼΪ: ethernet/ipv4/udp/gtp/ipv4/tcp/http, gtp: "gtp[3],udp[2],ip[1]", + + ijGREԭʼΪ: ethernet/ipv4/gre/ipv4/udp/dns, : "gre[2],ip[1]", + TODO: ƶȫֱsapp_global_val. +*/ + +embed_layer_t g_prune_inject_layer_set[SAPP_PRUNE_LAYER_NUM_MAX]; + + +/* + ЩǷǶԳƴڲ, C2SS2Cпûһ,´compare_addrʱ಻Գ, Ҫһղ, עʱע, Ҫ. + TODO: ƶȫֱsapp_global_val. +*/ +asymmetric_presence_layer_t g_asymmetric_presence_layer_set[__ADDR_TYPE_MAX]; + + +/* + g_asymmetric_presence_layer_set岻ͬ, ЩDzһ˫ַһµIJ, + C2SS2CIJȻһ,˫ĵַһ, gtp, gre, mpls, ethernet, vlanҲп. + עݰ, ˫, + Ҫ¶ԲϢ, עʱע. + ǵ, ҿ, עʧܵ!! + TODO: ƶȫֱsapp_global_val. +*/ +embed_layer_t g_asymmetric_addr_layer_set; + + +/* + ָЩЭΪַ, Ψһȷһ, 粻ͬڶʹ˽еַʱ, пܳͻ, Ӧðѵײvlan,mplsȲҲΪַȽϲ, + TODO: ƶȫֱsapp_global_val. +*/ +embed_layer_t g_stream_compare_layer_set; + + +extern char *MESA_MD5_sum_str(unsigned char *raw_data, unsigned int raw_data_len, char result[33]); + +static off_t _check_hdr_get_file_length(const char *file_name) +{ + struct stat file_stat; + + if(stat(file_name, &file_stat) == 0) + { + return file_stat.st_size; + } + return 0; +} + +static void sapp_include_header_check_file(const char *file_name, const char *std_md5_sum_str) +{ + char file_md5_sum_str[33]; + FILE *fp; + char *tmp_file_buf; + off_t file_len; + + fp = fopen(file_name, "r"); + if(NULL == fp){ + return; + } + + file_len = _check_hdr_get_file_length(file_name); + tmp_file_buf = (char *)malloc(file_len); + + fread(tmp_file_buf, file_len, 1, fp); + + MESA_MD5_sum_str((unsigned char *)tmp_file_buf, file_len, file_md5_sum_str); + + if(memcmp(file_md5_sum_str, std_md5_sum_str, 32) != 0){ + printf("\033[33m[Warning]stream_inc header file %s is disaccord with current sapp!\033[0m\n", file_name); + usleep(200 * 1000); + } + + fclose(fp); + free(tmp_file_buf); + + return; +} + +/* ⵱ǰлµ.h뻷Ƿһ */ +void sapp_include_header_check(void) +{ + const char *hdr_dir; + char tmp_file_path[1024]; + + hdr_dir = "/opt/MESA/include/MESA/stream_inc"; + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_base.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_BASE_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_control.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_CONTROL_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_entry.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_ENTRY_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_inject.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_INJECT_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_project.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_PROJECT_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_proxy.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_PROXY_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_rawpkt.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_RAWPKT_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_tunnel.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_TUNNEL_MD5_CHECK); + + hdr_dir = "/usr/include/MESA/stream_inc"; + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_base.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_BASE_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_control.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_CONTROL_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_entry.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_ENTRY_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_inject.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_INJECT_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_project.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_PROJECT_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_proxy.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_PROXY_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_rawpkt.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_RAWPKT_MD5_CHECK); + + snprintf(tmp_file_path, 1024, "%s/%s", hdr_dir, "stream_tunnel.h"); + sapp_include_header_check_file(tmp_file_path, STREAM_TUNNEL_MD5_CHECK); +} + +static inline int mem_alignment_pad(int size) +{ + return 64 - (size % 64); +} + +void sapp_global_val_sanity_check(void) +{ + /* ߳ȫֱsize64ֽ, cacheͻ, Ӱ */ + if((sizeof(struct sapp_global_mthread_t) % 64) != 0){ + printf("\033[1;31;40msizeof(struct sapp_global_mthread_t)=%u, is not multiple of 64B!\033[0m\n", sizeof(struct sapp_global_mthread_t)); + printf("\033[1;31;40mYou must add %d bytes padding to sapp_global_mthread_t!\033[0m\n", mem_alignment_pad(sizeof(struct sapp_global_mthread_t))); + abort(); + } + + if((sizeof(sapp_gval_mthread_t) % 64) != 0){ + printf("\033[1;31;40msizeof(sapp_gval_mthread_t)=%u, is not multiple of 64B!\033[0m\n", sizeof(sapp_gval_mthread_t)); + printf("\033[1;31;40mYou must add %d bytes padding to sapp_gval_mthread_t!\033[0m\n", mem_alignment_pad(sizeof(sapp_gval_mthread_t))); + abort(); + } + + if((sizeof(sapp_gval_individual_volatile_t) % 64) != 0){ + printf("\033[1;31;40msizeof(sapp_gval_individual_volatile_t)=%u, is not multiple of 64B!\033[0m\n", sizeof(sapp_gval_individual_volatile_t)); + printf("\033[1;31;40mYou must add %d bytes padding to sapp_gval_individual_volatile_t!\033[0m\n", mem_alignment_pad(sizeof(sapp_gval_individual_volatile_t))); + abort(); + } + + if((sizeof(layer_args_t) % 64) != 0){ + printf("\033[1;31;40msizeof(struct layer_args_t)=%u, is not multiple of 64B!\033[0m\n", sizeof(layer_args_t)); + abort(); + } + + sapp_include_header_check(); + if(INDEPENDENT_SEND_QUEUE_MAX_NUM > MAX_THREAD_NUM){ + printf("\033[1;31;40m INDEPENDENT_SEND_QUEUE_MAX_NUM(%d) > MAX_THREAD_NUM(%d)!\033[0m\n", INDEPENDENT_SEND_QUEUE_MAX_NUM, MAX_THREAD_NUM); + abort(); + } + +} + + +#ifdef __cplusplus +} +#endif + diff --git a/src/sapp_dev/sapp_init.c b/src/sapp_dev/sapp_init.c new file mode 100644 index 0000000..92acf2f --- /dev/null +++ b/src/sapp_dev/sapp_init.c @@ -0,0 +1,367 @@ +#include "sapp_api.h" +#include "sapp_private_api.h" + +/* Systemd */ +#include + +#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 +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; +//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_skip_ethernet_layer_sw; +//extern int g_kill_tcp_remedy_sw; +extern int g_encapsulate_with_ddp; +extern int g_encapsulate_with_L2E; /* ʹDDPЭװx27ԭʼIP */ + +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 char udp_teredo_identify_entry(const struct streaminfo *pstream, const void *this_hdr, const void *raw_pkt, void **pme); +extern char udp_gtp_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 int sapp_global_val_init(void); +extern int sapp_assistant_init(void); +extern char gdev_keepalive_ip_entry(const struct streaminfo *pstream,unsigned char routedir,int thread_seq, const struct mesa_ip4_hdr *ipv4_hdr); +extern char gdev_keepalive_udp_entry(const struct streaminfo *a_udp, void **pme, int thread_seq, const void *ip_hdr); + +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 +} + +static void sapp_platform_finish_up(void) +{ + /* + ½ļ: ./sapp_status/ + + עɹproject tag б, project_list.status, + name, type + + + вļϢ, dzɹDzɹ, Ҫÿcat | grep runtime.log + plug_load.status, plug_name, plug_path, state(succ,fail) + + + sapp״̬ǨƱ仯ʱ¼, sapp_state.staus: + start_time state + 01:00:00 just_start + 01:00:01 platform_init + 01:01:01 plugctrl_init + 02:01:01 processing + */ + return; +} + +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 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; + + sapp_set_current_state(SAPP_STATE_PLATFORM_INITING); + + ABBR_CURRENT_TIME = time(NULL); + ABBR_SAPP_START_TIME = time(NULL); + + /* 2016-09-22 lijia add, dictatorڵһαʱгʼ, ֹ߳ͻ, ƽ̨ȵһ, Ҫȡļ */ + //MESA_load_profile_int_def("conf/main.conf","Module", "dictator_switch", &G_DICTATOR_SW, 1); + sapp_dictator_init(); + + + ipv4_frag_init(g_iThreadNum, 1024 * 512); + ipv6_frag_init(g_iThreadNum, 1024 * 256); + tcp_set_stream_num(max_tcp_stream_num*3,max_tcp_stream_num,max_tcp_stream_num*2); + 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", "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", "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); + } + + MESA_load_profile_int_def("conf/main.conf","Module", "encapsulate_with_L2E", &g_encapsulate_with_L2E, 0); + if(g_encapsulate_with_L2E != 0){ + printf("\033[32m[Notice] '%s' is enable!\033[0m\n", "encapsulate_with_L2E"); + sleep(1); + } + + if((g_encapsulate_with_ddp != 0) && (g_encapsulate_with_L2E != 0)){ + printf("\033[1;31;40m[Error]g_encapsulate_with_ddp is conflict with g_encapsulate_with_L2E, can't enable simultaneously at all!\033[0m\n"); + return -1; + } + + if(packet_io_lib_load(ABBR_INTERFACE_TYPE) < 0){ + return -1; + } + + packet_io_set_work_thread_num(g_packet_io_thread_num); + + packet_io_set_cap_mode(ABBR_INTERFACE_TYPE); + + /* + (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(ABBR_INTERFACE_TYPE){ + 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: + case CAP_MODEL_TUN: + 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", ABBR_INTERFACE_TYPE); + exit(1); + } + + + packet_io_set_capdev_parallel(sapp_global_val->config.packet_io.internal.interface.name); + + if(DEPOLYMENT_MODE_TRANSPARENT == g_topology_mode_raw){ + packet_io_set_capdev_serial(sapp_global_val->config.packet_io.internal.interface.name, + sapp_global_val->config.packet_io.external.interface.name); + } + + packet_io_set_topology_mode(sapp_global_val->config.packet_io.depolyment_mode_bin); + + if(sapp_global_val->config.packet_io.input_bpf_filter != NULL){ + printf("\033[33m[Warning] pcap filter is '%s'.\033[0m\n", sapp_global_val->config.packet_io.input_bpf_filter); + packet_io_set_capture_filter(sapp_global_val->config.packet_io.input_bpf_filter); + } + + packet_io_set_cap_buf_queue(10000); + + 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); + + init_stream_manage(g_iThreadNum); + timestamp_record_init(); + + /* timer must before packet_io and plug_proc() */ + __sapp_timer_platform_init(); + + +#if 1 + 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); + //MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"Module", (char *)"reverse_ethernet_addr", &g_reverse_ethernet_addr_sw, 0); + + + /* ƽ̨ù̶IJ */ + stream_register_udp_raw(udp_teredo_identify_entry); + stream_register_udp_raw(udp_gtp_identify_entry); + + /* 2020-12-23 lijia add, vxlan */ + if(DEPOLYMENT_MODE_INLINE == sapp_global_val->config.packet_io.depolyment_mode_bin){ + stream_register_overlay_ip(gdev_keepalive_ip_entry); + stream_register_overlay_udp(gdev_keepalive_udp_entry); + + if(gdev_keepalive_plug_init() < 0){ + printf("\033[1;31;40m[Error]gdev_keepalive_plug_init() error!\033[0m\n"); + return -1; + } + } + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"Module", (char *)"symbol_conflict_check", &int_tmp,0); + if(int_tmp != 0){ + /* NOTE: һҪplugctrl_proc(), в֮, ĬϿ */ + symbol_check(); /* 2016-01-13 Lijia add, ȫַͻ */ + } + sapp_global_val_sanity_check(); + + 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); + } + + sapp_assistant_init(); + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"Module", (char *)"send_tcp_offload", &sapp_global_single.cfg_send_tcp_offload_sw, 0); + + sapp_set_current_state(SAPP_STATE_PLATFORM_INITED); + + /* 2017-02-24 lijia modify, + NOTE: + ȳʼ, ڲܻpthread_create̬߳, + , DPDKģʽ, dpdkʼ׶ξͻ󶨵ǰ̵߳ijCPU, + ôpthread_create߳Ĭϼ̳Ϊ, + , ȳʼ߳ٳʼpacket_io. + */ + /* + 2019-04-11, marsioѾ, ٻָȵpacket_io_init(), plugctrl(). + */ + + /* + 2020-09-15, packet_io_initȵ, Ѿհ, + Dzܻûʼ, sappʼհ, , л˻ᵼ¶, + ijȵplugctrl(), packet_io_init(). + */ + + + sapp_set_current_state(SAPP_STATE_PKT_IO_INITING); + + if(packet_io_init(argc, argv) < 0){ + return -1; + } + + sapp_dup_pkt_init(); + + sapp_set_current_state(SAPP_STATE_PLUG_INITING); + plugctrl_proc(); + + sapp_platform_finish_up(); /* ֶ֧̬project tagע, вʼɺ, һЩƽ̨ʼ׶εβ */ + sapp_set_current_state(SAPP_STATE_PLUG_INITED); + + sapp_set_current_state(SAPP_STATE_PKT_IO_INITED); + + return 0; +} + +/* 鱾ǷͨSYSTEMD */ +static int check_is_started_by_notify() +{ + char *notify_socket = getenv("NOTIFY_SOCKET"); + return notify_socket == NULL ? 0 : 1; +} + +void MESA_platform_run(void) +{ + /* + ʹatexit()עĺ, עắ෴, ԾӺatexit()עλ, + гʼ֮, packet_io_run()֮ǰ + */ + atexit(forbid_call_exit_in_running_state); + + __sapp_timer_platform_run(); + + packet_io_run(); + + sapp_set_current_state(SAPP_STATE_PROCESSING); + /* If TFE is run by systemd's notify, then tell the systemd our tfe is ready. + * and disable the stderr log, only print logs into files */ + if (check_is_started_by_notify()) + { + sd_notify(0, "READY=1"); + sleep(1); + } + +} + +#ifdef __cplusplus +} +#endif + + diff --git a/src/sapp_dev/sapp_plug.c b/src/sapp_dev/sapp_plug.c new file mode 100644 index 0000000..bf697e6 --- /dev/null +++ b/src/sapp_dev/sapp_plug.c @@ -0,0 +1,123 @@ +#include "sapp_api.h" +#include "sapp_private_api.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +static const unsigned char MESA_art_log[] = +{ + 0x50, 0x72, 0x6F, 0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x62, 0x79, 0x0D, 0x0A, 0x20, 0x20, 0x20, + 0x20, 0x5F, 0x5F, 0x20, 0x20, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, + 0x5F, 0x5F, 0x5F, 0x20, 0x5F, 0x5F, 0x5F, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x2F, 0x20, 0x20, 0x7C, + 0x2F, 0x20, 0x20, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x5F, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x2F, 0x2F, + 0x20, 0x20, 0x20, 0x7C, 0x0D, 0x0A, 0x20, 0x20, 0x2F, 0x20, 0x2F, 0x7C, 0x5F, 0x2F, 0x20, 0x2F, + 0x20, 0x5F, 0x5F, 0x2F, 0x20, 0x20, 0x5C, 0x5F, 0x5F, 0x20, 0x5C, 0x2F, 0x20, 0x2F, 0x7C, 0x20, + 0x7C, 0x0D, 0x0A, 0x20, 0x2F, 0x20, 0x2F, 0x20, 0x20, 0x2F, 0x20, 0x2F, 0x20, 0x2F, 0x5F, 0x5F, + 0x5F, 0x20, 0x5F, 0x5F, 0x5F, 0x2F, 0x20, 0x2F, 0x20, 0x5F, 0x5F, 0x5F, 0x20, 0x7C, 0x0D, 0x0A, + 0x2F, 0x5F, 0x2F, 0x20, 0x20, 0x2F, 0x5F, 0x2F, 0x5F, 0x5F, 0x5F, 0x5F, 0x5F, 0x2F ,0x2F, 0x5F, + 0x5F, 0x5F, 0x5F, 0x2F, 0x5F, 0x2F, 0x20, 0x20, 0x7C, 0x5F, 0x7C, 0x0D, 0x0A, 0x00/* EOF */ +}; + +int MESA_platform_init(int argc, char *argv[]); +void MESA_platform_run(void); +extern int sapp_args_v; +int dpdk_init(int argc, char **argv); + + +static void signal_user_handler(int signo) +{ + //time_t last_time = time(NULL); + //while(time(NULL) < last_time + 10); /* wait 10 second, for DPDK IO module detect this process is not running */ + signal(signo, SIG_DFL); + kill(getpid(), signo); +} + +static void signal_hup_handler(int signo) +{ + printf("SIGHUP recviced!\n"); + MESA_handle_runtime_log_reconstruction(NULL); +} + +static void signal_take_over(void) +{ + sapp_config_t *pconfig = &sapp_global_val->config; + signal(SIGHUP, signal_hup_handler); + if (0 == sapp_global_single.signal_take_over_sw && 0 == pconfig->tools.signal_handler.signal) + { + return; + } + + signal(SIGUSR1, signal_user_handler); + signal(SIGUSR2, signal_user_handler); + //signal(pconfig->tools.signal_handler.signal, signal_handler); +} + + +static void show_mesa_log(void) +{ + int i; + + for(i = 0; (i < sizeof(MESA_art_log)) && (MESA_art_log[i] != 0); i ++){ + //putchar(MESA_log[i]); + printf("%c", MESA_art_log[i]); + } + printf("\n"); + sapp_runtime_log(30, "\n\n%s", MESA_art_log); +} + + +int libsapp_setup_env(int argc, char *argv[]) +{ + int ret; + + sapp_gval_init(); + sapp_set_current_state(SAPP_STATE_JUST_START); + sapp_set_current_state(SAPP_STATE_CONFIG_PARSE); + + if(argc >= 2){ + /* в, Ƚcla, Ϊʹ-h, -vȰϢ */ + if(sapp_parse_cmd_args(argc, argv) < 0){ + return -1; + } + + ret = sapp_parse_config(); + }else{ + ret = sapp_parse_config(); + } + if(ret < 0){ + return -1; + } + + sapp_cla_override_cfg_file(); + + show_mesa_log(); + +#if SAPP_INSECTICIDE + printf("\033[33m[Warning]This sapp is a temp version for solve confounded bug!\033[0m\n"); +#endif + +#if COMPAT_PAPP_FOR_BENCHMARK + printf("\033[33m[Warning]This sapp is a emasculate version for compare with papp, in other word, it's papp!\033[0m\n"); + sleep(1); +#endif + + sapp_init_breakpad_mini(); + signal_take_over(); + + if(MESA_platform_init(argc, argv) < 0){ + return -1; + } + + + MESA_platform_run(); + + return 0; +} + + +#ifdef __cplusplus +} +#endif + -- cgit v1.2.3