From c85feeb44eef858cf53b8bc97082db25c0dcb3f7 Mon Sep 17 00:00:00 2001 From: lijia Date: Fri, 23 Aug 2019 18:21:39 +0800 Subject: 增加新发包接口sapp_inject_pkt; 改polling_entry优先级; tcpdump_mesa配置文件修改 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/etc/sapp.toml | 107 ++- cmake/Version.cmake | 2 +- include/private/sapp_global_val.h | 13 +- include/private/sapp_pkt_stat.h | 3 + include/public/sapp_api.h | 2 +- include/public/stream.h | 2 + include/public/stream_inc/sapp_inject.h | 37 + include/public/stream_inc/stream_entry.h | 15 +- include/public/stream_inc/stream_inject.h | 4 +- include/public/stream_inc/stream_rawpkt.h | 4 +- src/common/CMakeLists.txt | 5 +- src/config/CMakeLists.txt | 5 +- src/config/config_parse.cpp | 36 +- src/dealpkt/CMakeLists.txt | 5 +- src/dealpkt/callapp.c | 14 +- src/dealpkt/plug_support.c | 12 + src/dealpkt/stream_manage.c | 16 +- src/entry/CMakeLists.txt | 5 +- src/entry/Makefile | 2 +- src/entry/sapp_init.c | 2 +- src/inner_plug/CMakeLists.txt | 7 +- src/inner_plug/sapp_assistant.cpp | 15 +- src/packet_io/CMakeLists.txt | 10 +- src/packet_io/Makefile | 2 +- src/packet_io/cycle_pkt_dump_through_write.c | 956 --------------------- src/packet_io/cycle_pkt_dump_through_write.c.bak | 956 +++++++++++++++++++++ .../cycle_pkt_dump_through_write_offset.c | 111 ++- src/packet_io/packet_io.c | 3 +- src/packet_io/packet_io_marsio.c | 30 +- src/packet_io/packet_io_pcap.c | 47 +- src/packet_io/packet_io_status.cpp | 38 +- src/packet_io/sapp_inject.c | 55 ++ src/packet_io/sendpacket.c | 2 +- src/plugin/CMakeLists.txt | 5 +- src/project/CMakeLists.txt | 5 +- src/timer/CMakeLists.txt | 5 +- src/timer/sapp_timer.c | 4 +- test/CMakeLists.txt | 5 +- test/test_app_sapp.c | 63 +- 39 files changed, 1411 insertions(+), 1199 deletions(-) create mode 100644 include/public/stream_inc/sapp_inject.h delete mode 100644 src/packet_io/cycle_pkt_dump_through_write.c create mode 100644 src/packet_io/cycle_pkt_dump_through_write.c.bak create mode 100644 src/packet_io/sapp_inject.c diff --git a/bin/etc/sapp.toml b/bin/etc/sapp.toml index 45e4fbe..a088f7a 100644 --- a/bin/etc/sapp.toml +++ b/bin/etc/sapp.toml @@ -1,7 +1,7 @@ ################################################################################################### # NOTE: # The format of this file is toml (https://github.com/cktan/tomlc99) -# to make vim editor display colorful easier to read, +# to make vim editor display colorful and human readable, # you can create a symbolic links named sapp.ini to sapp.toml, ln -sf sapp.toml sapp.ini ################################################################################################### @@ -14,118 +14,107 @@ worker_threads=1 bind_mask=[1,2,3,4] [PACKET_IO] -### note, packet filter, if you do not want to set any filter rule, keep it empty as "" - BSD_packet_filter="" +### note, BSD_packet_filter, if you do not want to set any filter rule, keep it empty as "" +BSD_packet_filter="" ### note, depolyment.mode options: [mirror, inline, transparent] - [packet_io.depolyment] + [packet_io.depolyment] mode=mirror ### note, interface.type options: [pag,pcap,marsio] - [packet_io.internal.interface] + [packet_io.internal.interface] type=pcap - name=lo + name="lo" - [packet_io.external.interface] + [packet_io.external.interface] type=pcap name=lo + + [packet_io.polling] +### note, polling_priority = call sapp_recv_pkt every call polling_entry times, + polling_priority=1 [STREAM] - [stream.tcp] + [stream.tcp] max=10000 timeout=60 syn_mandatory=0 reorder_pkt_max=5 analyse_option_enabled=1 - [stream.tcp.inject] - link_mss=1460 + [stream.tcp.inject] + link_mss=1460 - [stream.tcp.inject.rst] - number=3 - signature_enabled=1 + [stream.tcp.inject.rst] + number=3 + signature_enabled=1 signature_seed1=65535 signature_seed2=13 - [stream.udp] + [stream.udp] max=10000 timeout=60 - [PROFILING] - [profiling.pkt_latency] + [profiling.pkt_latency] enabled=0 ### note, threshold unit is microseconds (us) threshold=1000000 - [profiling.sanity_check] + [profiling.sanity_check] raw_pkt_broken_enabled=0 symbol_conflict_enabled=0 - [profiling.log] + [profiling.log] level=20 interval=5 - [profiling.log.local] - enabled=1 + [profiling.log.local] + enabled=1 ### note, if "file_truncate_open_enabled=1", file will be truncated, otherwise open the file for appending. - file_truncate_enabled = 1 - log_file_name = "fs2_sysinfo.log" + file_truncate_enabled = 1 + log_file_name = "fs2_sysinfo.log" - [profiling.log.remote] - enabled=1 - server_ip="192.168.11.90" - server_port=8126 -### note, remote_send_out_type option value: [line_protocol, field_stat2] - remote_send_out_type=field_stat2 + [profiling.log.remote] + enabled=1 + server_ip="192.168.11.90" + server_port=8126 - [profiling.log.remote.field_stat2] + [profiling.log.remote.field_stat2] ### note, is valid when "remote_send_out_type=field_stat2" ### note, metric_type option value: [default, json] - metric_type = default - app_name=sapp - - [profiling.log.remote.line_protocol] -### note, is valid when "remote_send_out_type=line_protocol" - [[profiling.log.remote.line_protocol.tags]] - tag_name = "tag1" - tag_value = "value1" - [[profiling.log.remote.line_protocol.tags]] - tag_name = "tag2" - tag_value = "value2" - [[profiling.log.remote.line_protocol.tags]] - tag_name = "tag3" - tag_value = "value3" - + metric_type = default + app_name=sapp + [TOOLS] - [tools.pkt_dump] + [tools.pkt_dump] enabled=1 ### note, mode options value:[storage, udp_socket] mode=udp_socket - BSD_packet_filter="ip" + BSD_packet_filter="" - [tools.pkt_dump.threads] + [tools.pkt_dump.threads] ### note, if you want enable pkt dump in all thread, set dump_thread_all_enabled=1, then 'dump_thread_id' is obsoleted. ### if dump_thread_all_enabled=0, then use dump_thread_id to specify separate specified thread index. - all_threads_enabled=1 + all_threads_enabled=1 ### note, dump_thread_id start from 0, max is CPU.worker_threads-1 - dump_thread_id=[0,1,2,3,4] + dump_thread_id=[0,1,2,3,4] - [tools.pkt_dump.udp] - command_port=12345 + [tools.pkt_dump.udp] + command_port=12345 - [tools.pkt_dump.storage] + [tools.pkt_dump.storage] ### note, file path must be double quotation mark extension, for example, path="/dev/shm/pkt_dump" - path="/dev/shm/pkt_dump" + path="/dev/shm/pkt_dump" ### note, file size unit: MB - file_size_max_per_thread=10000 + file_size_max_per_thread=10000 ### note: ### These configurations format is complex and difficult to describe with toml grammar, ### so, create a Independent config file to description specific information. [SPECIAL_CONFIG_LINK] - project_list_path="./etc/project_list.conf" - plugin_path="./etc/plugin.conf" - entrylist_path="./etc/entrylist.conf" - send_raw_pkt_path="./etc/send_raw_pkt.conf" - vxlan_sport_service_map_path="./etc/vxlan_sport_service_map.conf" + project_list_path="./etc/project_list.conf" + plugin_path="./etc/plugin.conf" + entrylist_path="./etc/entrylist.conf" + send_raw_pkt_path="./etc/send_raw_pkt.conf" + vxlan_sport_service_map_path="./etc/vxlan_sport_service_map.conf" diff --git a/cmake/Version.cmake b/cmake/Version.cmake index 77c84f4..7bc8543 100644 --- a/cmake/Version.cmake +++ b/cmake/Version.cmake @@ -21,7 +21,7 @@ string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VCS_TAG}") string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VCS_TAG}") if(NOT VERSION_MAJOR) - set(VERSION_MAJOR 3) + set(VERSION_MAJOR 4) endif() if(NOT VERSION_MINOR) diff --git a/include/private/sapp_global_val.h b/include/private/sapp_global_val.h index 8c2f796..19b4511 100644 --- a/include/private/sapp_global_val.h +++ b/include/private/sapp_global_val.h @@ -22,13 +22,14 @@ typedef struct{ char mode_str[NAME_MAX]; /* storage/udp_socket */ int mode_bin; unsigned int command_port; /* integer, host order, user should truncate to short */ - unsigned int storge_size_max; + unsigned int file_size_max_per_thread; /* MB */ char *bpf_filter; char *storge_path; int dump_all_thread_enabled; int dump_thread_id_array_num; int dump_thread_id_array[SAPP_MAX_THREADS]; /* ûļԭʼ, ʽ: [0,1,2,3], ʾ0,1,2,3߳ò */ unsigned char dump_thread_id_mask[SAPP_MAX_THREADS]; /* ԭʼ, ߳idΪ±, ֵΪ0ʾǰ߳̿, ֵ:{1,1,1,1} */ + int pkt_dump_ratio; /* capture one packet every 'pkt_dump_ratio' packets */ }sapp_config_tools_pktdump_t; typedef struct{ @@ -106,11 +107,8 @@ typedef struct{ char log_app_name[NAME_MAX]; char server_ip[INET_ADDRSTRLEN]; /* string */ unsigned int server_port; /* integer(4 byte), host order, user should truncate to short before use it */ - char remote_send_out_type[NAME_MAX]; /* field_stat2 or line_protocol */ - union{ - sapp_config_profiling_log_remote_field_stat2_t field_stat2; - sapp_config_profiling_log_remote_line_protocol_t line_protocol; - }; + char remote_send_out_type[NAME_MAX]; /* field_stat2 */ + sapp_config_profiling_log_remote_field_stat2_t field_stat2; }sapp_config_profiling_log_remote_t; @@ -156,6 +154,7 @@ typedef struct{ sapp_config_packet_io_dev_t internal; sapp_config_packet_io_dev_t external; char pcap_dumpfile_name[NAME_MAX]; + int polling_priority; }sapp_config_packet_io_t; typedef struct{ @@ -206,7 +205,7 @@ typedef struct{ typedef struct{ sapp_gval_mthread_sys_stat_t sys_stat; - char __padding[16]; + char __padding[48]; }sapp_gval_mthread_t; /* for multi-thread, must 64byte alignment */ diff --git a/include/private/sapp_pkt_stat.h b/include/private/sapp_pkt_stat.h index b9d51a2..bdc0a95 100644 --- a/include/private/sapp_pkt_stat.h +++ b/include/private/sapp_pkt_stat.h @@ -34,6 +34,9 @@ enum __sapp_sys_stat_type{ SAPP_STAT_DROP_ETHERNET, SAPP_STAT_DROP_IPV4, SAPP_STAT_DROP_IPV6, + + SAPP_POLLING_WORK, + SAPP_POLLING_IDLE, /************* tunnel stat *******************/ SAPP_STAT_TUNNEL_4OVER6, diff --git a/include/public/sapp_api.h b/include/public/sapp_api.h index 70c06a2..106615f 100644 --- a/include/public/sapp_api.h +++ b/include/public/sapp_api.h @@ -47,7 +47,7 @@ #include "MESA_ring_queue.h" #include "wiredLB.h" #include "wired_cfg.h" -//#include "field_stat2.h" //must compile with g++, so move it. +//#include "field_stat2.h" //must compile with g++, add it in source file. /* sapp platform header */ #include "sapp_std.h" diff --git a/include/public/stream.h b/include/public/stream.h index f70f4c7..6418b0e 100644 --- a/include/public/stream.h +++ b/include/public/stream.h @@ -9,6 +9,8 @@ #include "stream_inc/stream_entry.h" #include "stream_inc/stream_rawpkt.h" #include "stream_inc/stream_tunnel.h" +#include "stream_inc/sapp_inject.h" + #define STREAM_H_VERSION (20170911) diff --git a/include/public/stream_inc/sapp_inject.h b/include/public/stream_inc/sapp_inject.h new file mode 100644 index 0000000..cbcaab1 --- /dev/null +++ b/include/public/stream_inc/sapp_inject.h @@ -0,0 +1,37 @@ +#ifndef _SAPP_INJECT_H_ +#define _SAPP_INJECT_H_ 1 + +#ifdef __cplusplus +extern "C" { +#endif + +#include "stream_base.h" + +enum sapp_inject_opt{ + SIO_DEFAULT = (1<<0), /* ֻ봿, ƽ̨Э淶쵱ǰĴIPͷ, , Ƕײͷ */ + SIO_EXCLUDE_THIS_LAYER_HDR = (1<<1), /* , ǰĴIPͷҲɵ߹, ƽ̨ǰijزͷ, , Ƕײͷ */ +}; + +/* + ARGS: + stream: ǰṹָ; + payload: Ҫ͵ĸָ; + payload_len: Ҫ͵ĸس; + snd_routedir: Ҫݵroute, + ͵İ뵱ǰͬ, snd_routedir = stream->routedir, + ͵İ뵱ǰ, snd_routedir = MESA_dir_reverse(stream->routedir). + + return value: + <=0 : error. + > 0 : ͵ݰʵܳ(payload_len + ײͷ); +*/ +int sapp_inject_pkt(struct streaminfo *stream, enum sapp_inject_opt sio, const void *payload, int payload_len, unsigned char snd_routedir); + + + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/include/public/stream_inc/stream_entry.h b/include/public/stream_inc/stream_entry.h index c145a45..09738c2 100644 --- a/include/public/stream_inc/stream_entry.h +++ b/include/public/stream_inc/stream_entry.h @@ -1,7 +1,7 @@ #ifndef _APP_STREAM_ENTRY_H_ #define _APP_STREAM_ENTRY_H_ -#define STREAM_ENTRY_H_VERSION (20160118) +#define STREAM_ENTRY_H_VERSION (20190818) /* CHN : ҵýʱsession_state״̬; @@ -64,6 +64,19 @@ char UDP_ENTRY_EXAMPLE(const struct streaminfo *a_udp, void **pme, int thread_s char SAPP_PKT_EXAMPLE(const struct streaminfo *pstream, const void *this_hdr, const void *raw_pkt); char SAPP_STREAM_EXAMPLE(const struct streaminfo *pstream, const void *this_hdr, const void *raw_pkt, void **pme); +#define POLLING_STATE_WORK 0x80 +#define POLLING_STATE_IDLE 0x40 + +/* + ÿһʱ, ƽ̨õǰӿ, Ƿݰ. + stream, pme, a_packet̶NULL, thread_seqհ̵߳. + + ֵ: + POLLING_STATE_WORK: ˴λص; + POLLING_STATE_IDLE: ˴λصʲôû, û, ģʽհ, ʵûյ; +*/ +char POLLING_ENTRY(struct streaminfo *stream, void **pme, int thread_seq,void *a_packet); + /* CHN : ҵصӿ ; diff --git a/include/public/stream_inc/stream_inject.h b/include/public/stream_inc/stream_inject.h index f5f55c5..ecc9dae 100644 --- a/include/public/stream_inc/stream_inject.h +++ b/include/public/stream_inc/stream_inject.h @@ -163,7 +163,7 @@ int sapp_forward_current_pkt(const struct streaminfo *stream, unsigned int targe enum sapp_send_pkt_opt_type{ SAPP_SEND_OPT_IP_ID = 0x10, - SAPP_SEND_OPT_IP_TTL = 0x11, + SAPP_SEND_OPT_IP_TTL = 0x11, SAPP_SEND_OPT_TCP_WIN = 0x20, @@ -184,7 +184,7 @@ enum sapp_send_pkt_opt_type{ SAPP_SEND_OPT_INNER_DMAC = 0x1303, /* ڲĿMAC */ SAPP_SEND_OPT_INNER_VLANID = 0x1304, /* ڲVLAN */ SAPP_SEND_OPT_VIRTUAL_LINK_ID = 0x1305, /* ·ţ ͬʱҪTUNNAT_CZ_ACTION_ENCAP_VIRTUAL_LINK_ID*/ - SAPP_SEND_OPT_REHASH_INDEX = 0x1306, /*rehash index ͬʱҪTUNNAT_CZ_ACTION_ENCAP_VIRTUAL_LINK_ID*/ + SAPP_SEND_OPT_REHASH_INDEX = 0x1306, /*rehash index ͬʱҪTUNNAT_CZ_ACTION_ENCAP_VIRTUAL_LINK_ID*/ }; int MESA_fakepacket_send_ipv4_options(const struct streaminfo *stream, uint8_t protocol, diff --git a/include/public/stream_inc/stream_rawpkt.h b/include/public/stream_inc/stream_rawpkt.h index 39b75d6..99aab94 100644 --- a/include/public/stream_inc/stream_rawpkt.h +++ b/include/public/stream_inc/stream_rawpkt.h @@ -1,7 +1,7 @@ #ifndef _APP_STREAM_RAWPKT_H_ #define _APP_STREAM_RAWPKT_H_ -#define STREAM_RAWPKT_H_VERSION (20160919) +#define STREAM_RAWPKT_H_VERSION (20190817) #include "stream_base.h" @@ -22,6 +22,8 @@ enum{ RAW_PKT_GET_VIRTUAL_LINK_ID, //value type is uint64 *, out_value should be uint64 * RAW_PKT_GET_REHASH_INDEX, // value type is uint64 *, out_value should be uint64 * RAW_PKT_GET_VXLAN_VPNID, // network-order, VPN_ID, value type is int, out_value should be int * + + RAW_PKT_GET_VXLAN_LOCAL_IP, // network-order, VXLAN Local IP, value type is int, out_value should be int * }; #ifdef __cplusplus diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 7fa4e23..a43d894 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) 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) + if(OPT_IOMODE_MARSIO) include_directories(${MARSIO_SDK_PREFIX}/include) diff --git a/src/config/CMakeLists.txt b/src/config/CMakeLists.txt index 52e9fb8..fb87611 100644 --- a/src/config/CMakeLists.txt +++ b/src/config/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) 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) + if(OPT_IOMODE_MARSIO) include_directories(${MARSIO_SDK_PREFIX}/include) diff --git a/src/config/config_parse.cpp b/src/config/config_parse.cpp index 0bdfc45..ee5ae0f 100644 --- a/src/config/config_parse.cpp +++ b/src/config/config_parse.cpp @@ -8,7 +8,7 @@ extern "C" { #include "field_stat2.h" -/* ֻ̬ȫֱԷκδ, sapp_global_val */ +/* ֻ̬ȫֱԷڴ, sapp_global_val */ static const char *default_config_file = "./etc/sapp.toml"; static const unsigned char sample_sapp_toml[] = @@ -473,6 +473,13 @@ static int config_sanity_check(void) return -1; } + if(strncasecmp(pconfig->tools.pkt_dump.mode_str, "udp_socket" ,strlen("udp_socket")) == 0){ + if(pconfig->tools.pkt_dump.command_port <=0 || pconfig->tools.pkt_dump.command_port > 65535){ + sapp_log(30, ~0, ~0, "[Error] TOOLS.PKT_DUMP.command_port: %u invalid!", pconfig->tools.pkt_dump.command_port); + return -1; + } + } + old_config_file_detect("./conf/main.conf", "./etc/sapp.toml"); return 0; @@ -523,6 +530,11 @@ int sapp_parse_config(void) /******************************* SYSTEM *********************************/ ret = tomlc99_wrap_load_string_def(default_config_file, (char *)"SYSTEM", (char *)"instance_name", pconfig->system.instance_name, NAME_MAX, "sapp_v4.0"); + if(TOMLC99_RET_PATTERN_ERROR == ret){ + /* ״νжPATTERN_ERROR, ˵tomlʽ, ټ */ + sapp_log(30, ~0, ~0, "parse config file %s error!", default_config_file); + exit(1); + } /******************************* CPU *********************************/ tomlc99_wrap_load_int_def(default_config_file, (char *)"CPU", (char *)"worker_threads", (int *)&pconfig->cpu.worker_threads, 1); @@ -539,6 +551,7 @@ int sapp_parse_config(void) tomlc99_wrap_load_string_def(default_config_file, (char *)"packet_io.internal.interface", (char *)"name", pconfig->packet_io.internal.interface.name, NAME_MAX, "lo"); tomlc99_wrap_load_string_def(default_config_file, (char *)"packet_io.external.interface", (char *)"type", pconfig->packet_io.external.interface.type_str, NAME_MAX, "pcap"); tomlc99_wrap_load_string_def(default_config_file, (char *)"packet_io.external.interface", (char *)"name", pconfig->packet_io.external.interface.name, NAME_MAX, "lo"); + tomlc99_wrap_load_int_def(default_config_file, (char *)"packet_io.polling", (char *)"polling_priority", &pconfig->packet_io.polling_priority, 10); /******************************* STREAM.TCP ******************************/ @@ -575,8 +588,8 @@ int sapp_parse_config(void) tomlc99_wrap_load_int_def(default_config_file, (char *)"profiling.log.remote", (char *)"enabled", (int *)&pconfig->profiling.log.remote.enabled, 0); tomlc99_wrap_load_string_def(default_config_file, (char *)"profiling.log.remote", (char *)"server_ip", pconfig->profiling.log.remote.server_ip, INET_ADDRSTRLEN, "#"); tomlc99_wrap_load_int_def(default_config_file, (char *)"profiling.log.remote", (char *)"server_port", (int *)&pconfig->profiling.log.remote.server_port, 0); - tomlc99_wrap_load_string_def(default_config_file, (char *)"profiling.log.remote", (char *)"remote_send_out_type", pconfig->profiling.log.remote.remote_send_out_type, NAME_MAX, "field_stat2"); - +#if 0 + //tomlc99_wrap_load_string_def(default_config_file, (char *)"profiling.log.remote", (char *)"remote_send_out_type", pconfig->profiling.log.remote.remote_send_out_type, NAME_MAX, "field_stat2"); /******************************* PROFILING.LOG.remote.field_stat2 **********************/ if(strncasecmp(pconfig->profiling.log.remote.remote_send_out_type, "field_stat2", strlen("field_stat2")) == 0){ tomlc99_wrap_load_string_def(default_config_file, (char *)"profiling.log.remote.field_stat2", (char *)"app_name", pconfig->profiling.log.remote.field_stat2.app_name, NAME_MAX, "sapp"); @@ -596,7 +609,19 @@ int sapp_parse_config(void) sapp_log(30, ~0, ~0, "config parse error! invalid value of 'profiling.log.remote.remote_send_out_type'"); return -1; } - +#else + strncpy(pconfig->profiling.log.remote.remote_send_out_type, "field_stat2", strlen("field_stat2")); + tomlc99_wrap_load_string_def(default_config_file, (char *)"profiling.log.remote.field_stat2", (char *)"app_name", pconfig->profiling.log.remote.field_stat2.app_name, NAME_MAX, "sapp"); + tomlc99_wrap_load_string_def(default_config_file, (char *)"profiling.log.remote.field_stat2", (char *)"metric_type", str_tmp, ARG_MAX, "default"); + if(strncasecmp(str_tmp, "default", strlen("default")) == 0){ + pconfig->profiling.log.remote.field_stat2.metrics_type = FS_METRIS_OUTPUT_DEFAULT; + }else if(strncasecmp(str_tmp, "json", strlen("json")) == 0){ + pconfig->profiling.log.remote.field_stat2.metrics_type = FS_METRIS_OUTPUT_JSON; + }else{ + sapp_log(30, ~0, ~0, "config parse error! invalid value of 'profiling.log.remote.field_stat2.metric_type'"); + return -1; + } +#endif /******************************* TOOLS **********************************/ tomlc99_wrap_load_int_def(default_config_file, (char *)"tools.pkt_dump", (char *)"enabled", (int *)&pconfig->tools.pkt_dump.enabled, 0); tomlc99_wrap_load_string_def(default_config_file, (char *)"tools.pkt_dump", (char *)"mode", pconfig->tools.pkt_dump.mode_str, NAME_MAX, "udp_socket"); @@ -612,10 +637,11 @@ int sapp_parse_config(void) } tomlc99_wrap_load_int_def(default_config_file, (char *)"tools.pkt_dump.udp", (char *)"command_port", (int *)&pconfig->tools.pkt_dump.command_port, 0); + tomlc99_wrap_load_int_def(default_config_file, (char *)"tools.pkt_dump.udp", (char *)"pkt_dump_ratio", (int *)&pconfig->tools.pkt_dump.pkt_dump_ratio, 30); tomlc99_wrap_load_string_def(default_config_file, (char *)"tools.pkt_dump.storage", (char *)"path", str_tmp, ARG_MAX, "/dev/shm/pkt_dump"); pconfig->tools.pkt_dump.storge_path = strdup(str_tmp); - tomlc99_wrap_load_int_def(default_config_file, (char *)"tools.pkt_dump.storage", (char *)"file_size_max_per_thread", (int *)&pconfig->tools.pkt_dump.storge_size_max, 1000); + tomlc99_wrap_load_int_def(default_config_file, (char *)"tools.pkt_dump.storage", (char *)"file_size_max_per_thread", (int *)&pconfig->tools.pkt_dump.file_size_max_per_thread, 1000); /*************************** SPECIAL_CONFIG_LINK *************************/ tomlc99_wrap_load_string_def(default_config_file, (char *)"SPECIAL_CONFIG_LINK", (char *)"project_list_path", str_tmp, ARG_MAX, "./etc/project_list.conf"); diff --git a/src/dealpkt/CMakeLists.txt b/src/dealpkt/CMakeLists.txt index dc42a41..8e058c1 100644 --- a/src/dealpkt/CMakeLists.txt +++ b/src/dealpkt/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) 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) + if(OPT_IOMODE_MARSIO) include_directories(${MARSIO_SDK_PREFIX}/include) diff --git a/src/dealpkt/callapp.c b/src/dealpkt/callapp.c index fdb3330..0bbb942 100644 --- a/src/dealpkt/callapp.c +++ b/src/dealpkt/callapp.c @@ -859,14 +859,24 @@ int stream_process_ipv6_frag(struct streaminfo *pstream,const struct ip6_hdr *th int stream_process_polling(int thread_seq) { int i; + char plug_ret, fun_ret = 0; __st_pkt_proc_context_check_timeout(thread_seq); for(i = 0; i < g_PollingFunNum; i++){ - g_PollingFun[i].stream_fun(NULL, NULL, thread_seq, NULL); + plug_ret = g_PollingFun[i].stream_fun(NULL, NULL, thread_seq, NULL); + if(plug_ret & POLLING_STATE_WORK){ + fun_ret = POLLING_STATE_WORK; + } } - return 0; + if(POLLING_STATE_WORK == fun_ret){ + sapp_global_val->mthread_volatile[thread_seq]->sys_stat.count[SAPP_POLLING_WORK]++; + }else{ + sapp_global_val->mthread_volatile[thread_seq]->sys_stat.count[SAPP_POLLING_IDLE]++; + } + + return fun_ret; } extern int g_use_MESA_sleep_sw; diff --git a/src/dealpkt/plug_support.c b/src/dealpkt/plug_support.c index 3f2e16a..876334b 100644 --- a/src/dealpkt/plug_support.c +++ b/src/dealpkt/plug_support.c @@ -84,6 +84,18 @@ int get_opt_from_rawpkt(const void *voidpkt, int type, void *void_value) } break; + case RAW_PKT_GET_VXLAN_LOCAL_IP: + { + const struct mr_tunnat_ctrlzone *mr_ctrlzone = (const struct mr_tunnat_ctrlzone *)marsio_buff_ctrlzone((marsio_buff_t *)rawpkt->io_lib_pkt_reference, 0); /* index */ + if(0 == (mr_ctrlzone->action & TUNNAT_CZ_ACTION_FORWARD)){/* vxlan, ϶ûЩѡ */ + ret = -1; + break; + } + int *out_value = (int *)void_value; + *out_value = mr_ctrlzone->l_device_in_addr; + } + break; + case RAW_PKT_GET_VXLAN_ID: { diff --git a/src/dealpkt/stream_manage.c b/src/dealpkt/stream_manage.c index a5c9a1e..ee37315 100644 --- a/src/dealpkt/stream_manage.c +++ b/src/dealpkt/stream_manage.c @@ -30,13 +30,13 @@ extern "C" { #endif -int sapp_version_v4_20190705; +int sapp_version_v4_20190820; /* İ汾packet_io_libУ, ֹƽ̨dl.soƥ䵼쳣, ޸BUGδ. */ #if IOMODE_PCAP -int sapp_packet_io_v = 20190524; +int sapp_packet_io_v = 20190820; #elif IOMODE_PAG int sapp_packet_io_v = 20151106; #elif IOMODE_PFRING @@ -48,7 +48,7 @@ int sapp_packet_io_v = 20160426; #elif IOMODE_TOPSEC int sapp_packet_io_v = 20151014; #elif IOMODE_MARSIO -int sapp_packet_io_v = 20190527; +int sapp_packet_io_v = 20190820; #elif IOMODE_SMITH int sapp_packet_io_v = 20161027; #elif IOMODE_DPDK_VXLAN @@ -145,15 +145,15 @@ static int init_stream_detail(struct global_stream *g_stream) 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[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.cnt=g_stream->freeList.max_cnt; g_stream->freeList.tail= &(g_stream->freeList.head[g_stream->freeList.max_cnt-1]); return 0; diff --git a/src/entry/CMakeLists.txt b/src/entry/CMakeLists.txt index a6dcf1e..69ce625 100644 --- a/src/entry/CMakeLists.txt +++ b/src/entry/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) 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) + LINK_DIRECTORIES(/opt/MESA/lib) LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/lib) diff --git a/src/entry/Makefile b/src/entry/Makefile index 50e548f..46f0bab 100644 --- a/src/entry/Makefile +++ b/src/entry/Makefile @@ -55,7 +55,6 @@ endif WHOLE_ARCH += ../lib/libtomlc99_wrap.a WHOLE_ARCH += ../lib/libconfig.a WHOLE_ARCH += ../lib/libtimer.a -WHOLE_ARCH += ../lib/libevent.a ifeq ($(debug), $(_DEBUG2)) LIB += ../lib/libiknow.a -lpcap @@ -82,6 +81,7 @@ LIB += -lMESA_field_stat2 #LIB += -lsapp_assistant LIB += -lcjson #LIB += -lsesame_door +LIB += -l event ifeq ($(iomode), $(_MODE_MARSIO)) LIB += -lmarsio diff --git a/src/entry/sapp_init.c b/src/entry/sapp_init.c index 7f77b83..0f946a9 100644 --- a/src/entry/sapp_init.c +++ b/src/entry/sapp_init.c @@ -341,7 +341,7 @@ int MESA_platform_init(int argc, char *argv[]) __sapp_timer_platform_init(); -#if CYCLE_PKT_DUMP +#if 1 if(cycle_pkt_dump_init(argc , argv) < 0){ return -1; } diff --git a/src/inner_plug/CMakeLists.txt b/src/inner_plug/CMakeLists.txt index fcc7a26..a328364 100644 --- a/src/inner_plug/CMakeLists.txt +++ b/src/inner_plug/CMakeLists.txt @@ -1,12 +1,13 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) -include_directories(${MESA_SDK_PREFIX}/include/MESA/stream_inc) 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) +include_directories(${MESA_SDK_PREFIX}/include/MESA/stream_inc) + if(OPT_IOMODE_MARSIO) include_directories(${MARSIO_SDK_PREFIX}/include) endif() diff --git a/src/inner_plug/sapp_assistant.cpp b/src/inner_plug/sapp_assistant.cpp index 5457cd3..db52c97 100644 --- a/src/inner_plug/sapp_assistant.cpp +++ b/src/inner_plug/sapp_assistant.cpp @@ -468,16 +468,29 @@ void sapp_fs2_set_latency(int thead_seq, long long time_cost) int sapp_assistant_init(void) { sapp_fs2_init(); - sapp_line_protocol_init(); + //sapp_line_protocol_init(); sapp_identify_broad_multicast_init(); pthread_mutex_init(&g_plug_Independent_thread_mutex, NULL); return 1; } + +int sapp_inject_pkt_with_feedback(struct streaminfo *stream, + enum sapp_inject_opt sio, const void *payload, int payload_len, unsigned char snd_routedir, + char *feedback_buf, int *feedback_buf_len) +{ + + +} + + + + #ifdef __cplusplus } #endif +/************************ C++ compiler **************************************/ static int sapp_fs2_init(void) { char cfg_ip_str[32]; diff --git a/src/packet_io/CMakeLists.txt b/src/packet_io/CMakeLists.txt index 87720e6..0d84e7f 100644 --- a/src/packet_io/CMakeLists.txt +++ b/src/packet_io/CMakeLists.txt @@ -4,12 +4,13 @@ add_definitions(-DPLATFORM_NSDPF_PAPP=1) LINK_DIRECTORIES(/opt/mrzcpd/lib) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) -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) +include_directories(${CMAKE_SOURCE_DIR}/include) + if(OPT_IOMODE_MARSIO) include_directories(${MARSIO_SDK_PREFIX}/include) endif() @@ -59,7 +60,8 @@ set(PACKET_IO_STATIC_SOURCE cycle_pkt_dump_through_write_offset.c packet_io_lib iknow_info.c packet_io_log.c sendpacket.c packet_io.c packet_io_status.cpp packet_io_status_new.c -packet_io_device.c sapp_compat_wangyan_api.c) +packet_io_device.c sapp_compat_wangyan_api.c +sapp_inject.c) add_library(packet_io STATIC ${PACKET_IO_STATIC_SOURCE}) if(CAPTURE_MODE MATCHES "MARSIO") diff --git a/src/packet_io/Makefile b/src/packet_io/Makefile index bc09a15..2a6338c 100644 --- a/src/packet_io/Makefile +++ b/src/packet_io/Makefile @@ -35,7 +35,7 @@ OBJS+=packet_io_status_new.o OBJS += cycle_pkt_dump_through_write_offset.o OBJS += packet_io_device.o OBJS += sapp_compat_wangyan_api.o - +OBJS += sapp_inject.o DLL_LIB= #ifeq ($(iomode), $(_MODE_PCAP)) diff --git a/src/packet_io/cycle_pkt_dump_through_write.c b/src/packet_io/cycle_pkt_dump_through_write.c deleted file mode 100644 index 8eeafd3..0000000 --- a/src/packet_io/cycle_pkt_dump_through_write.c +++ /dev/null @@ -1,956 +0,0 @@ -#include "sapp_api.h" -#include "sapp_private_api.h" - -/* - 2015-04-15 , LiJia. - cycle_pkt_dump, - ʵʱѭ洢ݰĿǽ߻ҵֻһcore, BUG޷. - sappcoredump 'pkt_dump_file_root_dir'Ŀ¼洢һʱյİ, - öģʽ֣пܷBUGԴ - ֣pkt_dump_file_max_sizeʵһЩ. - - main.conf1µsection5: - [pkt_dump] - pkt_dump_switch=0 //ܿ - pkt_dump_total_size=1000 //root_dirܼ.pcapļĴС, λ:MB, ֹƶдӲ - pkt_dump_file_max_size=200 //pcapļÿֵ߳̿, λ:MB. - pkt_dump_file_root_dir=/dev/shm //pcap洢·, Ƽ/dev/shm, Ӳٶȿܶ - pkt_dump_thread_seq=0,2,4,6,8 //ÿ̵߳ĿأΪܾ޴޷ȫ洢Ǿֻò̵߳dump , all=ȫ - -*/ - -#ifdef __cplusplus -extern "C" { -#endif - - -#define UNIX_DOMAIN_SOCKET_NAME "/tmp/.pktdump" -#define PKT_DUMP_PROCESS_NAME "sapp_pkt_dump" - -extern iknow_handle PLATFORM_IHANDLE; - -enum dump_mode{ - PKT_DUMP_LOCAL_FILE = 1, /* дļ */ - PKT_DUMP_UDP_SOCKET = 2, /* дUDP ij˿ */ -}; - -#if DEBUG -#include "iknow.h" - -typedef struct{ - unsigned long long total_pkt_num; - unsigned long long total_pkt_bytes; - unsigned long long discard_pkt_num; - unsigned long long discard_pkt_bytes; - char __pad__[32]; /* 64ֽcache */ -}pkt_dump_status_t; -static pkt_dump_status_t PKT_DISCARD_STAT[MAX_THREAD_NUM]; - -#endif - -extern int g_use_MESA_sleep_sw; -extern int MESA_mkdir_p(const char *pathname, mode_t mode); -//extern time_t g_CurrentTime; -//extern int g_packet_io_thread_num; -//extern int g_iThreadNum; /* for dual_stack, start */ -static char G_PKT_DUMP_ROOT_DIR[PATH_MAX]; /* ָΪ/dev/shm, ٶȿ */ -static long G_PKT_DUMP_FILE_SIZE = 104857600; /* Ĭ100MB, ߳ */ -int G_PKT_DUMP_SW = 0; /* ĬϹر */ -int G_PKT_DUMP_MODE = PKT_DUMP_LOCAL_FILE; /* Ĭдļ */ -static int G_PKT_DUMP_PER_THREAD_SW[MAX_THREAD_NUM]; - -static int pkt_dump_thread_num = 1; - -static FILE *G_PKT_DUMP_FP[MAX_THREAD_NUM]; -static int G_FILENAME_INDEX[MAX_THREAD_NUM]; -static long G_PCAP_SIZE[MAX_THREAD_NUM]; -static char G_PKT_DUMP_FILTER_STR[PATH_MAX]; -static struct bpf_program G_PKT_DUMP_BPF_FILTER[MAX_THREAD_NUM]; -static pcap_t *phony_pcap_handle[MAX_THREAD_NUM]; /* BPFʱʹ */ - -static unsigned short pkt_dump_tcp_bind_port = 12345; /* ĬTCP˿, ڽⲿtcpdump */ -static int pkt_dump_tcp_cmd_sd = -1;/* TCP fd, ڽⲿtcpdump */ -static volatile int pkt_dump_udp_socket_peer_alive = 0; /* ⲿtcpdumpǷ */ -static int pkt_dump_udp_pkt_sd[MAX_THREAD_NUM]; /* UDPݰfd */ -static struct sockaddr_in udp_recv_addr; - -static unsigned char phony_mac_hdr[14] = -{ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0x12, 0x23, 0x34, 0x56, 0x67, 0x78, - 0x08, 0x00 -}; -typedef struct { - unsigned int magic; - unsigned short version_major; - unsigned short version_minor; - unsigned int thiszone; /* gmt to local correction */ - unsigned int sigfigs; /* accuracy of timestamps */ - unsigned int snaplen; /* max length saved portion of each pkt */ - unsigned int linktype; /* data link type (LINKTYPE_*) */ -}pkt_dump_file_hdr_t; - -struct pkt_dump_timeval { - unsigned int tv_sec; /* seconds */ - unsigned int tv_usec; /* microseconds */ -}; - -typedef struct { - struct pkt_dump_timeval ts; /* time stamp */ - unsigned int caplen; /* length of portion present */ - unsigned int len; /* length this packet (off wire) */ -}pkt_dump_pkt_hdr_t; - -typedef struct{ - enum addr_type_t low_layer_type; /* ԭʼײЭ, MAC(pcap), ҲIPv4(pag) */ - int raw_pkt_len; /* ԭʼܳ */ - //char pkt_data[]; -}pkt_dump_pipe_t; - - -static const pkt_dump_file_hdr_t pfile_hdr = -{ - 0xA1B2C3D4, - 0x0002, - 0x0004, - 0, - 0, - 0xFFFF, - 1 -}; - -/* reliable read */ -static inline ssize_t Rread(int fd, void *buf, size_t expect_count) -{ - ssize_t ret; - void *save_ptr = buf; - size_t left_count = expect_count; - - while(left_count > 0){ -retry: - ret = read(fd, save_ptr, left_count); - if(ret < 0){ - switch(errno){ - case EAGAIN: - if(0 == g_use_MESA_sleep_sw){ - usleep(1); - }else{ - MESA_sleep(); - } - /* no break here!!! no break here!!! no break here!!!*/ - case EINTR: - goto retry; - break; - - default: - printf("pkt dump, read error:%s, ptr=%p, expect_count=%lu, left_count=%lu\n", - strerror(errno), save_ptr, expect_count, left_count); - break; - } - }else if(0 == ret){ - return 0; /* peer close */ - }else{ - save_ptr = (char *)save_ptr + ret; - left_count -= ret; - } - } - - return expect_count - left_count; -} - -/* reliable write */ -static inline ssize_t Rwrite(int fd, const void *buf, size_t expect_count) -{ - ssize_t ret; - const void *real_ptr = buf; - size_t left_count = expect_count; - - while(left_count > 0){ -retry: - ret = write(fd, real_ptr, left_count); - if(ret < 0){ - switch(errno){ - case EINTR: - goto retry; - break; - - default: - printf("pkt dump, write error:%s\n", strerror(errno)); - /* дռ䲻, , Ӱ */ - break; - } - }else{ - real_ptr = (const char *)real_ptr + ret; - left_count -= ret; - } - } - - return expect_count - left_count; -} - -#if 0 -static void *cycle_pkt_dump_check_father(void *arg) -{ - while(1){ - if(1 == getppid()){ /* ̱Ϊinit, ˵Ѿ˳ */ - exit(1); - }else{ - usleep(100000); - } - } - - return NULL; -} - - -static void *cycle_pkt_dump_thread(void *arg) -{ - int thread_seq = *((int *)arg); - FILE *fp; - int file_name_post_prefix = 0, ret, pfile_hdr_flag = 0; - long tot_pcap_file_len = 0; - char pkt_buf[65536]; - char dump_file_name[PATH_MAX]; - pkt_dump_pkt_hdr_t *ppkt_hdr; - int pkt_len; - int skip_pkt_hdr_len; /* Ԥͷռ䳤, ݲģʽͬ, ܻEth */ - - snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", - G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, file_name_post_prefix); - fp = fopen(dump_file_name, "w+"); - if(NULL == fp){ - printf("fopen %s error!\n", dump_file_name); - goto done; - } - ppkt_hdr = (pkt_dump_pkt_hdr_t *)pkt_buf; - ppkt_hdr->ts.tv_usec = 0; -#ifdef CAPTURE_MODE_PAG - memcpy(pkt_buf+sizeof(pkt_dump_pkt_hdr_t), phony_mac_hdr, 14); - skip_pkt_hdr_len = sizeof(pkt_dump_pkt_hdr_t) + 14; -#else - skip_pkt_hdr_len = sizeof(pkt_dump_pkt_hdr_t); -#endif - while(1){ - /* pcapͷռԤ, ֻһfwrite, ͽͷһд */ - pkt_len = read(G_UNIX_DOMAIN_SD[thread_seq], (char *)pkt_buf+skip_pkt_hdr_len, 2048); - if(0 == pkt_len){ - goto done; - }else if(pkt_len < 0){ - continue; - } - - /* pcap file header, first time */ - if(0 == pfile_hdr_flag){ - fwrite(&pfile_hdr, sizeof(pkt_dump_file_hdr_t), 1, fp); - tot_pcap_file_len += sizeof(pkt_dump_file_hdr_t); - pfile_hdr_flag = 1; - } - /* °, ʱ */ - ppkt_hdr->ts.tv_sec = (unsigned int )g_CurrentTime; -#ifdef CAPTURE_MODE_PAG - ppkt_hdr->caplen = pkt_len+14; - ppkt_hdr->len = pkt_len+14; -#else - ppkt_hdr->caplen = pkt_len; - ppkt_hdr->len = pkt_len; -#endif - - fwrite(pkt_buf, pkt_len+skip_pkt_hdr_len, 1, fp); - tot_pcap_file_len += pkt_len+skip_pkt_hdr_len; - - /* , ѭдļ */ - if(tot_pcap_file_len >= G_PKT_DUMP_FILE_SIZE){ - fclose(fp); - file_name_post_prefix = file_name_post_prefix ^ 1; - snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", - G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, file_name_post_prefix); - fp = fopen(dump_file_name, "w+"); /* cycle write two files */ - if(NULL == fp){ - printf("fopen %s error!\n", dump_file_name); - goto done; - } - tot_pcap_file_len = 0; - pfile_hdr_flag = 0; - } - } - -done: - - return NULL; -} -#endif - - -static void __do_cycle_pkt_dump_udp_socket(int thread_seq, const raw_pkt_t *p_raw_pkt) -{ - if(1 == pkt_dump_udp_socket_peer_alive){ - if(g_packet_io_cap_level != CAP_LEVEL_MAC){ - if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, - (const unsigned char *)p_raw_pkt->raw_pkt_data-14, - p_raw_pkt->raw_pkt_len+14, p_raw_pkt->raw_pkt_len+14)){ - return; - } - }else{ - if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, - (const unsigned char *)p_raw_pkt->raw_pkt_data, - p_raw_pkt->raw_pkt_len, p_raw_pkt->raw_pkt_len)){ - return; - } - } - - - /* bingo, match filter, sendto */ - sendto(pkt_dump_udp_pkt_sd[thread_seq], p_raw_pkt->raw_pkt_data, p_raw_pkt->raw_pkt_len, - MSG_DONTWAIT, - (const struct sockaddr *)&udp_recv_addr, - sizeof(udp_recv_addr)); - } -} - -static void __do_cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt) -{ - int ret; - /* Ϊpcap_hdr[2],һССtrick, [0]ʾpcapͷϢ, - [1]ʾPAGģʽ, 洢etherhdr, pkt_dump_pkt_hdr_tΪ16ֽ, ÿԷһether hdr. - */ - pkt_dump_pkt_hdr_t pcap_hdr[2]; - int write_hdr_len; - char dump_file_name[PATH_MAX]; - - if(PKT_DUMP_LOCAL_FILE == G_PKT_DUMP_MODE){ - if(0 == G_PKT_DUMP_PER_THREAD_SW[thread_seq]){ - return; - } - }else{ - __do_cycle_pkt_dump_udp_socket(thread_seq, p_raw_pkt); - return; - } - - if(G_PKT_DUMP_FILTER_STR[0] != '\0'){ - if(g_packet_io_cap_level != CAP_LEVEL_MAC){ - if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, - (const unsigned char *)p_raw_pkt->raw_pkt_data-14, - p_raw_pkt->raw_pkt_len+14, p_raw_pkt->raw_pkt_len+14)){ - return; - } - }else{ - if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, - (const unsigned char *)p_raw_pkt->raw_pkt_data, - p_raw_pkt->raw_pkt_len, p_raw_pkt->raw_pkt_len)){ - return; - } - } - } - - if(unlikely(NULL == G_PKT_DUMP_FP[thread_seq])){ - snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", - G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, G_FILENAME_INDEX[thread_seq]); - G_PKT_DUMP_FP[thread_seq] = fopen(dump_file_name, "wb+"); - if(NULL == G_PKT_DUMP_FP[thread_seq]){ - printf("fopen %s error!\n", dump_file_name); - return; - } - /* pcap file header, first time */ - fwrite(&pfile_hdr, sizeof(pkt_dump_file_hdr_t), 1, G_PKT_DUMP_FP[thread_seq]); - } - - /* °, ʱ */ - pcap_hdr[0].ts.tv_sec = (unsigned int )g_CurrentTime; - - if(g_packet_io_cap_level != CAP_LEVEL_MAC){ - pcap_hdr[0].caplen = p_raw_pkt->raw_pkt_len+14; - pcap_hdr[0].len = p_raw_pkt->raw_pkt_len+14; - memcpy(&pcap_hdr[1], phony_mac_hdr, 14); - write_hdr_len = sizeof(pkt_dump_pkt_hdr_t) + 14; - }else{ - pcap_hdr[0].caplen = p_raw_pkt->raw_pkt_len; - pcap_hdr[0].len = p_raw_pkt->raw_pkt_len; - write_hdr_len = sizeof(pkt_dump_pkt_hdr_t); - } - - ret = fwrite(&pcap_hdr[0], write_hdr_len, 1, G_PKT_DUMP_FP[thread_seq]); - if(unlikely(ret < 1)){ - return; - } - ret = fwrite(p_raw_pkt->raw_pkt_data, p_raw_pkt->raw_pkt_len, 1, G_PKT_DUMP_FP[thread_seq]); - if(unlikely(ret < 1)){ - /* ֻдͷ, дݳִ, ع */ - fseek(G_PKT_DUMP_FP[thread_seq], (int)0-(int)sizeof(pkt_dump_pkt_hdr_t), SEEK_CUR); - return; - } - - /* , ѭдļ */ - G_PCAP_SIZE[thread_seq] += write_hdr_len + p_raw_pkt->raw_pkt_len; - if(unlikely(G_PCAP_SIZE[thread_seq] >= G_PKT_DUMP_FILE_SIZE)){ - fclose(G_PKT_DUMP_FP[thread_seq]); - G_FILENAME_INDEX[thread_seq] ^= 1; - snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", - G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, G_FILENAME_INDEX[thread_seq]); - G_PKT_DUMP_FP[thread_seq] = fopen(dump_file_name, "wb+"); - if(NULL == G_PKT_DUMP_FP[thread_seq]){ - printf("fopen %s error!\n", dump_file_name); - return; - } - /* pcap file header, first time */ - fwrite(&pfile_hdr, sizeof(pkt_dump_file_hdr_t), 1, G_PKT_DUMP_FP[thread_seq]); - G_PCAP_SIZE[thread_seq] = 0; - } - - return; -} - -void cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt) -{ - if(likely(0 == G_PKT_DUMP_SW)){ - return; - } - - __do_cycle_pkt_dump(thread_seq, p_raw_pkt); -} - -void cycle_pkt_dump_old(int thread_seq, const void *ippkt, int ippktlen) -{ - raw_pkt_t raw_pkt; - - raw_pkt.raw_pkt_data = ippkt; - raw_pkt.raw_pkt_len = ippktlen; - - cycle_pkt_dump(thread_seq, &raw_pkt); -} - -#if 0 -static int cycle_pkt_dump_child_init(void) -{ - int i, ret; - struct sockaddr_un father_pro_addr; - int addr_len; - char test_buf[1024]; - int buf_len = 0; - socklen_t opt_len; - - for(i = 0; i < g_packet_io_thread_num; i++){ - G_UNIX_DOMAIN_SD[i] = socket(AF_UNIX,SOCK_DGRAM,0); - if(G_UNIX_DOMAIN_SD[i] < 0){ - printf("socket error: %s\n", strerror(errno)); - return -1; - } - - addr_len = sizeof(father_pro_addr); - father_pro_addr.sun_family=AF_UNIX; - snprintf(father_pro_addr.sun_path, sizeof(father_pro_addr.sun_path), "%s%d", UNIX_DOMAIN_SOCKET_NAME, i); - - unlink(father_pro_addr.sun_path); - if(bind(G_UNIX_DOMAIN_SD[i], (struct sockaddr *)&father_pro_addr, addr_len) < 0){ - printf("bind %s error: %s\n", father_pro_addr.sun_path, strerror(errno)); - return -1; - } - - listen(G_UNIX_DOMAIN_SD[i], 10); - } - - usleep(10000); - - for(i = 0; i < g_packet_io_thread_num; i++){ - ret = read(G_UNIX_DOMAIN_SD[i], test_buf, 1024); /* ȴ̵, ȷ˵ */ - if(ret <= 0){ - printf("socket read error:%s\n", strerror(errno)); - return -1; - } - } - - return 0; -} -#endif - - -#if DEBUG -static long ifuncb_show_pktdump_summary(iknow_handle ihandle, iknow_conn iconn, const void *cb_fun_arg, const char *cmd_args) -{ - int i; - unsigned long long discard_pkt_sum = 0, discard_byte_sum = 0; - unsigned long long tot_pkt_sum = 0, tot_byte_sum = 0; - - for(i = 0; i < g_packet_io_thread_num; i++){ - tot_pkt_sum += PKT_DISCARD_STAT[i].total_pkt_num; - tot_byte_sum += PKT_DISCARD_STAT[i].total_pkt_bytes; - discard_pkt_sum += PKT_DISCARD_STAT[i].discard_pkt_num; - discard_byte_sum += PKT_DISCARD_STAT[i].discard_pkt_bytes; - } - //iprintf(ihandle, iconn,"NOTE: unit of byte for total is 'byte', for realtime is 'bps, bit per second'!\n"); - iprintf(ihandle, iconn, "%8s %8s %13s\n", "", "packet", "byte"); - iprintf(ihandle, iconn, "%8s %8llu %13llu\n", "total", tot_pkt_sum, tot_byte_sum); - iprintf(ihandle, iconn, "%8s %8llu %13llu\n", "DROP", discard_pkt_sum, discard_byte_sum); - - return 0; -} - -#endif - -#if 0 -static int cycle_pkt_dump_father_init(void) -{ - int i; - struct sockaddr_un child_pro_addr; - int addr_len; - char test_buf[1024] = "I am father"; - int buf_len = 0; - socklen_t opt_len; - - for(i = 0; i < g_packet_io_thread_num; i++){ - G_UNIX_DOMAIN_SD[i] = socket(AF_UNIX,SOCK_DGRAM,0); - if(G_UNIX_DOMAIN_SD[i] < 0){ - printf("socket error: %s\n", strerror(errno)); - return -1; - } - - addr_len = sizeof(child_pro_addr); - child_pro_addr.sun_family=AF_UNIX; - snprintf(child_pro_addr.sun_path, sizeof(child_pro_addr.sun_path), "%s%d", UNIX_DOMAIN_SOCKET_NAME, i); - - if(connect(G_UNIX_DOMAIN_SD[i], (struct sockaddr*)&child_pro_addr, addr_len) < 0){ - printf("connect %s error: %s\n", child_pro_addr.sun_path, strerror(errno)); - return -1; - } - } - - for(i = 0; i < g_packet_io_thread_num; i++){ - write(G_UNIX_DOMAIN_SD[i], test_buf, 1024); - } - - unlink(child_pro_addr.sun_path); -#if DEBUG - iknow_register_cmd(PLATFORM_IHANDLE, "show pktdump summary", "display packet dump status", - ifuncb_show_pktdump_summary, NULL, 0, NULL); -#endif - - return 0; -} -#endif - -#if 0 -static void cycle_pkt_dump_clear_last_status(void) -{ - char cmd_buf[128]; - - snprintf(cmd_buf, 128, "killall %s", PKT_DUMP_PROCESS_NAME); - - system(cmd_buf); -} - - -static void cycle_pkt_dump_exec(int argc, char *argv[]) -{ - int i; - pid_t pid; - pthread_t check_ppid, thread_id[MAX_THREAD_NUM]; - static int thread_seq[MAX_THREAD_NUM]; - - cycle_pkt_dump_clear_last_status(); - - pid = fork(); - if(0 == pid){ /* ӽ, */ - if(cycle_pkt_dump_child_init() < 0){ - printf("cycle pkt dump init error!\n"); - exit(1); - } - prctl(PR_SET_NAME, PKT_DUMP_PROCESS_NAME, 0, 0, 0); - }else if(pid > 0){ - usleep(50000); - if(cycle_pkt_dump_father_init() < 0){ - printf("cycle pkt dump init error!\n"); - exit(1); - } - return; /* ̷, sapp */ - }else{ - printf("cycle_pkt_dump fork error!\n"); - exit(0); - } - - for(i = 0; i < pkt_dump_thread_num; i++){ - if(G_PKT_DUMP_PER_THREAD_SW[i] != 0){ - thread_seq[i] = i; - pthread_create(&thread_id[i], NULL, cycle_pkt_dump_thread, &thread_seq[i]); - usleep(100); - } - } - - pthread_create(&check_ppid, NULL, cycle_pkt_dump_check_father, NULL); - - /* ӽ̵߳ȴ */ - for(i = 0; i < pkt_dump_thread_num; i++){ - if(G_PKT_DUMP_PER_THREAD_SW[i] != 0){ - pthread_join(thread_id[i], NULL); - } - } - - exit(0); -} -#endif - -static void cycle_pkt_dump_set_thread_num(int thread_num) -{ - pkt_dump_thread_num = thread_num; -} - -static int cycle_pkt_dump_get_core_num(void) -{ - FILE *fp; - char line_buf[PATH_MAX] = {0}; - int core_num = 0; - - fp = popen("ls -l core.* | wc -l", "r"); - if(NULL == fp){ - return -1; - } - - fgets(line_buf, PATH_MAX, fp); - - core_num = atoi(line_buf); - - pclose(fp); - return core_num; -} - -/* config is like: "0,1,2,3,4,5" */ -static int cycle_pkt_dump_parse_per_thread_switch(char *config_str) -{ - char *parse_config = config_str; - char *save_ptr; - char *token; - int i, thread_seq; - - if(strncasecmp("all", config_str, 3) == 0){ /* all thread is enable */ - for(i = 0; i < pkt_dump_thread_num; i++){ - G_PKT_DUMP_PER_THREAD_SW[i] = 1; - } - return 0; - } - - while((token = strtok_r(parse_config, ", \t", &save_ptr)) != NULL){ - thread_seq = atoi(token); - if(thread_seq < 0 || thread_seq >= pkt_dump_thread_num){ - printf("'pkt_dump_thread_seq' config invalid, threadnum=%d!\n", pkt_dump_thread_num); - return -1; - } - G_PKT_DUMP_PER_THREAD_SW[thread_seq] = 1; - parse_config = NULL; - } - - return 0; -} - - -static long cycle_pkt_dump_get_cur_files_size(void) -{ - FILE *fp; - long cur_files_size; - char cmd_str[PATH_MAX], tmp_str[1024]; - - snprintf(cmd_str, PATH_MAX, "du -scb %s | grep %s | awk {'print $1'}", G_PKT_DUMP_ROOT_DIR, G_PKT_DUMP_ROOT_DIR); - - fp = popen(cmd_str, "r"); - if(NULL == fp){ - return -1; - } - - if(NULL == fgets(tmp_str, 1024, fp)){ - return -1; - } - cur_files_size = (long)strtol(tmp_str, NULL, 10); - - pclose(fp); - - return cur_files_size; -} - -static int cycle_pkt_dump_get_cur_enable_thread(void) -{ - int i, sum = 0; - - for(i = 0; i < MAX_THREAD_NUM; i++){ - if(G_PKT_DUMP_PER_THREAD_SW[i] > 0){ - sum++; - } - } - - return sum; -} -/* - total_files_size: Ŀ¼ܵ.pcapС, λ:Byte; - file_size_per_thread_by_conf: ߳ļС, λ:Byte; - - յʵfile_size_per_threadֵ, ȡڵǰе.pcapļСtotal_files_sizeĹϵ; - ǰе.pcapļѳtotal_files_size, дµļ. -*/ -static int cycle_pkt_dump_set_file_size(long total_files_size, long file_size_per_thread_by_conf) -{ - long cur_files_size; - int cur_enable_thread_num = 0; - long file_size_per_thread_by_calc; /* ʵʼóÿ߳ļС */ - - cur_files_size = cycle_pkt_dump_get_cur_files_size(); - if(cur_files_size < 0 || cur_files_size >= total_files_size){ - printf("\033[33m[Warning]pkt_dump is enable, but current files size is more than 'pkt_dump_total_size', disable pktdump!\033[0m\n"); - usleep(100000); - G_PKT_DUMP_SW = 0; - return -1; - } - - cur_enable_thread_num = cycle_pkt_dump_get_cur_enable_thread(); - if(0 == cur_enable_thread_num){ - printf("\033[33m[Warning]pkt_dump is enable, but enable thread number is 0, disable pktdump!\033[0m\n"); - usleep(100000); - return -1; - } - - file_size_per_thread_by_calc = (total_files_size-cur_files_size)/cur_enable_thread_num/2; - - /* ȡС */ - G_PKT_DUMP_FILE_SIZE = (file_size_per_thread_by_calc < file_size_per_thread_by_conf?file_size_per_thread_by_calc:file_size_per_thread_by_conf); - if(G_PKT_DUMP_FILE_SIZE < 2048){ - printf("\033[33m[Warning]pkt_dump is enable, but current files size is more than 'pkt_dump_total_size'!\033[0m\n"); - return -1; - } - - return 0; -} - -static void pkt_dump_sig_ignore(int sig) -{ - return; -} - -static void pkt_dump_sig_handle(int sig) -{ - signal(SIGSEGV, pkt_dump_sig_ignore); /* SIGSEGV, һٵжϴƭϵͳ */ - sync(); /* δд̵ǿд */ - abort(); /* ֹ, coreļ */ -} - -static void cycle_pkt_dump_command_parse(int connfd) -{ - char cmd_buf[1024]; - int i, ret; - int *bpf_filter_len; /* with EOF */ - unsigned short *udp_rcv_port; - - /* ȷ2ֽtcpdump UDPն˿ */ - ret = MESA_sock_greedy_read(connfd, cmd_buf, 2, -1); - if(0 == ret){ - goto err_exit; - }else if(ret < 0){ - printf("sock read error, %s\n", strerror(errno)); - goto err_exit; - } - udp_rcv_port = (unsigned short *)&cmd_buf[0]; - - udp_recv_addr.sin_family = AF_INET; - udp_recv_addr.sin_addr.s_addr = htonl(0x7f000001); - udp_recv_addr.sin_port = htons(*udp_rcv_port); - - /* ٷ4ֽڳϢ, ʾBPF-filterַ, filterEOF */ - ret = MESA_sock_greedy_read(connfd, cmd_buf, 4, -1); - if(0 == ret){ - goto err_exit; - }else if(ret < 0){ - printf("sock read error, %s\n", strerror(errno)); - goto err_exit; - } - bpf_filter_len = (int *)&cmd_buf[0]; - - if(bpf_filter_len > 0){ /* ûfilter */ - ret = MESA_sock_greedy_read(connfd, cmd_buf, (size_t )(*bpf_filter_len), -1); - if(0 == ret){ - goto err_exit; - } - } - for(i = 0; i < g_packet_io_thread_num; i++){ - phony_pcap_handle[i] = pcap_open_dead(DLT_EN10MB, 65535); - if(NULL == phony_pcap_handle[i]){ - goto err_exit; - } - - if(bpf_filter_len > 0){ /* ûfilter */ - if(pcap_compile(phony_pcap_handle[i], &G_PKT_DUMP_BPF_FILTER[i], cmd_buf, 100, 0) < 0){ - printf("Compile pcap filter '%s' error:%s\n", cmd_buf, pcap_geterr(phony_pcap_handle[i])); - goto err_exit; - } - } - } - - pkt_dump_udp_socket_peer_alive = 1; - - while(MESA_sock_greedy_read(connfd, cmd_buf, 1, -1) != 0); - -err_exit: - pkt_dump_udp_socket_peer_alive = 0; - usleep(1000 * 500); /* ʱͷ, ߳ʹBPF */ - for(i = 0; i < g_packet_io_thread_num; i++){ - pcap_freecode(&G_PKT_DUMP_BPF_FILTER[i]); - } - close(connfd); - return; -} - -static void * cycle_pkt_dump_udp_socket_daemon_thread(void *arg) -{ - socklen_t cliaddr_len; - struct sockaddr_in cliaddr; - int connfd; -#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 480) /* 汾ж, ܶϵͳ޷ */ -#ifdef _GNU_SOURCE - pthread_setname_np(pthread_self(), "sapp_pkt_dump"); -#endif -#endif - - while(1){ - cliaddr_len = sizeof(cliaddr); - connfd = accept(pkt_dump_tcp_cmd_sd, (struct sockaddr *) &cliaddr, &cliaddr_len); - if(connfd > 0){ - cycle_pkt_dump_command_parse(connfd); - } - } - - return NULL; -} - -static int cycle_pkt_dump_socket_init(void) -{ - pthread_t pid; - int i, opt; - struct sockaddr_in sockadd; - - pkt_dump_tcp_cmd_sd = socket(AF_INET, SOCK_STREAM, 0); - - opt = 1; - setsockopt(pkt_dump_tcp_cmd_sd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); - - bzero(&sockadd, sizeof(sockadd)); - sockadd.sin_family = AF_INET; - sockadd.sin_addr.s_addr = htonl(INADDR_ANY); - sockadd.sin_port = htons(pkt_dump_tcp_bind_port); - - if(bind(pkt_dump_tcp_cmd_sd, (struct sockaddr *) &sockadd, sizeof(sockadd)) < 0){ - printf("pkt dump bind cmd port %u error, %s!\n", pkt_dump_tcp_bind_port, strerror(errno)); - return -1; - } - listen(pkt_dump_tcp_cmd_sd, 1); - - for(i = 0; i < g_packet_io_thread_num; i++){ - pkt_dump_udp_pkt_sd[i] = socket(AF_INET, SOCK_DGRAM, 0); - } - - pthread_create(&pid, NULL, cycle_pkt_dump_udp_socket_daemon_thread, NULL); - - return 0; -} - -int cycle_pkt_dump_init(int argc, char *argv[]) -{ - int i, ret = 0; - int int_tmp, total_files_size, core_num; - char per_thread_sw[PATH_MAX]; - char pcap_errbuf[PCAP_ERRBUF_SIZE]; - - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_switch", &G_PKT_DUMP_SW, 0); - - if(0 == G_PKT_DUMP_SW){ - return 0; - } - - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_mode", &G_PKT_DUMP_MODE, 1); - if((G_PKT_DUMP_MODE != PKT_DUMP_LOCAL_FILE) && (G_PKT_DUMP_MODE != PKT_DUMP_UDP_SOCKET)){ - printf("\n\033[41mError! pkt_dump_mode only support 1 or 2!\033[0m\n"); - return -1; - } - - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_cmd_port", &int_tmp, 12345); - if((int_tmp <=0) || (int_tmp > 65535)){ - printf("\n\033[41mError! pkt_dump_cmd_port invalid, %d!\033[0m\n", int_tmp); - return -1; - } - pkt_dump_tcp_bind_port = (unsigned short)int_tmp; - -#if 0 == CYCLE_PKT_DUMP - printf("\n\033[33m[Warning]pkt_dump is enable, but sapp is not compile correct, please use debug=2.\033[0m\n"); - sleep(1); -#endif - - if(PKT_DUMP_LOCAL_FILE == G_PKT_DUMP_MODE){ - cycle_pkt_dump_set_thread_num(g_packet_io_thread_num); - - core_num = cycle_pkt_dump_get_core_num(); - if(core_num < 0 || core_num > 10){ - printf("\033[33m[Warning]core.* number is more than 10, pkt dump is auto disabled!\n"); - G_PKT_DUMP_SW = 0; - return 0; - } - - MESA_load_profile_string_def((char *)"conf/main.conf",(char *)"pkt_dump", (char *)"pkt_dump_bpf_filter", G_PKT_DUMP_FILTER_STR, PATH_MAX, (char *)""); - if(G_PKT_DUMP_FILTER_STR[0] != '\0'){ - for(i = 0; i < g_packet_io_thread_num; i++){ - phony_pcap_handle[i] = pcap_open_dead(DLT_EN10MB, 65535); - if(NULL == phony_pcap_handle[i]){ - printf("pcap_open %s error, %s\n", "lo", pcap_errbuf); - printf("\n\033[41mpkt_dump_bpf_filter is not enable!\033[0m\n"); - goto skip_bpf_filter; - } - if(pcap_compile(phony_pcap_handle[i], &G_PKT_DUMP_BPF_FILTER[i], G_PKT_DUMP_FILTER_STR, 100, 0) < 0){ - printf("Compile pcap filter '%s' error:%s\n", G_PKT_DUMP_FILTER_STR, pcap_geterr(phony_pcap_handle[i])); - return -1; - } - } - } - - skip_bpf_filter: - - MESA_load_profile_string_def((char *)"conf/main.conf",(char *)"pkt_dump", (char *)"pkt_dump_file_root_dir", G_PKT_DUMP_ROOT_DIR, PATH_MAX, (char *)"/tmp/pkt_dump"); - MESA_mkdir_p(G_PKT_DUMP_ROOT_DIR, 0755); - - MESA_load_profile_string_nodef((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_thread_seq", per_thread_sw, PATH_MAX); - if(cycle_pkt_dump_parse_per_thread_switch(per_thread_sw) < 0){ - return 0; - } - - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_total_size", &total_files_size, 1000); - if(total_files_size < 1 || (unsigned int)total_files_size >= 1024*100 /* 100GB */){ - printf("pkt_dump_total_size invalid!\n"); - return -1; - } - - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_file_max_size", &int_tmp, 100); - if(int_tmp < 2 || (unsigned int)int_tmp >= 1024*100 /* 100GB */){ - printf("pkt_dump_file_max_size invalid!\n"); - return -1; - } - /* ÿ߳Ҫѭд2ļ, Ҫpkt_dump_file_max_sizeʵֵٳ2 */ - if(cycle_pkt_dump_set_file_size((long)total_files_size*1024*1024, (long)int_tmp*1024*512) < 0){ - return 0; - } - - signal(SIGPIPE, SIG_IGN); - /* SIGSEGVź, ֤쳣ֹʱ, һBUGpcapдӲ */ - if((SIG_ERR == signal(SIGSEGV, pkt_dump_sig_handle))){ - printf("Catch SIGSEGV error!\n"); - } - -#if IKNOW_ENABLE - iknow_register_cmd(PLATFORM_IHANDLE, "show pkt dump summary", "display pkt dump summary", - ifuncb_show_pktdump_summary, NULL, 0, NULL); -#endif - printf("\n\033[33m[Warning]pkt dump enable, this maybe encumber performance.\033[0m\n"); - sleep(1); - }else{ - ret = cycle_pkt_dump_socket_init(); - } - - return ret; -} - - -#ifdef __cplusplus -} -#endif - - - diff --git a/src/packet_io/cycle_pkt_dump_through_write.c.bak b/src/packet_io/cycle_pkt_dump_through_write.c.bak new file mode 100644 index 0000000..8eeafd3 --- /dev/null +++ b/src/packet_io/cycle_pkt_dump_through_write.c.bak @@ -0,0 +1,956 @@ +#include "sapp_api.h" +#include "sapp_private_api.h" + +/* + 2015-04-15 , LiJia. + cycle_pkt_dump, + ʵʱѭ洢ݰĿǽ߻ҵֻһcore, BUG޷. + sappcoredump 'pkt_dump_file_root_dir'Ŀ¼洢һʱյİ, + öģʽ֣пܷBUGԴ + ֣pkt_dump_file_max_sizeʵһЩ. + + main.conf1µsection5: + [pkt_dump] + pkt_dump_switch=0 //ܿ + pkt_dump_total_size=1000 //root_dirܼ.pcapļĴС, λ:MB, ֹƶдӲ + pkt_dump_file_max_size=200 //pcapļÿֵ߳̿, λ:MB. + pkt_dump_file_root_dir=/dev/shm //pcap洢·, Ƽ/dev/shm, Ӳٶȿܶ + pkt_dump_thread_seq=0,2,4,6,8 //ÿ̵߳ĿأΪܾ޴޷ȫ洢Ǿֻò̵߳dump , all=ȫ + +*/ + +#ifdef __cplusplus +extern "C" { +#endif + + +#define UNIX_DOMAIN_SOCKET_NAME "/tmp/.pktdump" +#define PKT_DUMP_PROCESS_NAME "sapp_pkt_dump" + +extern iknow_handle PLATFORM_IHANDLE; + +enum dump_mode{ + PKT_DUMP_LOCAL_FILE = 1, /* дļ */ + PKT_DUMP_UDP_SOCKET = 2, /* дUDP ij˿ */ +}; + +#if DEBUG +#include "iknow.h" + +typedef struct{ + unsigned long long total_pkt_num; + unsigned long long total_pkt_bytes; + unsigned long long discard_pkt_num; + unsigned long long discard_pkt_bytes; + char __pad__[32]; /* 64ֽcache */ +}pkt_dump_status_t; +static pkt_dump_status_t PKT_DISCARD_STAT[MAX_THREAD_NUM]; + +#endif + +extern int g_use_MESA_sleep_sw; +extern int MESA_mkdir_p(const char *pathname, mode_t mode); +//extern time_t g_CurrentTime; +//extern int g_packet_io_thread_num; +//extern int g_iThreadNum; /* for dual_stack, start */ +static char G_PKT_DUMP_ROOT_DIR[PATH_MAX]; /* ָΪ/dev/shm, ٶȿ */ +static long G_PKT_DUMP_FILE_SIZE = 104857600; /* Ĭ100MB, ߳ */ +int G_PKT_DUMP_SW = 0; /* ĬϹر */ +int G_PKT_DUMP_MODE = PKT_DUMP_LOCAL_FILE; /* Ĭдļ */ +static int G_PKT_DUMP_PER_THREAD_SW[MAX_THREAD_NUM]; + +static int pkt_dump_thread_num = 1; + +static FILE *G_PKT_DUMP_FP[MAX_THREAD_NUM]; +static int G_FILENAME_INDEX[MAX_THREAD_NUM]; +static long G_PCAP_SIZE[MAX_THREAD_NUM]; +static char G_PKT_DUMP_FILTER_STR[PATH_MAX]; +static struct bpf_program G_PKT_DUMP_BPF_FILTER[MAX_THREAD_NUM]; +static pcap_t *phony_pcap_handle[MAX_THREAD_NUM]; /* BPFʱʹ */ + +static unsigned short pkt_dump_tcp_bind_port = 12345; /* ĬTCP˿, ڽⲿtcpdump */ +static int pkt_dump_tcp_cmd_sd = -1;/* TCP fd, ڽⲿtcpdump */ +static volatile int pkt_dump_udp_socket_peer_alive = 0; /* ⲿtcpdumpǷ */ +static int pkt_dump_udp_pkt_sd[MAX_THREAD_NUM]; /* UDPݰfd */ +static struct sockaddr_in udp_recv_addr; + +static unsigned char phony_mac_hdr[14] = +{ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x12, 0x23, 0x34, 0x56, 0x67, 0x78, + 0x08, 0x00 +}; +typedef struct { + unsigned int magic; + unsigned short version_major; + unsigned short version_minor; + unsigned int thiszone; /* gmt to local correction */ + unsigned int sigfigs; /* accuracy of timestamps */ + unsigned int snaplen; /* max length saved portion of each pkt */ + unsigned int linktype; /* data link type (LINKTYPE_*) */ +}pkt_dump_file_hdr_t; + +struct pkt_dump_timeval { + unsigned int tv_sec; /* seconds */ + unsigned int tv_usec; /* microseconds */ +}; + +typedef struct { + struct pkt_dump_timeval ts; /* time stamp */ + unsigned int caplen; /* length of portion present */ + unsigned int len; /* length this packet (off wire) */ +}pkt_dump_pkt_hdr_t; + +typedef struct{ + enum addr_type_t low_layer_type; /* ԭʼײЭ, MAC(pcap), ҲIPv4(pag) */ + int raw_pkt_len; /* ԭʼܳ */ + //char pkt_data[]; +}pkt_dump_pipe_t; + + +static const pkt_dump_file_hdr_t pfile_hdr = +{ + 0xA1B2C3D4, + 0x0002, + 0x0004, + 0, + 0, + 0xFFFF, + 1 +}; + +/* reliable read */ +static inline ssize_t Rread(int fd, void *buf, size_t expect_count) +{ + ssize_t ret; + void *save_ptr = buf; + size_t left_count = expect_count; + + while(left_count > 0){ +retry: + ret = read(fd, save_ptr, left_count); + if(ret < 0){ + switch(errno){ + case EAGAIN: + if(0 == g_use_MESA_sleep_sw){ + usleep(1); + }else{ + MESA_sleep(); + } + /* no break here!!! no break here!!! no break here!!!*/ + case EINTR: + goto retry; + break; + + default: + printf("pkt dump, read error:%s, ptr=%p, expect_count=%lu, left_count=%lu\n", + strerror(errno), save_ptr, expect_count, left_count); + break; + } + }else if(0 == ret){ + return 0; /* peer close */ + }else{ + save_ptr = (char *)save_ptr + ret; + left_count -= ret; + } + } + + return expect_count - left_count; +} + +/* reliable write */ +static inline ssize_t Rwrite(int fd, const void *buf, size_t expect_count) +{ + ssize_t ret; + const void *real_ptr = buf; + size_t left_count = expect_count; + + while(left_count > 0){ +retry: + ret = write(fd, real_ptr, left_count); + if(ret < 0){ + switch(errno){ + case EINTR: + goto retry; + break; + + default: + printf("pkt dump, write error:%s\n", strerror(errno)); + /* дռ䲻, , Ӱ */ + break; + } + }else{ + real_ptr = (const char *)real_ptr + ret; + left_count -= ret; + } + } + + return expect_count - left_count; +} + +#if 0 +static void *cycle_pkt_dump_check_father(void *arg) +{ + while(1){ + if(1 == getppid()){ /* ̱Ϊinit, ˵Ѿ˳ */ + exit(1); + }else{ + usleep(100000); + } + } + + return NULL; +} + + +static void *cycle_pkt_dump_thread(void *arg) +{ + int thread_seq = *((int *)arg); + FILE *fp; + int file_name_post_prefix = 0, ret, pfile_hdr_flag = 0; + long tot_pcap_file_len = 0; + char pkt_buf[65536]; + char dump_file_name[PATH_MAX]; + pkt_dump_pkt_hdr_t *ppkt_hdr; + int pkt_len; + int skip_pkt_hdr_len; /* Ԥͷռ䳤, ݲģʽͬ, ܻEth */ + + snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", + G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, file_name_post_prefix); + fp = fopen(dump_file_name, "w+"); + if(NULL == fp){ + printf("fopen %s error!\n", dump_file_name); + goto done; + } + ppkt_hdr = (pkt_dump_pkt_hdr_t *)pkt_buf; + ppkt_hdr->ts.tv_usec = 0; +#ifdef CAPTURE_MODE_PAG + memcpy(pkt_buf+sizeof(pkt_dump_pkt_hdr_t), phony_mac_hdr, 14); + skip_pkt_hdr_len = sizeof(pkt_dump_pkt_hdr_t) + 14; +#else + skip_pkt_hdr_len = sizeof(pkt_dump_pkt_hdr_t); +#endif + while(1){ + /* pcapͷռԤ, ֻһfwrite, ͽͷһд */ + pkt_len = read(G_UNIX_DOMAIN_SD[thread_seq], (char *)pkt_buf+skip_pkt_hdr_len, 2048); + if(0 == pkt_len){ + goto done; + }else if(pkt_len < 0){ + continue; + } + + /* pcap file header, first time */ + if(0 == pfile_hdr_flag){ + fwrite(&pfile_hdr, sizeof(pkt_dump_file_hdr_t), 1, fp); + tot_pcap_file_len += sizeof(pkt_dump_file_hdr_t); + pfile_hdr_flag = 1; + } + /* °, ʱ */ + ppkt_hdr->ts.tv_sec = (unsigned int )g_CurrentTime; +#ifdef CAPTURE_MODE_PAG + ppkt_hdr->caplen = pkt_len+14; + ppkt_hdr->len = pkt_len+14; +#else + ppkt_hdr->caplen = pkt_len; + ppkt_hdr->len = pkt_len; +#endif + + fwrite(pkt_buf, pkt_len+skip_pkt_hdr_len, 1, fp); + tot_pcap_file_len += pkt_len+skip_pkt_hdr_len; + + /* , ѭдļ */ + if(tot_pcap_file_len >= G_PKT_DUMP_FILE_SIZE){ + fclose(fp); + file_name_post_prefix = file_name_post_prefix ^ 1; + snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", + G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, file_name_post_prefix); + fp = fopen(dump_file_name, "w+"); /* cycle write two files */ + if(NULL == fp){ + printf("fopen %s error!\n", dump_file_name); + goto done; + } + tot_pcap_file_len = 0; + pfile_hdr_flag = 0; + } + } + +done: + + return NULL; +} +#endif + + +static void __do_cycle_pkt_dump_udp_socket(int thread_seq, const raw_pkt_t *p_raw_pkt) +{ + if(1 == pkt_dump_udp_socket_peer_alive){ + if(g_packet_io_cap_level != CAP_LEVEL_MAC){ + if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, + (const unsigned char *)p_raw_pkt->raw_pkt_data-14, + p_raw_pkt->raw_pkt_len+14, p_raw_pkt->raw_pkt_len+14)){ + return; + } + }else{ + if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, + (const unsigned char *)p_raw_pkt->raw_pkt_data, + p_raw_pkt->raw_pkt_len, p_raw_pkt->raw_pkt_len)){ + return; + } + } + + + /* bingo, match filter, sendto */ + sendto(pkt_dump_udp_pkt_sd[thread_seq], p_raw_pkt->raw_pkt_data, p_raw_pkt->raw_pkt_len, + MSG_DONTWAIT, + (const struct sockaddr *)&udp_recv_addr, + sizeof(udp_recv_addr)); + } +} + +static void __do_cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt) +{ + int ret; + /* Ϊpcap_hdr[2],һССtrick, [0]ʾpcapͷϢ, + [1]ʾPAGģʽ, 洢etherhdr, pkt_dump_pkt_hdr_tΪ16ֽ, ÿԷһether hdr. + */ + pkt_dump_pkt_hdr_t pcap_hdr[2]; + int write_hdr_len; + char dump_file_name[PATH_MAX]; + + if(PKT_DUMP_LOCAL_FILE == G_PKT_DUMP_MODE){ + if(0 == G_PKT_DUMP_PER_THREAD_SW[thread_seq]){ + return; + } + }else{ + __do_cycle_pkt_dump_udp_socket(thread_seq, p_raw_pkt); + return; + } + + if(G_PKT_DUMP_FILTER_STR[0] != '\0'){ + if(g_packet_io_cap_level != CAP_LEVEL_MAC){ + if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, + (const unsigned char *)p_raw_pkt->raw_pkt_data-14, + p_raw_pkt->raw_pkt_len+14, p_raw_pkt->raw_pkt_len+14)){ + return; + } + }else{ + if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, + (const unsigned char *)p_raw_pkt->raw_pkt_data, + p_raw_pkt->raw_pkt_len, p_raw_pkt->raw_pkt_len)){ + return; + } + } + } + + if(unlikely(NULL == G_PKT_DUMP_FP[thread_seq])){ + snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", + G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, G_FILENAME_INDEX[thread_seq]); + G_PKT_DUMP_FP[thread_seq] = fopen(dump_file_name, "wb+"); + if(NULL == G_PKT_DUMP_FP[thread_seq]){ + printf("fopen %s error!\n", dump_file_name); + return; + } + /* pcap file header, first time */ + fwrite(&pfile_hdr, sizeof(pkt_dump_file_hdr_t), 1, G_PKT_DUMP_FP[thread_seq]); + } + + /* °, ʱ */ + pcap_hdr[0].ts.tv_sec = (unsigned int )g_CurrentTime; + + if(g_packet_io_cap_level != CAP_LEVEL_MAC){ + pcap_hdr[0].caplen = p_raw_pkt->raw_pkt_len+14; + pcap_hdr[0].len = p_raw_pkt->raw_pkt_len+14; + memcpy(&pcap_hdr[1], phony_mac_hdr, 14); + write_hdr_len = sizeof(pkt_dump_pkt_hdr_t) + 14; + }else{ + pcap_hdr[0].caplen = p_raw_pkt->raw_pkt_len; + pcap_hdr[0].len = p_raw_pkt->raw_pkt_len; + write_hdr_len = sizeof(pkt_dump_pkt_hdr_t); + } + + ret = fwrite(&pcap_hdr[0], write_hdr_len, 1, G_PKT_DUMP_FP[thread_seq]); + if(unlikely(ret < 1)){ + return; + } + ret = fwrite(p_raw_pkt->raw_pkt_data, p_raw_pkt->raw_pkt_len, 1, G_PKT_DUMP_FP[thread_seq]); + if(unlikely(ret < 1)){ + /* ֻдͷ, дݳִ, ع */ + fseek(G_PKT_DUMP_FP[thread_seq], (int)0-(int)sizeof(pkt_dump_pkt_hdr_t), SEEK_CUR); + return; + } + + /* , ѭдļ */ + G_PCAP_SIZE[thread_seq] += write_hdr_len + p_raw_pkt->raw_pkt_len; + if(unlikely(G_PCAP_SIZE[thread_seq] >= G_PKT_DUMP_FILE_SIZE)){ + fclose(G_PKT_DUMP_FP[thread_seq]); + G_FILENAME_INDEX[thread_seq] ^= 1; + snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", + G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, G_FILENAME_INDEX[thread_seq]); + G_PKT_DUMP_FP[thread_seq] = fopen(dump_file_name, "wb+"); + if(NULL == G_PKT_DUMP_FP[thread_seq]){ + printf("fopen %s error!\n", dump_file_name); + return; + } + /* pcap file header, first time */ + fwrite(&pfile_hdr, sizeof(pkt_dump_file_hdr_t), 1, G_PKT_DUMP_FP[thread_seq]); + G_PCAP_SIZE[thread_seq] = 0; + } + + return; +} + +void cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt) +{ + if(likely(0 == G_PKT_DUMP_SW)){ + return; + } + + __do_cycle_pkt_dump(thread_seq, p_raw_pkt); +} + +void cycle_pkt_dump_old(int thread_seq, const void *ippkt, int ippktlen) +{ + raw_pkt_t raw_pkt; + + raw_pkt.raw_pkt_data = ippkt; + raw_pkt.raw_pkt_len = ippktlen; + + cycle_pkt_dump(thread_seq, &raw_pkt); +} + +#if 0 +static int cycle_pkt_dump_child_init(void) +{ + int i, ret; + struct sockaddr_un father_pro_addr; + int addr_len; + char test_buf[1024]; + int buf_len = 0; + socklen_t opt_len; + + for(i = 0; i < g_packet_io_thread_num; i++){ + G_UNIX_DOMAIN_SD[i] = socket(AF_UNIX,SOCK_DGRAM,0); + if(G_UNIX_DOMAIN_SD[i] < 0){ + printf("socket error: %s\n", strerror(errno)); + return -1; + } + + addr_len = sizeof(father_pro_addr); + father_pro_addr.sun_family=AF_UNIX; + snprintf(father_pro_addr.sun_path, sizeof(father_pro_addr.sun_path), "%s%d", UNIX_DOMAIN_SOCKET_NAME, i); + + unlink(father_pro_addr.sun_path); + if(bind(G_UNIX_DOMAIN_SD[i], (struct sockaddr *)&father_pro_addr, addr_len) < 0){ + printf("bind %s error: %s\n", father_pro_addr.sun_path, strerror(errno)); + return -1; + } + + listen(G_UNIX_DOMAIN_SD[i], 10); + } + + usleep(10000); + + for(i = 0; i < g_packet_io_thread_num; i++){ + ret = read(G_UNIX_DOMAIN_SD[i], test_buf, 1024); /* ȴ̵, ȷ˵ */ + if(ret <= 0){ + printf("socket read error:%s\n", strerror(errno)); + return -1; + } + } + + return 0; +} +#endif + + +#if DEBUG +static long ifuncb_show_pktdump_summary(iknow_handle ihandle, iknow_conn iconn, const void *cb_fun_arg, const char *cmd_args) +{ + int i; + unsigned long long discard_pkt_sum = 0, discard_byte_sum = 0; + unsigned long long tot_pkt_sum = 0, tot_byte_sum = 0; + + for(i = 0; i < g_packet_io_thread_num; i++){ + tot_pkt_sum += PKT_DISCARD_STAT[i].total_pkt_num; + tot_byte_sum += PKT_DISCARD_STAT[i].total_pkt_bytes; + discard_pkt_sum += PKT_DISCARD_STAT[i].discard_pkt_num; + discard_byte_sum += PKT_DISCARD_STAT[i].discard_pkt_bytes; + } + //iprintf(ihandle, iconn,"NOTE: unit of byte for total is 'byte', for realtime is 'bps, bit per second'!\n"); + iprintf(ihandle, iconn, "%8s %8s %13s\n", "", "packet", "byte"); + iprintf(ihandle, iconn, "%8s %8llu %13llu\n", "total", tot_pkt_sum, tot_byte_sum); + iprintf(ihandle, iconn, "%8s %8llu %13llu\n", "DROP", discard_pkt_sum, discard_byte_sum); + + return 0; +} + +#endif + +#if 0 +static int cycle_pkt_dump_father_init(void) +{ + int i; + struct sockaddr_un child_pro_addr; + int addr_len; + char test_buf[1024] = "I am father"; + int buf_len = 0; + socklen_t opt_len; + + for(i = 0; i < g_packet_io_thread_num; i++){ + G_UNIX_DOMAIN_SD[i] = socket(AF_UNIX,SOCK_DGRAM,0); + if(G_UNIX_DOMAIN_SD[i] < 0){ + printf("socket error: %s\n", strerror(errno)); + return -1; + } + + addr_len = sizeof(child_pro_addr); + child_pro_addr.sun_family=AF_UNIX; + snprintf(child_pro_addr.sun_path, sizeof(child_pro_addr.sun_path), "%s%d", UNIX_DOMAIN_SOCKET_NAME, i); + + if(connect(G_UNIX_DOMAIN_SD[i], (struct sockaddr*)&child_pro_addr, addr_len) < 0){ + printf("connect %s error: %s\n", child_pro_addr.sun_path, strerror(errno)); + return -1; + } + } + + for(i = 0; i < g_packet_io_thread_num; i++){ + write(G_UNIX_DOMAIN_SD[i], test_buf, 1024); + } + + unlink(child_pro_addr.sun_path); +#if DEBUG + iknow_register_cmd(PLATFORM_IHANDLE, "show pktdump summary", "display packet dump status", + ifuncb_show_pktdump_summary, NULL, 0, NULL); +#endif + + return 0; +} +#endif + +#if 0 +static void cycle_pkt_dump_clear_last_status(void) +{ + char cmd_buf[128]; + + snprintf(cmd_buf, 128, "killall %s", PKT_DUMP_PROCESS_NAME); + + system(cmd_buf); +} + + +static void cycle_pkt_dump_exec(int argc, char *argv[]) +{ + int i; + pid_t pid; + pthread_t check_ppid, thread_id[MAX_THREAD_NUM]; + static int thread_seq[MAX_THREAD_NUM]; + + cycle_pkt_dump_clear_last_status(); + + pid = fork(); + if(0 == pid){ /* ӽ, */ + if(cycle_pkt_dump_child_init() < 0){ + printf("cycle pkt dump init error!\n"); + exit(1); + } + prctl(PR_SET_NAME, PKT_DUMP_PROCESS_NAME, 0, 0, 0); + }else if(pid > 0){ + usleep(50000); + if(cycle_pkt_dump_father_init() < 0){ + printf("cycle pkt dump init error!\n"); + exit(1); + } + return; /* ̷, sapp */ + }else{ + printf("cycle_pkt_dump fork error!\n"); + exit(0); + } + + for(i = 0; i < pkt_dump_thread_num; i++){ + if(G_PKT_DUMP_PER_THREAD_SW[i] != 0){ + thread_seq[i] = i; + pthread_create(&thread_id[i], NULL, cycle_pkt_dump_thread, &thread_seq[i]); + usleep(100); + } + } + + pthread_create(&check_ppid, NULL, cycle_pkt_dump_check_father, NULL); + + /* ӽ̵߳ȴ */ + for(i = 0; i < pkt_dump_thread_num; i++){ + if(G_PKT_DUMP_PER_THREAD_SW[i] != 0){ + pthread_join(thread_id[i], NULL); + } + } + + exit(0); +} +#endif + +static void cycle_pkt_dump_set_thread_num(int thread_num) +{ + pkt_dump_thread_num = thread_num; +} + +static int cycle_pkt_dump_get_core_num(void) +{ + FILE *fp; + char line_buf[PATH_MAX] = {0}; + int core_num = 0; + + fp = popen("ls -l core.* | wc -l", "r"); + if(NULL == fp){ + return -1; + } + + fgets(line_buf, PATH_MAX, fp); + + core_num = atoi(line_buf); + + pclose(fp); + return core_num; +} + +/* config is like: "0,1,2,3,4,5" */ +static int cycle_pkt_dump_parse_per_thread_switch(char *config_str) +{ + char *parse_config = config_str; + char *save_ptr; + char *token; + int i, thread_seq; + + if(strncasecmp("all", config_str, 3) == 0){ /* all thread is enable */ + for(i = 0; i < pkt_dump_thread_num; i++){ + G_PKT_DUMP_PER_THREAD_SW[i] = 1; + } + return 0; + } + + while((token = strtok_r(parse_config, ", \t", &save_ptr)) != NULL){ + thread_seq = atoi(token); + if(thread_seq < 0 || thread_seq >= pkt_dump_thread_num){ + printf("'pkt_dump_thread_seq' config invalid, threadnum=%d!\n", pkt_dump_thread_num); + return -1; + } + G_PKT_DUMP_PER_THREAD_SW[thread_seq] = 1; + parse_config = NULL; + } + + return 0; +} + + +static long cycle_pkt_dump_get_cur_files_size(void) +{ + FILE *fp; + long cur_files_size; + char cmd_str[PATH_MAX], tmp_str[1024]; + + snprintf(cmd_str, PATH_MAX, "du -scb %s | grep %s | awk {'print $1'}", G_PKT_DUMP_ROOT_DIR, G_PKT_DUMP_ROOT_DIR); + + fp = popen(cmd_str, "r"); + if(NULL == fp){ + return -1; + } + + if(NULL == fgets(tmp_str, 1024, fp)){ + return -1; + } + cur_files_size = (long)strtol(tmp_str, NULL, 10); + + pclose(fp); + + return cur_files_size; +} + +static int cycle_pkt_dump_get_cur_enable_thread(void) +{ + int i, sum = 0; + + for(i = 0; i < MAX_THREAD_NUM; i++){ + if(G_PKT_DUMP_PER_THREAD_SW[i] > 0){ + sum++; + } + } + + return sum; +} +/* + total_files_size: Ŀ¼ܵ.pcapС, λ:Byte; + file_size_per_thread_by_conf: ߳ļС, λ:Byte; + + յʵfile_size_per_threadֵ, ȡڵǰе.pcapļСtotal_files_sizeĹϵ; + ǰе.pcapļѳtotal_files_size, дµļ. +*/ +static int cycle_pkt_dump_set_file_size(long total_files_size, long file_size_per_thread_by_conf) +{ + long cur_files_size; + int cur_enable_thread_num = 0; + long file_size_per_thread_by_calc; /* ʵʼóÿ߳ļС */ + + cur_files_size = cycle_pkt_dump_get_cur_files_size(); + if(cur_files_size < 0 || cur_files_size >= total_files_size){ + printf("\033[33m[Warning]pkt_dump is enable, but current files size is more than 'pkt_dump_total_size', disable pktdump!\033[0m\n"); + usleep(100000); + G_PKT_DUMP_SW = 0; + return -1; + } + + cur_enable_thread_num = cycle_pkt_dump_get_cur_enable_thread(); + if(0 == cur_enable_thread_num){ + printf("\033[33m[Warning]pkt_dump is enable, but enable thread number is 0, disable pktdump!\033[0m\n"); + usleep(100000); + return -1; + } + + file_size_per_thread_by_calc = (total_files_size-cur_files_size)/cur_enable_thread_num/2; + + /* ȡС */ + G_PKT_DUMP_FILE_SIZE = (file_size_per_thread_by_calc < file_size_per_thread_by_conf?file_size_per_thread_by_calc:file_size_per_thread_by_conf); + if(G_PKT_DUMP_FILE_SIZE < 2048){ + printf("\033[33m[Warning]pkt_dump is enable, but current files size is more than 'pkt_dump_total_size'!\033[0m\n"); + return -1; + } + + return 0; +} + +static void pkt_dump_sig_ignore(int sig) +{ + return; +} + +static void pkt_dump_sig_handle(int sig) +{ + signal(SIGSEGV, pkt_dump_sig_ignore); /* SIGSEGV, һٵжϴƭϵͳ */ + sync(); /* δд̵ǿд */ + abort(); /* ֹ, coreļ */ +} + +static void cycle_pkt_dump_command_parse(int connfd) +{ + char cmd_buf[1024]; + int i, ret; + int *bpf_filter_len; /* with EOF */ + unsigned short *udp_rcv_port; + + /* ȷ2ֽtcpdump UDPն˿ */ + ret = MESA_sock_greedy_read(connfd, cmd_buf, 2, -1); + if(0 == ret){ + goto err_exit; + }else if(ret < 0){ + printf("sock read error, %s\n", strerror(errno)); + goto err_exit; + } + udp_rcv_port = (unsigned short *)&cmd_buf[0]; + + udp_recv_addr.sin_family = AF_INET; + udp_recv_addr.sin_addr.s_addr = htonl(0x7f000001); + udp_recv_addr.sin_port = htons(*udp_rcv_port); + + /* ٷ4ֽڳϢ, ʾBPF-filterַ, filterEOF */ + ret = MESA_sock_greedy_read(connfd, cmd_buf, 4, -1); + if(0 == ret){ + goto err_exit; + }else if(ret < 0){ + printf("sock read error, %s\n", strerror(errno)); + goto err_exit; + } + bpf_filter_len = (int *)&cmd_buf[0]; + + if(bpf_filter_len > 0){ /* ûfilter */ + ret = MESA_sock_greedy_read(connfd, cmd_buf, (size_t )(*bpf_filter_len), -1); + if(0 == ret){ + goto err_exit; + } + } + for(i = 0; i < g_packet_io_thread_num; i++){ + phony_pcap_handle[i] = pcap_open_dead(DLT_EN10MB, 65535); + if(NULL == phony_pcap_handle[i]){ + goto err_exit; + } + + if(bpf_filter_len > 0){ /* ûfilter */ + if(pcap_compile(phony_pcap_handle[i], &G_PKT_DUMP_BPF_FILTER[i], cmd_buf, 100, 0) < 0){ + printf("Compile pcap filter '%s' error:%s\n", cmd_buf, pcap_geterr(phony_pcap_handle[i])); + goto err_exit; + } + } + } + + pkt_dump_udp_socket_peer_alive = 1; + + while(MESA_sock_greedy_read(connfd, cmd_buf, 1, -1) != 0); + +err_exit: + pkt_dump_udp_socket_peer_alive = 0; + usleep(1000 * 500); /* ʱͷ, ߳ʹBPF */ + for(i = 0; i < g_packet_io_thread_num; i++){ + pcap_freecode(&G_PKT_DUMP_BPF_FILTER[i]); + } + close(connfd); + return; +} + +static void * cycle_pkt_dump_udp_socket_daemon_thread(void *arg) +{ + socklen_t cliaddr_len; + struct sockaddr_in cliaddr; + int connfd; +#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 480) /* 汾ж, ܶϵͳ޷ */ +#ifdef _GNU_SOURCE + pthread_setname_np(pthread_self(), "sapp_pkt_dump"); +#endif +#endif + + while(1){ + cliaddr_len = sizeof(cliaddr); + connfd = accept(pkt_dump_tcp_cmd_sd, (struct sockaddr *) &cliaddr, &cliaddr_len); + if(connfd > 0){ + cycle_pkt_dump_command_parse(connfd); + } + } + + return NULL; +} + +static int cycle_pkt_dump_socket_init(void) +{ + pthread_t pid; + int i, opt; + struct sockaddr_in sockadd; + + pkt_dump_tcp_cmd_sd = socket(AF_INET, SOCK_STREAM, 0); + + opt = 1; + setsockopt(pkt_dump_tcp_cmd_sd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); + + bzero(&sockadd, sizeof(sockadd)); + sockadd.sin_family = AF_INET; + sockadd.sin_addr.s_addr = htonl(INADDR_ANY); + sockadd.sin_port = htons(pkt_dump_tcp_bind_port); + + if(bind(pkt_dump_tcp_cmd_sd, (struct sockaddr *) &sockadd, sizeof(sockadd)) < 0){ + printf("pkt dump bind cmd port %u error, %s!\n", pkt_dump_tcp_bind_port, strerror(errno)); + return -1; + } + listen(pkt_dump_tcp_cmd_sd, 1); + + for(i = 0; i < g_packet_io_thread_num; i++){ + pkt_dump_udp_pkt_sd[i] = socket(AF_INET, SOCK_DGRAM, 0); + } + + pthread_create(&pid, NULL, cycle_pkt_dump_udp_socket_daemon_thread, NULL); + + return 0; +} + +int cycle_pkt_dump_init(int argc, char *argv[]) +{ + int i, ret = 0; + int int_tmp, total_files_size, core_num; + char per_thread_sw[PATH_MAX]; + char pcap_errbuf[PCAP_ERRBUF_SIZE]; + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_switch", &G_PKT_DUMP_SW, 0); + + if(0 == G_PKT_DUMP_SW){ + return 0; + } + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_mode", &G_PKT_DUMP_MODE, 1); + if((G_PKT_DUMP_MODE != PKT_DUMP_LOCAL_FILE) && (G_PKT_DUMP_MODE != PKT_DUMP_UDP_SOCKET)){ + printf("\n\033[41mError! pkt_dump_mode only support 1 or 2!\033[0m\n"); + return -1; + } + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_cmd_port", &int_tmp, 12345); + if((int_tmp <=0) || (int_tmp > 65535)){ + printf("\n\033[41mError! pkt_dump_cmd_port invalid, %d!\033[0m\n", int_tmp); + return -1; + } + pkt_dump_tcp_bind_port = (unsigned short)int_tmp; + +#if 0 == CYCLE_PKT_DUMP + printf("\n\033[33m[Warning]pkt_dump is enable, but sapp is not compile correct, please use debug=2.\033[0m\n"); + sleep(1); +#endif + + if(PKT_DUMP_LOCAL_FILE == G_PKT_DUMP_MODE){ + cycle_pkt_dump_set_thread_num(g_packet_io_thread_num); + + core_num = cycle_pkt_dump_get_core_num(); + if(core_num < 0 || core_num > 10){ + printf("\033[33m[Warning]core.* number is more than 10, pkt dump is auto disabled!\n"); + G_PKT_DUMP_SW = 0; + return 0; + } + + MESA_load_profile_string_def((char *)"conf/main.conf",(char *)"pkt_dump", (char *)"pkt_dump_bpf_filter", G_PKT_DUMP_FILTER_STR, PATH_MAX, (char *)""); + if(G_PKT_DUMP_FILTER_STR[0] != '\0'){ + for(i = 0; i < g_packet_io_thread_num; i++){ + phony_pcap_handle[i] = pcap_open_dead(DLT_EN10MB, 65535); + if(NULL == phony_pcap_handle[i]){ + printf("pcap_open %s error, %s\n", "lo", pcap_errbuf); + printf("\n\033[41mpkt_dump_bpf_filter is not enable!\033[0m\n"); + goto skip_bpf_filter; + } + if(pcap_compile(phony_pcap_handle[i], &G_PKT_DUMP_BPF_FILTER[i], G_PKT_DUMP_FILTER_STR, 100, 0) < 0){ + printf("Compile pcap filter '%s' error:%s\n", G_PKT_DUMP_FILTER_STR, pcap_geterr(phony_pcap_handle[i])); + return -1; + } + } + } + + skip_bpf_filter: + + MESA_load_profile_string_def((char *)"conf/main.conf",(char *)"pkt_dump", (char *)"pkt_dump_file_root_dir", G_PKT_DUMP_ROOT_DIR, PATH_MAX, (char *)"/tmp/pkt_dump"); + MESA_mkdir_p(G_PKT_DUMP_ROOT_DIR, 0755); + + MESA_load_profile_string_nodef((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_thread_seq", per_thread_sw, PATH_MAX); + if(cycle_pkt_dump_parse_per_thread_switch(per_thread_sw) < 0){ + return 0; + } + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_total_size", &total_files_size, 1000); + if(total_files_size < 1 || (unsigned int)total_files_size >= 1024*100 /* 100GB */){ + printf("pkt_dump_total_size invalid!\n"); + return -1; + } + + MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_file_max_size", &int_tmp, 100); + if(int_tmp < 2 || (unsigned int)int_tmp >= 1024*100 /* 100GB */){ + printf("pkt_dump_file_max_size invalid!\n"); + return -1; + } + /* ÿ߳Ҫѭд2ļ, Ҫpkt_dump_file_max_sizeʵֵٳ2 */ + if(cycle_pkt_dump_set_file_size((long)total_files_size*1024*1024, (long)int_tmp*1024*512) < 0){ + return 0; + } + + signal(SIGPIPE, SIG_IGN); + /* SIGSEGVź, ֤쳣ֹʱ, һBUGpcapдӲ */ + if((SIG_ERR == signal(SIGSEGV, pkt_dump_sig_handle))){ + printf("Catch SIGSEGV error!\n"); + } + +#if IKNOW_ENABLE + iknow_register_cmd(PLATFORM_IHANDLE, "show pkt dump summary", "display pkt dump summary", + ifuncb_show_pktdump_summary, NULL, 0, NULL); +#endif + printf("\n\033[33m[Warning]pkt dump enable, this maybe encumber performance.\033[0m\n"); + sleep(1); + }else{ + ret = cycle_pkt_dump_socket_init(); + } + + return ret; +} + + +#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 0d2d51b..2262154 100644 --- a/src/packet_io/cycle_pkt_dump_through_write_offset.c +++ b/src/packet_io/cycle_pkt_dump_through_write_offset.c @@ -44,7 +44,7 @@ static pkt_dump_status_t PKT_DISCARD_STAT[MAX_THREAD_NUM]; #endif -const int tcpdump_mesa_version_VERSION_20181114 = 20181114; +const int tcpdump_mesa_version_VERSION_20190820 = 20190820; extern int g_use_MESA_sleep_sw; extern void MESA_sleep(void); @@ -64,6 +64,7 @@ static FILE *G_PKT_DUMP_FP[MAX_THREAD_NUM]; static int G_FILENAME_INDEX[MAX_THREAD_NUM]; static long G_PCAP_SIZE[MAX_THREAD_NUM]; static char G_PKT_DUMP_FILTER_STR[PATH_MAX]; + static struct bpf_program G_PKT_DUMP_BPF_FILTER[MAX_THREAD_NUM]; static pcap_t *phony_pcap_handle[MAX_THREAD_NUM]; /* BPFʱʹ */ static unsigned short pkt_dump_data_offset = 0; /* ʱָƫ, vxlan²ڲݰ */ @@ -421,6 +422,7 @@ static void __do_cycle_pkt_dump_udp_socket(int thread_seq, const raw_pkt_t *p_ra int pktlen, for_bpf_filter_len, ret; char modify_pkt_buf[2048]; /* Ҫ޸ԭʼ, copyʱ */ struct perceptive_info *pperceptive_info; + const sapp_config_tools_pktdump_t *pktdump_cfg = &sapp_global_val->config.tools.pkt_dump; if(0 == pkt_dump_udp_socket_peer_alive){ return; @@ -519,6 +521,7 @@ static void __do_cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt) pkt_dump_pkt_hdr_t pcap_hdr[2]; int write_hdr_len; char dump_file_name[PATH_MAX]; + const sapp_config_tools_pktdump_t *pktdump_cfg = &sapp_global_val->config.tools.pkt_dump; if(PKT_DUMP_LOCAL_FILE == G_PKT_DUMP_MODE){ if(0 == G_PKT_DUMP_PER_THREAD_SW[thread_seq]){ @@ -529,7 +532,7 @@ static void __do_cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt) return; } - if(G_PKT_DUMP_FILTER_STR[0] != '\0'){ + if(pktdump_cfg->bpf_filter[0] != '\0'){ if(g_packet_io_cap_level != (enum addr_type_t)CAP_LEVEL_MAC){ if(0 == bpf_filter(G_PKT_DUMP_BPF_FILTER[thread_seq].bf_insns, (const unsigned char *)p_raw_pkt->raw_pkt_data-14, @@ -547,7 +550,7 @@ static void __do_cycle_pkt_dump(int thread_seq, const raw_pkt_t *p_raw_pkt) if(unlikely(NULL == G_PKT_DUMP_FP[thread_seq])){ snprintf(dump_file_name, PATH_MAX, "%s/pid_%d.tid_%d.%d.pcap", - G_PKT_DUMP_ROOT_DIR, getpid(), thread_seq, G_FILENAME_INDEX[thread_seq]); + pktdump_cfg->storge_path, getpid(), thread_seq, G_FILENAME_INDEX[thread_seq]); G_PKT_DUMP_FP[thread_seq] = fopen(dump_file_name, "wb+"); if(NULL == G_PKT_DUMP_FP[thread_seq]){ printf("fopen %s error!\n", dump_file_name); @@ -814,6 +817,7 @@ static int cycle_pkt_dump_get_core_num(void) return core_num; } +#if 0 /* config is like: "0,1,2,3,4,5" */ static int cycle_pkt_dump_parse_per_thread_switch(char *config_str) { @@ -841,6 +845,7 @@ static int cycle_pkt_dump_parse_per_thread_switch(char *config_str) return 0; } +#endif static long cycle_pkt_dump_get_cur_files_size(void) @@ -869,9 +874,10 @@ static long cycle_pkt_dump_get_cur_files_size(void) static int cycle_pkt_dump_get_cur_enable_thread(void) { int i, sum = 0; + const sapp_config_tools_pktdump_t *pktdump_cfg = &sapp_global_val->config.tools.pkt_dump; for(i = 0; i < MAX_THREAD_NUM; i++){ - if(G_PKT_DUMP_PER_THREAD_SW[i] > 0){ + if(G_PKT_DUMP_PER_THREAD_SW != 0){ sum++; } } @@ -932,15 +938,20 @@ static void pkt_dump_sig_handle(int sig) static void pkt_dump_set_default_opt(void) { + int i; + const sapp_config_tools_pktdump_t *pktdump_cfg = &sapp_global_val->config.tools.pkt_dump; udp_recv_addr.sin_family = AF_INET; udp_recv_addr.sin_addr.s_addr = htonl(0x7f000001); udp_recv_addr.sin_port = htons(12345); - - memset(G_PKT_DUMP_FILTER_STR, 0, sizeof(G_PKT_DUMP_FILTER_STR)); - pkt_dump_data_offset = 0; - /* Ĭ߳ȫ */ - memset(G_PKT_DUMP_PER_THREAD_SW, 1, sizeof(G_PKT_DUMP_PER_THREAD_SW)); + /* ԭʼøһ, Ϊudpģʽ, ܻ-kıֵ */ + for(i = 0; i < MAX_THREAD_NUM; i++){ + G_PKT_DUMP_PER_THREAD_SW[i] = pktdump_cfg->dump_thread_id_mask[i]; + } + + if(pktdump_cfg->bpf_filter != NULL){ + strncpy(G_PKT_DUMP_FILTER_STR, pktdump_cfg->bpf_filter, sizeof(G_PKT_DUMP_FILTER_STR)-1); + } } static void pkt_dump_parse_opt_rcv_port(const struct pkt_dump_opt *cmd_opt, const char *opt_val) @@ -1032,10 +1043,12 @@ static void cycle_pkt_dump_command_parse(int connfd) } /* ݲϸ޶Զtcpdump_mesa汾, ֻӡϢ */ +#if 0 if(ntohl(pkt_hdr.version) != (unsigned int)tcpdump_mesa_version_VERSION_20181114){ printf("Remote tcpdump_mesa version is incompatible!\n"); sapp_runtime_log(20, "Remote tcpdump_mesa version is incompatible!\n"); } +#endif pkt_dump_set_default_opt(); @@ -1210,71 +1223,50 @@ int cycle_pkt_dump_init(int argc, char *argv[]) int int_tmp, total_files_size, core_num; char per_thread_sw[PATH_MAX]; char pcap_errbuf[PCAP_ERRBUF_SIZE]; + const sapp_config_tools_pktdump_t *pktdump_cfg = &sapp_global_val->config.tools.pkt_dump; - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_switch", &G_PKT_DUMP_SW, 0); - - if(0 == G_PKT_DUMP_SW){ + if(0 == pktdump_cfg->enabled){ return 0; } - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_mode", &G_PKT_DUMP_MODE, 1); - if((G_PKT_DUMP_MODE != PKT_DUMP_LOCAL_FILE) && (G_PKT_DUMP_MODE != PKT_DUMP_UDP_SOCKET)){ - printf("\n\033[41mError! pkt_dump_mode only support 1 or 2!\033[0m\n"); - return -1; - } + G_PKT_DUMP_SW = pktdump_cfg->enabled; + pkt_dump_tcp_bind_port = (unsigned short)pktdump_cfg->command_port; + cur_conn_pkt_dump_ratio = pktdump_cfg->pkt_dump_ratio; + cur_conn_pkt_dump_ratio = pktdump_cfg->pkt_dump_ratio; + G_PKT_DUMP_MODE = pktdump_cfg->mode_bin; - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_cmd_port", &int_tmp, 12345); - if((int_tmp <=0) || (int_tmp > 65535)){ - printf("\n\033[41mError! pkt_dump_cmd_port invalid, %d!\033[0m\n", int_tmp); - return -1; + /* ԭʼøһ, Ϊudpģʽ, ܻ-kıֵ */ + for(i = 0; i < MAX_THREAD_NUM; i++){ + G_PKT_DUMP_PER_THREAD_SW[i] = pktdump_cfg->dump_thread_id_mask[i]; + } + if(pktdump_cfg->bpf_filter != NULL){ + strncpy(G_PKT_DUMP_FILTER_STR, pktdump_cfg->bpf_filter, sizeof(G_PKT_DUMP_FILTER_STR)-1); } - pkt_dump_tcp_bind_port = (unsigned short)int_tmp; - - - MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_ratio", &raw_cfg_pkt_dump_ratio, 30); - cur_conn_pkt_dump_ratio = raw_cfg_pkt_dump_ratio; - -#if 0 == CYCLE_PKT_DUMP - printf("\n\033[33m[Warning]pkt_dump is enable, but sapp is not compile correct, please use debug=2.\033[0m\n"); - sleep(1); -#endif - if(PKT_DUMP_LOCAL_FILE == G_PKT_DUMP_MODE){ cycle_pkt_dump_set_thread_num(g_packet_io_thread_num); - core_num = cycle_pkt_dump_get_core_num(); - if(core_num < 0 || core_num > 10){ - printf("\033[33m[Warning]core.* number is more than 10, pkt dump is auto disabled!\n"); - G_PKT_DUMP_SW = 0; - return 0; - } - - MESA_load_profile_string_def((char *)"conf/main.conf",(char *)"pkt_dump", (char *)"pkt_dump_bpf_filter", G_PKT_DUMP_FILTER_STR, PATH_MAX, (char *)""); - if(G_PKT_DUMP_FILTER_STR[0] != '\0'){ + if(pktdump_cfg->bpf_filter[0] != '\0'){ for(i = 0; i < g_packet_io_thread_num; i++){ phony_pcap_handle[i] = pcap_open_dead(DLT_EN10MB, 65535); if(NULL == phony_pcap_handle[i]){ - printf("pcap_open %s error, %s\n", "lo", pcap_errbuf); - printf("\n\033[41mpkt_dump_bpf_filter is not enable!\033[0m\n"); - goto skip_bpf_filter; + sapp_log(30, 1, 1, "pcap_open %s error, %s\n", "lo", pcap_errbuf); + sapp_log(30, 1, 1, "pkt_dump_bpf_filter is not enable!\n"); + return -1; } - if(pcap_compile(phony_pcap_handle[i], &G_PKT_DUMP_BPF_FILTER[i], G_PKT_DUMP_FILTER_STR, 100, 0) < 0){ - printf("Compile pcap filter '%s' error:%s\n", G_PKT_DUMP_FILTER_STR, pcap_geterr(phony_pcap_handle[i])); + if(pcap_compile(phony_pcap_handle[i], &G_PKT_DUMP_BPF_FILTER[i], pktdump_cfg->bpf_filter, 100, 0) < 0){ + sapp_log(30, 1, 1, "Compile pcap filter '%s' error:%s\n", pktdump_cfg->bpf_filter, pcap_geterr(phony_pcap_handle[i])); return -1; } } } - skip_bpf_filter: - - MESA_load_profile_string_def((char *)"conf/main.conf",(char *)"pkt_dump", (char *)"pkt_dump_file_root_dir", G_PKT_DUMP_ROOT_DIR, PATH_MAX, (char *)"/tmp/pkt_dump"); - MESA_mkdir_p(G_PKT_DUMP_ROOT_DIR, 0755); - - MESA_load_profile_string_nodef((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_thread_seq", per_thread_sw, PATH_MAX); - if(cycle_pkt_dump_parse_per_thread_switch(per_thread_sw) < 0){ - return 0; + if(MESA_mkdir_p(pktdump_cfg->storge_path, 0755) < 0){ + sapp_log(30, 1, 1, "tools.pkt_dump.storage mkdir '%s' error!\n", pktdump_cfg->storge_path); + return -1; } - + + G_PKT_DUMP_FILE_SIZE = pktdump_cfg->file_size_max_per_thread * 1024 * 1024 ; +#if 0 MESA_load_profile_int_def((char *)"conf/main.conf", (char *)"pkt_dump", (char *)"pkt_dump_total_size", &total_files_size, 1000); if(total_files_size < 1 || (unsigned int)total_files_size >= 1024*1000 /* 1000GB */){ printf("pkt_dump_total_size invalid!\n"); @@ -1290,19 +1282,14 @@ int cycle_pkt_dump_init(int argc, char *argv[]) if(cycle_pkt_dump_set_file_size((long)total_files_size*1024*1024, (long)int_tmp*1024*512) < 0){ return 0; } - +#endif signal(SIGPIPE, SIG_IGN); /* SIGSEGVź, ֤쳣ֹʱ, һBUGpcapдӲ */ if((SIG_ERR == signal(SIGSEGV, pkt_dump_sig_handle))){ printf("Catch SIGSEGV error!\n"); } -#if IKNOW_ENABLE - iknow_register_cmd(PLATFORM_IHANDLE, "show pkt dump summary", "display pkt dump summary", - ifuncb_show_pktdump_summary, NULL, 0, NULL); -#endif - printf("\n\033[33m[Warning]pkt dump enable, this maybe encumber performance.\033[0m\n"); - sleep(1); + sapp_log(20, 1, 1, "tools.pkt_dump enable, this maybe encumber performance!\n"); }else{ ret = cycle_pkt_dump_socket_init(); } diff --git a/src/packet_io/packet_io.c b/src/packet_io/packet_io.c index 03bdfd0..5df71e2 100644 --- a/src/packet_io/packet_io.c +++ b/src/packet_io/packet_io.c @@ -705,7 +705,8 @@ int packet_io_init(int argc, char *argv[]) printf("Warning, can't get %s mac addr!\n", g_send_dev_name); } }else{ - printf("Warning, not assign send device!\n"); + //printf("Warning, not assign send device!\n"); + ; } parse_send_gdev_ip_conf("conf/send_gdev.conf"); diff --git a/src/packet_io/packet_io_marsio.c b/src/packet_io/packet_io_marsio.c index be43cba..c7def66 100644 --- a/src/packet_io/packet_io_marsio.c +++ b/src/packet_io/packet_io_marsio.c @@ -26,8 +26,8 @@ typedef struct{ struct mr_sendpath *sendpath_up, *sendpath_down; /* ģʽ, ֱʹsendpath_up; ͬʱ˫ģʽ */ }marsio4_io_handle; -int g_marsio4_version_VERSION_20190528; -int g_marsio4_version_VERSION = 20190528; +int g_marsio4_version_VERSION_20190820; +int g_marsio4_version_VERSION = 20190820; static PACKET_IO_CB_T g_marsio4_work_fun; static int g_marsio4_work_thread_num = 1; static int g_marsio4_cap_level = CAP_LEVEL_MAC; @@ -596,7 +596,7 @@ static int marsio4_process_packet(int tid, raw_pkt_t *raw_pkt) static int marsio4_sleep_time_table[100] = { 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -629,7 +629,7 @@ static void *marsio4_worker(void *arg) { int tid = *((int *)arg); raw_pkt_t raw_pkt; - int total_call_times = 0, rcv_pkt_tims = 0; + int total_call_times = 0, rcv_pkt_tims = 0, polling_work_times = 0; char thread_name[32] = ""; sprintf(thread_name, "sapp_marsio_%d", tid); @@ -652,17 +652,31 @@ static void *marsio4_worker(void *arg) while(1){ if(marsio4_process_packet(tid, &raw_pkt) > 0){ rcv_pkt_tims++; + }else{ + /* ްʱҪpollingӿ */ + if(stream_process_polling(tid) > 0){ + polling_work_times++; + } } + total_call_times++; - stream_process_polling(tid); - if(total_call_times >= 100){ - if(rcv_pkt_tims < 99){ - marsio4_sleep(total_call_times, rcv_pkt_tims); + if((rcv_pkt_tims % sapp_global_val->config.packet_io.polling_priority) == 0){ + /* ޴, һֱаʱ, ÿpolling_priorityҲҪpollingӿ */ + if(stream_process_polling(tid) > 0){ + polling_work_times++; } + total_call_times++; + } + + if(total_call_times >= 100){ + marsio4_sleep(total_call_times, rcv_pkt_tims + polling_work_times); total_call_times = 0; rcv_pkt_tims = 0; + polling_work_times = 0; } + + } return NULL; diff --git a/src/packet_io/packet_io_pcap.c b/src/packet_io/packet_io_pcap.c index a9e8aa0..ba8d429 100644 --- a/src/packet_io/packet_io_pcap.c +++ b/src/packet_io/packet_io_pcap.c @@ -9,8 +9,8 @@ extern "C" { #define PROCESS_BAR_SW (1) -int g_pcap_version_VERSION_20190524; -static int g_pcap_version_internal = 20190524; +int g_pcap_version_VERSION_20190820; +static int g_pcap_version_internal = 20190820; #define PCAP_SNAPLEN_MAX (65535) @@ -740,6 +740,7 @@ static void *__pcap_work_thread(void *arg) pkt_queue_t pkt_queue_node; long recv_len; int ret; + int total_call_times = 0, rcv_pkt_tims = 0, polling_work_times = 0; #if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 480) /* 汾ж, ܶϵͳ޷ */ #ifdef _GNU_SOURCE @@ -753,23 +754,21 @@ static void *__pcap_work_thread(void *arg) } while(1){ - pthread_mutex_lock(&work_thread_pool[thread_num].mutex); - -#if 0/* for sapp timer */ - while(0 == MESA_lqueue_get_count(work_thread_pool[thread_num].pkt_queue)){ - pthread_cond_wait(&work_thread_pool[thread_num].cond, &work_thread_pool[thread_num].mutex); - } -#else if(0 == MESA_lqueue_get_count(work_thread_pool[thread_num].pkt_queue)){ - pthread_mutex_unlock(&work_thread_pool[thread_num].mutex); - stream_process_polling(thread_num); - usleep(10); - continue; - }else{ - stream_process_polling(thread_num); - } + if(stream_process_polling(thread_num) != 0){ + polling_work_times++; + } + total_call_times++; -#endif + if(total_call_times >= 100){ + total_call_times = 0; + rcv_pkt_tims = 0; + polling_work_times = 0; + usleep(1); + } + continue; + } + pthread_mutex_lock(&work_thread_pool[thread_num].mutex); recv_len = sizeof(pkt_queue_t); ret = MESA_lqueue_get_head(work_thread_pool[thread_num].pkt_queue, &pkt_queue_node, &recv_len); pthread_mutex_unlock(&work_thread_pool[thread_num].mutex); @@ -777,6 +776,20 @@ static void *__pcap_work_thread(void *arg) continue; } + total_call_times++; + if(total_call_times >= sapp_global_val->config.packet_io.polling_priority){ + /* ޴, һֱаʱ, ÿpolling_priorityҲҪpollingӿ */ + if(stream_process_polling(thread_num) > 0){ + polling_work_times++; + } + } + if(total_call_times >= 100){ + total_call_times = 0; + rcv_pkt_tims = 0; + polling_work_times = 0; + usleep(1); + } + if(PCAP_OP_FLAG_IDLE_POLLING == pkt_queue_node.op_flag){ idle_polling_call(thread_num); continue; diff --git a/src/packet_io/packet_io_status.cpp b/src/packet_io/packet_io_status.cpp index 1188bcd..3afab4c 100644 --- a/src/packet_io/packet_io_status.cpp +++ b/src/packet_io/packet_io_status.cpp @@ -72,7 +72,7 @@ void line_protocol_update_metrics(void) //} /* line_protocolsysinfo.logܲһ, ͳһ˲ʱֵ, ˴бʷֵ, õǰֵ */ - buf_len = snprintf(metrics_buf, 65000, "protocol,type=raw Eth_Pkt=%llu,Eth_Bit=%llu,Ipv4_Pkt=%llu,Ipv4_Bit=%llu,Ipv6_Pkt=%llu,Ipv6_Bit=%llu,Tcp_Pkt=%llu,Tcp_Bit=%llu,Udp_Pkt=%llu,Udp_Bit=%llu\n", + buf_len = snprintf(metrics_buf, 65000, "protocol,flow_type=inline Eth_Pkt=%llu,Eth_Bit=%llu,Ipv4_Pkt=%llu,Ipv4_Bit=%llu,Ipv6_Pkt=%llu,Ipv6_Bit=%llu,Tcp_Pkt=%llu,Tcp_Bit=%llu,Udp_Pkt=%llu,Udp_Bit=%llu\n", cur_count[SAPP_STAT_RCV_ETHERNET] - lp_handle->history_count[SAPP_STAT_RCV_ETHERNET], 8 *( cur_length[SAPP_STAT_RCV_ETHERNET]) - 8 * lp_handle->history_length[SAPP_STAT_RCV_ETHERNET], cur_count[SAPP_STAT_RCV_IPV4] - lp_handle->history_count[SAPP_STAT_RCV_IPV4], @@ -91,7 +91,7 @@ void line_protocol_update_metrics(void) Tcp_ConcurrentֵǶμõ, ʾǰʱ˲ʱֵ, ûʱ, ֱдInfluxDB, ʾʱҪʹMEAN, ƽֵʾ. */ - buf_len = snprintf(metrics_buf, 65000, "stream,type=raw Tcp_Concurrent=%llu,Tcp_Link_New=%llu,Tcp_Link_Del=%llu,Tcp_Link_Double=%llu,Tcp_Link_C2S=%llu,Tcp_Link_S2C=%llu\n", + buf_len = snprintf(metrics_buf, 65000, "stream,flow_type=inline Tcp_Concurrent=%llu,Tcp_Link_New=%llu,Tcp_Link_Del=%llu,Tcp_Link_Double=%llu,Tcp_Link_C2S=%llu,Tcp_Link_S2C=%llu\n", cur_count[SAPP_STAT_TCP_STREAM_DATA], (cur_count[SAPP_STAT_TCP_STREAM_NEW] - lp_handle->history_count[SAPP_STAT_TCP_STREAM_NEW])/sapp_global_val->config.profiling.log.interval, (cur_count[SAPP_STAT_TCP_STREAM_DEL] - lp_handle->history_count[SAPP_STAT_TCP_STREAM_DEL])/sapp_global_val->config.profiling.log.interval, @@ -184,13 +184,16 @@ void sysinfo_output(void) fprintf(fp,"\n*********************** %s *********************\n", time_string); fprintf(fp,"*********************** \033[41m%s\033[0m *************************\n", "sysinfo exactly"); - fprintf(fp,"%-10s %12s %12s %12s %12s\n","RCV-PKT", "total_pkt", "total_len","pps", "Bps"); - fprintf(fp,"%-10s %12llu %12llu %12llu %12llu\n", "Ethernet", cur_count[SAPP_STAT_RCV_ETHERNET],cur_length[SAPP_STAT_RCV_ETHERNET],(cur_count[SAPP_STAT_RCV_ETHERNET]-history_count[SAPP_STAT_RCV_ETHERNET]),(cur_length[SAPP_STAT_RCV_ETHERNET]-history_length[SAPP_STAT_RCV_ETHERNET])); - fprintf(fp,"%-10s %12llu %12llu %12llu %12llu\n", "IPv4", cur_count[SAPP_STAT_RCV_IPV4],cur_length[SAPP_STAT_RCV_IPV4],(cur_count[SAPP_STAT_RCV_IPV4]-history_count[SAPP_STAT_RCV_IPV4]),(cur_length[SAPP_STAT_RCV_IPV4]-history_length[SAPP_STAT_RCV_IPV4])); - fprintf(fp,"%-10s %12llu %12llu %12llu %12llu\n","IPv6",cur_count[SAPP_STAT_RCV_IPV6],cur_length[SAPP_STAT_RCV_IPV6],(cur_count[SAPP_STAT_RCV_IPV6]-history_count[SAPP_STAT_RCV_IPV6]),(cur_length[SAPP_STAT_RCV_IPV6]-history_length[SAPP_STAT_RCV_IPV6])); - fprintf(fp,"%-10s %12llu %12llu %12llu %12llu\n","TCP",cur_count[SAPP_STAT_RCV_TCP],cur_length[SAPP_STAT_RCV_TCP],(cur_count[SAPP_STAT_RCV_TCP]-history_count[SAPP_STAT_RCV_TCP]),(cur_length[SAPP_STAT_RCV_TCP]-history_length[SAPP_STAT_RCV_TCP])); - fprintf(fp,"%-10s %12llu %12llu %12llu %12llu\n","UDP",cur_count[SAPP_STAT_RCV_UDP],cur_length[SAPP_STAT_RCV_UDP],(cur_count[SAPP_STAT_RCV_UDP]-history_count[SAPP_STAT_RCV_UDP]),(cur_length[SAPP_STAT_RCV_UDP]-history_length[SAPP_STAT_RCV_UDP])); - fprintf(fp,"%-10s %12llu %12llu %12llu %12llu\n","Unknown",cur_count[SAPP_STAT_RCV_UNKNOWN],cur_length[SAPP_STAT_RCV_UNKNOWN],(cur_count[SAPP_STAT_RCV_UNKNOWN]-history_count[SAPP_STAT_RCV_UNKNOWN]),(cur_length[SAPP_STAT_RCV_UNKNOWN]-history_length[SAPP_STAT_RCV_UNKNOWN])); + fprintf(fp,"%-12s %12s %12s %12s %12s\n","RCV-PKT", "total_pkt", "total_len","pps", "Bps"); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n", "Polling_work", 0,0,(cur_count[SAPP_POLLING_WORK]-history_count[SAPP_POLLING_WORK]),0); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n", "Polling_idle", 0,0,(cur_count[SAPP_POLLING_IDLE]-history_count[SAPP_POLLING_IDLE]),0); + + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n", "Ethernet", cur_count[SAPP_STAT_RCV_ETHERNET],cur_length[SAPP_STAT_RCV_ETHERNET],(cur_count[SAPP_STAT_RCV_ETHERNET]-history_count[SAPP_STAT_RCV_ETHERNET]),(cur_length[SAPP_STAT_RCV_ETHERNET]-history_length[SAPP_STAT_RCV_ETHERNET])); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n", "IPv4", cur_count[SAPP_STAT_RCV_IPV4],cur_length[SAPP_STAT_RCV_IPV4],(cur_count[SAPP_STAT_RCV_IPV4]-history_count[SAPP_STAT_RCV_IPV4]),(cur_length[SAPP_STAT_RCV_IPV4]-history_length[SAPP_STAT_RCV_IPV4])); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n","IPv6",cur_count[SAPP_STAT_RCV_IPV6],cur_length[SAPP_STAT_RCV_IPV6],(cur_count[SAPP_STAT_RCV_IPV6]-history_count[SAPP_STAT_RCV_IPV6]),(cur_length[SAPP_STAT_RCV_IPV6]-history_length[SAPP_STAT_RCV_IPV6])); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n","TCP",cur_count[SAPP_STAT_RCV_TCP],cur_length[SAPP_STAT_RCV_TCP],(cur_count[SAPP_STAT_RCV_TCP]-history_count[SAPP_STAT_RCV_TCP]),(cur_length[SAPP_STAT_RCV_TCP]-history_length[SAPP_STAT_RCV_TCP])); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n","UDP",cur_count[SAPP_STAT_RCV_UDP],cur_length[SAPP_STAT_RCV_UDP],(cur_count[SAPP_STAT_RCV_UDP]-history_count[SAPP_STAT_RCV_UDP]),(cur_length[SAPP_STAT_RCV_UDP]-history_length[SAPP_STAT_RCV_UDP])); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n","Unknown",cur_count[SAPP_STAT_RCV_UNKNOWN],cur_length[SAPP_STAT_RCV_UNKNOWN],(cur_count[SAPP_STAT_RCV_UNKNOWN]-history_count[SAPP_STAT_RCV_UNKNOWN]),(cur_length[SAPP_STAT_RCV_UNKNOWN]-history_length[SAPP_STAT_RCV_UNKNOWN])); fprintf(fp,"\n***************************************************************\n"); fprintf(fp,"%-10s %12s %12s %12s %12s\n","SND-PKT", "total_pkt", "total_len","pps", "Bps"); fprintf(fp,"%-10s %12llu %12llu %12llu %12llu\n", "TCP_RST", cur_count[SAPP_STAT_SND_TCP_RST],cur_length[SAPP_STAT_SND_TCP_RST],(cur_count[SAPP_STAT_SND_TCP_RST]-history_count[SAPP_STAT_SND_TCP_RST]),(cur_length[SAPP_STAT_SND_TCP_RST]-history_length[SAPP_STAT_SND_TCP_RST])); @@ -277,13 +280,16 @@ void sysinfo_output(void) fprintf(fp,"*****************************************************************\n\n"); fprintf(fp,"################# \033[41m%s\033[0m --- is same as above! #########\n", "sysinfo humanly"); - fprintf(fp,"%-10s %12s %12s %12s %12s\n", "RCV-PKT", "total_pkt", "total_len","pps", "bps"); - fprintf(fp,"%-10s %12llu %12s %12llu %12s\n","Ethernet",cur_count[SAPP_STAT_RCV_ETHERNET],byte_convert_human(cur_length[SAPP_STAT_RCV_ETHERNET],1 ,1,s1),(cur_count[SAPP_STAT_RCV_ETHERNET]-history_count[SAPP_STAT_RCV_ETHERNET]),byte_convert_human((cur_length[SAPP_STAT_RCV_ETHERNET]-history_length[SAPP_STAT_RCV_ETHERNET]),1,8,s2)); - fprintf(fp,"%-10s %12llu %12s %12llu %12s\n","IPv4",cur_count[SAPP_STAT_RCV_IPV4],byte_convert_human(cur_length[SAPP_STAT_RCV_IPV4],1 ,1,s1),(cur_count[SAPP_STAT_RCV_IPV4]-history_count[SAPP_STAT_RCV_IPV4]),byte_convert_human((cur_length[SAPP_STAT_RCV_IPV4]-history_length[SAPP_STAT_RCV_IPV4]),1,8,s2)); - fprintf(fp,"%-10s %12llu %12s %12llu %12s\n","IPv6",cur_count[SAPP_STAT_RCV_IPV6],byte_convert_human(cur_length[SAPP_STAT_RCV_IPV6],1,1,s1),(cur_count[SAPP_STAT_RCV_IPV6]-history_count[SAPP_STAT_RCV_IPV6]),byte_convert_human((cur_length[SAPP_STAT_RCV_IPV6]-history_length[SAPP_STAT_RCV_IPV6]),1,8,s2)); - fprintf(fp,"%-10s %12llu %12s %12llu %12s\n","TCP",cur_count[SAPP_STAT_RCV_TCP],byte_convert_human(cur_length[SAPP_STAT_RCV_TCP],1,1,s1),(cur_count[SAPP_STAT_RCV_TCP]-history_count[SAPP_STAT_RCV_TCP]),byte_convert_human((cur_length[SAPP_STAT_RCV_TCP]-history_length[SAPP_STAT_RCV_TCP]),1,8,s2)); - fprintf(fp,"%-10s %12llu %12s %12llu %12s\n","UDP",cur_count[SAPP_STAT_RCV_UDP],byte_convert_human(cur_length[SAPP_STAT_RCV_UDP],1,1,s1),(cur_count[SAPP_STAT_RCV_UDP]-history_count[SAPP_STAT_RCV_UDP]),byte_convert_human((cur_length[SAPP_STAT_RCV_UDP]-history_length[SAPP_STAT_RCV_UDP]),1,8,s2)); - fprintf(fp,"%-10s %12llu %12s %12llu %12s\n","Unknown",cur_count[SAPP_STAT_RCV_UNKNOWN],byte_convert_human(cur_length[SAPP_STAT_RCV_UNKNOWN],1,1,s1),(cur_count[SAPP_STAT_RCV_UNKNOWN]-history_count[SAPP_STAT_RCV_UNKNOWN]),byte_convert_human((cur_length[SAPP_STAT_RCV_UNKNOWN]-history_length[SAPP_STAT_RCV_UNKNOWN]),1,8,s2)); + fprintf(fp,"%-12s %12s %12s %12s %12s\n", "RCV-PKT", "total_pkt", "total_len","pps", "bps"); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n", "Polling_work", 0,0,(cur_count[SAPP_POLLING_WORK]-history_count[SAPP_POLLING_WORK]),0); + fprintf(fp,"%-12s %12llu %12llu %12llu %12llu\n", "Polling_idle", 0,0,(cur_count[SAPP_POLLING_IDLE]-history_count[SAPP_POLLING_IDLE]),0); + + fprintf(fp,"%-12s %12llu %12s %12llu %12s\n","Ethernet",cur_count[SAPP_STAT_RCV_ETHERNET],byte_convert_human(cur_length[SAPP_STAT_RCV_ETHERNET],1 ,1,s1),(cur_count[SAPP_STAT_RCV_ETHERNET]-history_count[SAPP_STAT_RCV_ETHERNET]),byte_convert_human((cur_length[SAPP_STAT_RCV_ETHERNET]-history_length[SAPP_STAT_RCV_ETHERNET]),1,8,s2)); + fprintf(fp,"%-12s %12llu %12s %12llu %12s\n","IPv4",cur_count[SAPP_STAT_RCV_IPV4],byte_convert_human(cur_length[SAPP_STAT_RCV_IPV4],1 ,1,s1),(cur_count[SAPP_STAT_RCV_IPV4]-history_count[SAPP_STAT_RCV_IPV4]),byte_convert_human((cur_length[SAPP_STAT_RCV_IPV4]-history_length[SAPP_STAT_RCV_IPV4]),1,8,s2)); + fprintf(fp,"%-12s %12llu %12s %12llu %12s\n","IPv6",cur_count[SAPP_STAT_RCV_IPV6],byte_convert_human(cur_length[SAPP_STAT_RCV_IPV6],1,1,s1),(cur_count[SAPP_STAT_RCV_IPV6]-history_count[SAPP_STAT_RCV_IPV6]),byte_convert_human((cur_length[SAPP_STAT_RCV_IPV6]-history_length[SAPP_STAT_RCV_IPV6]),1,8,s2)); + fprintf(fp,"%-12s %12llu %12s %12llu %12s\n","TCP",cur_count[SAPP_STAT_RCV_TCP],byte_convert_human(cur_length[SAPP_STAT_RCV_TCP],1,1,s1),(cur_count[SAPP_STAT_RCV_TCP]-history_count[SAPP_STAT_RCV_TCP]),byte_convert_human((cur_length[SAPP_STAT_RCV_TCP]-history_length[SAPP_STAT_RCV_TCP]),1,8,s2)); + fprintf(fp,"%-12s %12llu %12s %12llu %12s\n","UDP",cur_count[SAPP_STAT_RCV_UDP],byte_convert_human(cur_length[SAPP_STAT_RCV_UDP],1,1,s1),(cur_count[SAPP_STAT_RCV_UDP]-history_count[SAPP_STAT_RCV_UDP]),byte_convert_human((cur_length[SAPP_STAT_RCV_UDP]-history_length[SAPP_STAT_RCV_UDP]),1,8,s2)); + fprintf(fp,"%-12s %12llu %12s %12llu %12s\n","Unknown",cur_count[SAPP_STAT_RCV_UNKNOWN],byte_convert_human(cur_length[SAPP_STAT_RCV_UNKNOWN],1,1,s1),(cur_count[SAPP_STAT_RCV_UNKNOWN]-history_count[SAPP_STAT_RCV_UNKNOWN]),byte_convert_human((cur_length[SAPP_STAT_RCV_UNKNOWN]-history_length[SAPP_STAT_RCV_UNKNOWN]),1,8,s2)); fprintf(fp,"#################################################################\n"); diff --git a/src/packet_io/sapp_inject.c b/src/packet_io/sapp_inject.c new file mode 100644 index 0000000..3f2f3a9 --- /dev/null +++ b/src/packet_io/sapp_inject.c @@ -0,0 +1,55 @@ +#include "sapp_api.h" +#include "sapp_private_api.h" +#include "stream_inc/sapp_inject.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/* + MESA_kill_tcpʵȹõǰIP, TCPͷ, + ҪָϢ, Ҳдһassistantģ, ôipttl, ipid, tcpwinֵ. + Ȼ__sapp_inject_pkt(), sio = SIO_EXCLUDE_THIS_LAYER_HDR. + + sappֱӸijͷֶôֵ, assistantģ鸺, sappհ. + + kill_remedyҲǿassistantݵǰseq, rstseq, Ƶfdɹ, Ҫ׷Ӷ. + assistant. +*/ + + +extern int __do_inject_pkt(struct streaminfo *stream, const char *payload, int payload_len, const void *ext_raw_pkt, UCHAR snd_routedir, char *feedback_buf, int *feedback_buf_len); + + +int __sapp_inject_pkt(struct streaminfo *stream, enum sapp_inject_opt sio, const void *payload, int payload_len, unsigned char snd_routedir, void *user_arg) +{ + int ret = -1; + + switch((int)sio){ + case SIO_DEFAULT: + ret = __do_inject_pkt(stream, (const char *)payload, payload_len, NULL, snd_routedir, (char *)KILL_TCP_PHONY_POINTER, (int *)KILL_TCP_PHONY_POINTER); + break; + + case SIO_EXCLUDE_THIS_LAYER_HDR: + ret = __do_inject_pkt(stream->pfather, (const char *)payload, payload_len, NULL, snd_routedir, (char *)KILL_TCP_PHONY_POINTER, (int *)KILL_TCP_PHONY_POINTER); + break; + + + default: + break; + } + + return ret; +} + +int sapp_inject_pkt(struct streaminfo *stream, enum sapp_inject_opt sio, const void *payload, int payload_len, unsigned char snd_routedir) +{ + return __sapp_inject_pkt(stream, sio, payload, payload_len, snd_routedir, NULL); +} + + +#ifdef __cplusplus +} +#endif + diff --git a/src/packet_io/sendpacket.c b/src/packet_io/sendpacket.c index a74b844..3824e19 100644 --- a/src/packet_io/sendpacket.c +++ b/src/packet_io/sendpacket.c @@ -2528,7 +2528,7 @@ int MESA_kill_connection_feedback(struct streaminfo *stream, const void *ext_raw return ret; } -static int __do_inject_pkt(struct streaminfo *stream, const char *payload, int payload_len, +int __do_inject_pkt(struct streaminfo *stream, const char *payload, int payload_len, const void *ext_raw_pkt, UCHAR snd_routedir, char *feedback_buf, int *feedback_buf_len) { diff --git a/src/plugin/CMakeLists.txt b/src/plugin/CMakeLists.txt index 9c1b83a..01c4762 100644 --- a/src/plugin/CMakeLists.txt +++ b/src/plugin/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) include_directories(inc) 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_library(plugctrl STATIC src/plugin_proc.c src/plugin.c src/plugin_platform.c src/plugin_protocol.c src/plugin_business.c) \ No newline at end of file diff --git a/src/project/CMakeLists.txt b/src/project/CMakeLists.txt index 8662b50..822e387 100644 --- a/src/project/CMakeLists.txt +++ b/src/project/CMakeLists.txt @@ -2,11 +2,12 @@ cmake_minimum_required(VERSION 2.8) add_definitions(-D_BSD_SOURCE -D_BSD_SOURCE -D__BSD_SOURCE -D__FAVOR_BSD=1 -DHAVE_NET_ETHERNET_H) add_definitions(-DPLATFORM_NSDPF_PAPP=1) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) 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_library(project STATIC project_requirement.c raw_ip_frag_list.c ) diff --git a/src/timer/CMakeLists.txt b/src/timer/CMakeLists.txt index 494e9b7..9ba1fbf 100644 --- a/src/timer/CMakeLists.txt +++ b/src/timer/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) 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) + if(OPT_IOMODE_MARSIO) include_directories(${MARSIO_SDK_PREFIX}/include) diff --git a/src/timer/sapp_timer.c b/src/timer/sapp_timer.c index 92d50e7..dcf00d8 100644 --- a/src/timer/sapp_timer.c +++ b/src/timer/sapp_timer.c @@ -237,7 +237,7 @@ static void __st_add_time_event_entry(void) topt.tv_sec = sapp_global_val->config.profiling.log.interval; topt.tv_usec = 0; sapp_event_set_opt(tev, STEO_TIMEOUT_VAL, &topt, sizeof(topt)); - sapp_event_set_opt(tev, STEO_CALLBACK_FUN, (void *)&line_protocol_timer_cbfun, sizeof(void *)); + //sapp_event_set_opt(tev, STEO_CALLBACK_FUN, (void *)&line_protocol_timer_cbfun, sizeof(void *)); ret = sapp_timer_add(th, tev); assert(ret >= 0); @@ -255,7 +255,7 @@ int __sapp_timer_platform_init(void) sapp_global_val->individual_fixed.sapp_standalone_timer = __st_timer_new(STO_EFFECTIVE_SCOPE_STANDALONE_THREAD); - __st_add_time_event_entry(); + //__st_add_time_event_entry(); return 0; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd6bc77..f2b67db 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 2.8) -include_directories(${MESA_SDK_PREFIX}/include) -include_directories(${MESA_SDK_PREFIX}/include/MESA) 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) + if(OPT_IOMODE_MARSIO) include_directories(${MARSIO_SDK_PREFIX}/include) diff --git a/test/test_app_sapp.c b/test/test_app_sapp.c index 71979d2..e504781 100644 --- a/test/test_app_sapp.c +++ b/test/test_app_sapp.c @@ -1392,36 +1392,53 @@ char test_inject_tcp_pkt(struct streaminfo *stream,void **pme, int thread_seq,co return APP_STATE_DROPME; } -char test_20140731(struct streaminfo *pstream,void **pme, int thread_seq,const void *raw_pkt) +char test_sapp_inject_pkt(struct streaminfo *stream,void **pme, int thread_seq,const void *raw_pkt) { - static int __tot_c2s_pkt = 0; - static int __tot_s2c_pkt = 0; - static int __tot_s2c_len = 0; - static int __tot_c2s_len = 0; - struct tcpdetail *a_tcp = (struct tcpdetail *)pstream->pdetail; - - /* 2014-07-31, lijia add for test, òͬunorderֵ, ջȡݿܲһ */ - tcp_set_single_stream_max_unorder(pstream, DIR_DOUBLE, 1); + char MESA_inject_pkt_payload[] = "send by MESA_inject_pkt "; + char sapp_inject_pkt_def_payload[] = "send by sapp_inject_pkt def"; + char sapp_inject_pkt_no_hdr_payload[256]; + struct stream_tuple4_v4 *tuple_v4; - if(a_tcp->datalen <= 0){ - return APP_STATE_GIVEME; - } - - if(DIR_C2S == pstream->curdir){ - __tot_c2s_pkt++; - __tot_c2s_len += a_tcp->datalen; - }else{ - __tot_s2c_pkt++; - __tot_s2c_len += a_tcp->datalen; + if(OP_STATE_CLOSE == stream->opstate){ + return APP_STATE_DROPME; } - printf("tot pkt: C2S:%6d, S2C:%6d\n", __tot_c2s_pkt, __tot_s2c_pkt); - printf("tot len: C2S:%6d, S2C:%6d\n\n", __tot_c2s_len, __tot_s2c_len); + tuple_v4 = (struct stream_tuple4_v4 *)(stream->addr.paddr); + + MESA_inject_pkt(stream, MESA_inject_pkt_payload, strlen("send by MESA_inject_pkt "), raw_pkt, stream->routedir); + sapp_inject_pkt(stream, SIO_DEFAULT, sapp_inject_pkt_def_payload, strlen("send by sapp_inject_pkt def"), stream->routedir); + struct mesa_ip4_hdr *ip4hdr = (struct mesa_ip4_hdr *)sapp_inject_pkt_no_hdr_payload; + struct mesa_tcp_hdr *thdr = (struct mesa_tcp_hdr *)((char *)ip4hdr + sizeof(struct mesa_ip4_hdr)); //no options + + thdr->th_sport = tuple_v4->source; + thdr->th_dport = tuple_v4->dest; + thdr->th_seq = 0x11111111; + thdr->th_ack = 0x22222222; + thdr->th_off = 5; + thdr->th_win = 100; + thdr->th_sum = 0; + + ip4hdr->ip_v = 4; + ip4hdr->ip_hl = 5; + ip4hdr->ip_len = htons(sizeof(struct mesa_ip4_hdr) + sizeof(struct mesa_tcp_hdr) + strlen("send by sapp_inject_pkt hdr")); + ip4hdr->ip_id = 0x3333; + ip4hdr->ip_off = 0; + ip4hdr->ip_ttl = 128; + ip4hdr->ip_p = 6; + ip4hdr->ip_sum = 0; + ip4hdr->ip_src.s_addr = tuple_v4->saddr; + ip4hdr->ip_dst.s_addr = tuple_v4->daddr; + + strcpy(sapp_inject_pkt_no_hdr_payload + sizeof(struct mesa_ip4_hdr) + sizeof(struct mesa_tcp_hdr), "send by sapp_inject_pkt hdr"); + + sapp_inject_pkt(stream, SIO_EXCLUDE_THIS_LAYER_HDR, sapp_inject_pkt_no_hdr_payload, ntohs(ip4hdr->ip_len), stream->routedir); + return APP_STATE_GIVEME; } + char test_project_add(struct streaminfo *pstream,void **pme, int thread_seq,const void *a_packet) { static int __init_flag = 0; @@ -2363,12 +2380,14 @@ char IPv6_RAW_ENTRY(const struct streaminfo *pstream, const void *this_hdr, cons return APP_STATE_GIVEME; } +#endif + char POLLING_ENTRY(struct streaminfo *stream, void **pme, int thread_seq,void *a_packet) { + printf("polling entry, tid:%d\n", thread_seq); return APP_STATE_GIVEME; } -#endif int CHAR_INIT() -- cgit v1.2.3