diff options
| author | 崔一鸣 <[email protected]> | 2018-11-26 21:23:00 +0800 |
|---|---|---|
| committer | 崔一鸣 <[email protected]> | 2018-11-26 21:23:00 +0800 |
| commit | eaab1f2770dbda848a01f31c631961a016526bf8 (patch) | |
| tree | eec2a656ed23ef176e2f429ba476f6b7a6c49cb0 /access/src/main.cpp | |
| parent | 957151da0b0c0ad7be5c6577d8cb7619cbcbeae0 (diff) | |
增加field_stat统计,修复内存泄漏,增加一些功能,该版本可稳定运行
Diffstat (limited to 'access/src/main.cpp')
| -rw-r--r-- | access/src/main.cpp | 245 |
1 files changed, 216 insertions, 29 deletions
diff --git a/access/src/main.cpp b/access/src/main.cpp index 61b0b15..15d37f8 100644 --- a/access/src/main.cpp +++ b/access/src/main.cpp @@ -17,13 +17,16 @@ struct udp_client_handle struct mgw_handle { void* logger; - const char *profile; + char *profile; Maat_feather_t Maat_feather; struct htable_opts *htable_opts_s; struct mgw_tun_handle *mgw_tun_handle_s; struct nat_handle *nat_handle_s; struct ip_mgr_handle *ip_mgr_handle_s; struct udp_client_handle *udp_client_handle_s; + MESA_htable_handle cand_ip_detail_htable; + MESA_htable_handle ip2user_htable; + struct field_stat_handle *fs_handle; }; static void wrapped_Maat_set_feather_opt(void *logger, Maat_feather_t feather, enum MAAT_INIT_OPT type, const void* value, int size) @@ -62,9 +65,9 @@ static Maat_feather_t Maat_init(const char *profile, void *logger) wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_INSTANCE_NAME, "mgw", strlen("mgw")+1); wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_REDIS_IP, Maat_redis_ip, strlen(Maat_redis_ip)+1); wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_REDIS_PORT, &Maat_redis_port, sizeof(Maat_redis_port)); - wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_STAT_FILE_PATH, stat_file_path, strlen(stat_file_path)+1); - wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_STAT_ON, NULL, 0); - wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_PERF_ON, NULL, 0); + //wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_STAT_FILE_PATH, stat_file_path, strlen(stat_file_path)+1); + //wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_STAT_ON, NULL, 0); + //wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_PERF_ON, NULL, 0); //wrapped_Maat_set_feather_opt(feather, MAAT_OPT_SCANDIR_INTERVAL_MS,&scan_interval_ms, sizeof(scan_interval_ms)); //wrapped_Maat_set_feather_opt(feather, MAAT_OPT_EFFECT_INVERVAL_MS, &effective_interval_ms, sizeof(effective_interval_ms)); //wrapped_Maat_set_feather_opt(feather, MAAT_OPT_SCAN_DETAIL, &scan_detail, sizeof(scan_detail)); @@ -92,15 +95,22 @@ static struct htable_opts * htable_opt_init(const char* profile, void *logger) return _htable_opts; } -void ip2user_htable_free_data_cb(void *data) +static void ip2user_htable_data_free_cb(void *data) { FREE(&data); } -struct udp_client_handle * udp_client_init(const char *profile, void *logger) +static void cand_ip_detail_htable_data_free_cb(void *data) +{ + struct ip_mgr_cand_ip_detail *cand_ip_detail = (struct ip_mgr_cand_ip_detail *)data; + FREE(&cand_ip_detail->vxlan_info); + FREE(&data); +} + +static struct udp_client_handle * udp_client_init(const char *profile, void *logger) { const char *section = "mrl"; - char ip[MGW_SYMBOL_MAX]; + char ip[MGW_SYMBOL_MAX] = ""; uint16_t port; MESA_load_profile_string_def(profile, section, "ip", ip, MGW_SYMBOL_MAX, "192.168.10.242"); MESA_load_profile_int_def(profile, section, "port", (int *)&port, 23456); @@ -119,6 +129,138 @@ struct udp_client_handle * udp_client_init(const char *profile, void *logger) return handle; } +static struct field_stat_handle * fs_init(const char *profile, void *logger) +{ + const char *section = "field_stat"; + char stat_path[MGW_SYMBOL_MAX] = ""; + char FP_HISTOGRAM_BINS[MGW_SYMBOL_MAX] = ""; + MESA_load_profile_string_def(profile, section, "stat_path", stat_path, MGW_SYMBOL_MAX, "./fs2_mgw.status"); + MESA_load_profile_string_def(profile, section, "histogram_bins", FP_HISTOGRAM_BINS, MGW_SYMBOL_MAX, "0.5,0.8,0.9,0.95"); + MGW_LOG_INFO(logger, "MESA_prof_load, [%s]:\n stat_path: %s\n histogram_bins: %s", "field_stat", stat_path, FP_HISTOGRAM_BINS); + screen_stat_handle_t handle = NULL; + const char* app_name = "fs2_mgw"; + char buff[128]; + int value=0; + handle = FS_create_handle(); + FS_set_para(handle, HISTOGRAM_GLOBAL_BINS, FP_HISTOGRAM_BINS, strlen(FP_HISTOGRAM_BINS)+1); + + FS_set_para(handle, APP_NAME, app_name, strlen(app_name)+1); + value=0; + FS_set_para(handle, FLUSH_BY_DATE, &value, sizeof(value)); + FS_set_para(handle, OUTPUT_DEVICE, stat_path, strlen(stat_path)+1); + value=1; + FS_set_para(handle, PRINT_MODE, &value, sizeof(value)); + value=1; + FS_set_para(handle, CREATE_THREAD, &value, sizeof(value)); + value=5; + FS_set_para(handle, STAT_CYCLE, &value, sizeof(value)); + value=4096; + FS_set_para(handle, MAX_STAT_FIELD_NUM, &value, sizeof(value)); + + //field: packet handle status + snprintf(buff, sizeof(buff),"read_from_tun"); + int field_read_from_tun = FS_register(handle, FS_STYLE_FIELD, FS_CALC_CURRENT, buff); + + snprintf(buff, sizeof(buff),"write_to_tun"); + int field_write_to_tun = FS_register(handle, FS_STYLE_FIELD, FS_CALC_CURRENT, buff); + + snprintf(buff, sizeof(buff),"recv_from_mrl"); + int field_recv_from_mrl = FS_register(handle, FS_STYLE_FIELD, FS_CALC_CURRENT, buff); + + snprintf(buff, sizeof(buff),"send_to_mrl"); + int field_send_to_mrl = FS_register(handle, FS_STYLE_FIELD, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"snat_succ_rate"); + FS_register_ratio(handle, + field_send_to_mrl, + field_read_from_tun, + 1, + FS_STYLE_FIELD, + FS_CALC_CURRENT, + buff); + + snprintf(buff,sizeof(buff),"dnat_succ_rate"); + FS_register_ratio(handle, + field_write_to_tun, + field_recv_from_mrl, + 1, + FS_STYLE_FIELD, + FS_CALC_CURRENT, + buff); + + //column: cache hit/miss, line htable + snprintf(buff,sizeof(buff),"snat"); + int line_snat = FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"dnat"); + int line_dnat = FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"ip2user"); + int line_ip2user = FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"user_policy"); + int line_user_policy = FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"ip_group"); + int line_ip_group = FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"ip_detail"); + int line_ip_detail = FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"query_num"); + int cloumn_queyr_num = FS_register(handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"element_num"); + int cloumn_element_num = FS_register(handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"cache_miss"); + int cloumn_cache_miss = FS_register(handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"cache_hit"); + int cloumn_cache_hit = FS_register(handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff); + + snprintf(buff,sizeof(buff),"cache_hit_rate"); + FS_register_ratio(handle, + cloumn_cache_hit, + cloumn_queyr_num, + 1, + FS_STYLE_COLUMN, + FS_CALC_CURRENT, + buff); + + //snat/dnat latency + snprintf(buff,sizeof(buff), "snat(us)"); + int snat_latency = FS_register_histogram(handle, FS_CALC_CURRENT, buff, 1, 30*1000, 3); + + snprintf(buff,sizeof(buff), "dnat(us)"); + int dnat_latency = FS_register_histogram(handle, FS_CALC_CURRENT, buff, 1, 30*1000, 3); + + FS_start(handle); + struct field_stat_handle *fs_handle= ALLOC(struct field_stat_handle, 1); + fs_handle->handle = handle; + //field + fs_handle->field_read_from_tun = field_read_from_tun; + fs_handle->field_write_to_tun = field_write_to_tun; + fs_handle->field_recv_from_mrl = field_recv_from_mrl; + fs_handle->field_send_to_mrl = field_send_to_mrl; + //cloumn and line + fs_handle->cloumn_cache_hit = cloumn_cache_hit; + fs_handle->cloumn_cache_miss = cloumn_cache_miss; + fs_handle->cloumn_element_num = cloumn_element_num; + fs_handle->cloumn_queyr_num = cloumn_queyr_num; + fs_handle->line_snat = line_snat; + fs_handle->line_dnat = line_dnat; + fs_handle->line_ip2user = line_ip2user; + fs_handle->line_user_policy = line_user_policy; + fs_handle->line_ip_group = line_ip_group; + fs_handle->line_ip_detail = line_ip_detail; + //snat/dnat latency + fs_handle->snat_latency = snat_latency; + fs_handle->dnat_latency = dnat_latency; + return fs_handle; +} + + static struct mgw_handle * mgw_init() { struct mgw_handle *_mgw_handle = ALLOC(struct mgw_handle, 1); @@ -131,8 +273,10 @@ static struct mgw_handle * mgw_init() //init logger char log_path[MGW_PATH_MAX]; + int log_level; MESA_load_profile_string_def(profile, section, "log_path", log_path, sizeof(log_path), "./log/mgw.log"); - void *logger = MESA_create_runtime_log_handle(log_path, RLOG_LV_DEBUG); + MESA_load_profile_int_def(profile, section, "log_level", &log_level, 30); + void *logger = MESA_create_runtime_log_handle(log_path, log_level); if (unlikely(logger == NULL)) { MGW_LOG_ERROR(logger, "Failed at creating logger: %s", log_path); @@ -140,12 +284,16 @@ static struct mgw_handle * mgw_init() } MGW_LOG_INFO(logger, "MESA_prof_load, [%s]:\n log_path: %s", "global", log_path); _mgw_handle->logger = logger; - _mgw_handle->profile = (const char *)profile; + _mgw_handle->profile = (char *)profile; //init htable_opt struct htable_opts * _htable_opts = htable_opt_init(profile, logger); _mgw_handle->htable_opts_s = _htable_opts; + //init feild_stat + struct field_stat_handle *fs_handle = fs_init(profile, logger); + _mgw_handle->fs_handle = fs_handle; + //init Maat Maat_feather_t Maat_feather = Maat_init((const char *)profile, logger); _mgw_handle->Maat_feather = Maat_feather; @@ -153,17 +301,15 @@ static struct mgw_handle * mgw_init() //init tun _mgw_handle->mgw_tun_handle_s = mgw_tun_init("tun_mgw", logger); - //init nat - MESA_htable_handle ip2user_htable = mgw_utils_create_htable("ip2user_htable", _mgw_handle->htable_opts_s, (void *)ip2user_htable_free_data_cb, NULL); - if(ip2user_htable == NULL) + //init ip_mgr + MESA_htable_handle cand_ip_detail_htable = mgw_utils_create_htable("cand_ip_detail_htable", _mgw_handle->htable_opts_s, (void *)cand_ip_detail_htable_data_free_cb, NULL); + if(cand_ip_detail_htable == NULL) { - MGW_LOG_ERROR(logger, "Failed at create ip2user_htable"); + MGW_LOG_ERROR(logger, "Failed at create cand_ip_detail_htable"); exit(EXIT_FAILURE); } - _mgw_handle->nat_handle_s = nat_init(ip2user_htable, logger, _htable_opts); - - //init ip_mgr - struct ip_mgr_handle * _ip_mgr_handle = ip_mgr_init(logger, Maat_feather, _htable_opts); + _mgw_handle->cand_ip_detail_htable = cand_ip_detail_htable; + struct ip_mgr_handle * _ip_mgr_handle = ip_mgr_init(cand_ip_detail_htable, fs_handle, Maat_feather, _htable_opts, logger); if(unlikely(_ip_mgr_handle == NULL)) { MGW_LOG_ERROR(logger, "Failed at init_ip_mgr"); @@ -171,12 +317,25 @@ static struct mgw_handle * mgw_init() } _mgw_handle->ip_mgr_handle_s = _ip_mgr_handle; + + //init nat + MESA_htable_handle ip2user_htable = mgw_utils_create_htable("ip2user_htable", _mgw_handle->htable_opts_s, (void *)ip2user_htable_data_free_cb, NULL); + if(ip2user_htable == NULL) + { + MGW_LOG_ERROR(logger, "Failed at create ip2user_htable"); + exit(EXIT_FAILURE); + } + _mgw_handle->ip2user_htable = ip2user_htable; + _mgw_handle->nat_handle_s = nat_init(ip2user_htable, cand_ip_detail_htable, fs_handle, _htable_opts, logger); + + //create thread_vpn_monitor pthread_t thread_id; struct vpn_monitor_args *args = ALLOC(struct vpn_monitor_args, 1); args->ip2user_htable = ip2user_htable; args->logger = logger; args->profile = (const char *)profile; + args->fs_handle = fs_handle; int rtn = pthread_create(&thread_id, NULL, thread_vpn_monitor, (void *)args); if(unlikely(rtn != 0)) { @@ -190,6 +349,7 @@ static struct mgw_handle * mgw_init() _udp_server_args->profile = profile; _udp_server_args->_nat_handle = _mgw_handle->nat_handle_s; _udp_server_args->tun_handle = _mgw_handle->mgw_tun_handle_s; + _udp_server_args->fs_handle = fs_handle; rtn = pthread_create(&thread_id, NULL, thread_udp_server, (void *)_udp_server_args); if(unlikely(rtn != 0)) { @@ -203,45 +363,71 @@ static struct mgw_handle * mgw_init() return _mgw_handle; } -static void mgw_destroy(struct mgw_handle *_mgw_handle) + +static void mgw_destroy(struct mgw_handle *handle) { - MESA_destroy_runtime_log_handle(_mgw_handle->logger); - Maat_burn_feather(_mgw_handle->Maat_feather); + MESA_destroy_runtime_log_handle(handle->logger); + FREE(&handle->profile); + FREE(&handle->htable_opts_s); + Maat_burn_feather(handle->Maat_feather); + MESA_htable_destroy(handle->cand_ip_detail_htable, NULL); + MESA_htable_destroy(handle->ip2user_htable, NULL); + mgw_tun_destroy(handle->mgw_tun_handle_s); + nat_destroy(handle->nat_handle_s); + ip_mgr_destroy(handle->ip_mgr_handle_s); + FREE(&handle->udp_client_handle_s); + FS_stop(&handle->fs_handle->handle); + FREE(&handle); } -static void send_data_to_mrl(struct udp_client_handle *handle, char *buff, int len, struct ip_mgr_vxlan_info *vxlan_info) +static int send_data_to_mrl(struct udp_client_handle *handle, char *buff, int len, MESA_htable_handle cand_ip_detail_htable, + struct ip_mgr_vxlan_info *vxlan_info, uint32_t mrl_ip) { void *logger = handle->logger; int socket_fd = handle->socket_fd; - memcpy(buff + len, vxlan_info, sizeof(struct ip_mgr_vxlan_info)); + char _buff[MGW_PACKET_MAX]; + memcpy(_buff, vxlan_info, sizeof(struct ip_mgr_vxlan_info)); + memcpy(_buff + sizeof(struct ip_mgr_vxlan_info), buff, len); len += sizeof(struct ip_mgr_vxlan_info); - uint32_t dip = inet_addr(vxlan_info->vxlan_outer_local_ip); uint16_t dport = htons(handle->dport); - int rtn = mgw_socket_udp_send(socket_fd, buff, len, dip, dport); + int rtn = mgw_socket_udp_send(socket_fd, _buff, len, mrl_ip, dport); if (rtn < 0) { MGW_LOG_ERROR(logger, "mgw_socket: Failed at send udp data, errno is %d, %s", errno, strerror(errno)); - return; } - MGW_LOG_INFO(logger, "mgw_socket: Succeed to send udp data, len is %d", len); + return rtn; } static void mgw_run(struct mgw_handle *handle) { void *logger = handle->logger; + sleep(10); + struct timespec start_time, end_time; + struct field_stat_handle *fs_handle = handle->fs_handle; + while(1) { + clock_gettime(CLOCK_MONOTONIC, &start_time); char buff[MGW_PACKET_MAX]; int len = mgw_tun_read(handle->mgw_tun_handle_s, buff, MGW_PACKET_MAX); - printf("tun: len is %d\n", len); + FS_operate(fs_handle->handle, fs_handle->field_read_from_tun, 0, FS_OP_ADD, 1); struct ip_mgr_vxlan_info *vxlan_info = NULL; - int rtn = nat_src_convert(handle->nat_handle_s, handle->ip_mgr_handle_s, buff, len, &vxlan_info); + uint32_t mrl_ip; + int rtn = nat_src_convert(handle->nat_handle_s, handle->ip_mgr_handle_s, buff, len, &vxlan_info, &mrl_ip); if(rtn == NAT_COVERT_FAILURE) { MGW_LOG_ERROR(logger, "Failed at nat_src_convert"); continue; } - send_data_to_mrl(handle->udp_client_handle_s, buff, len, vxlan_info); + int send_len = send_data_to_mrl(handle->udp_client_handle_s, buff, len, handle->cand_ip_detail_htable, vxlan_info, mrl_ip); + if(send_len > 0) + { + FS_operate(fs_handle->handle, fs_handle->field_send_to_mrl, 0, FS_OP_ADD, 1); + } + clock_gettime(CLOCK_MONOTONIC, &end_time); + long long cost_time; + cost_time = (end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_nsec - start_time.tv_nsec) / 1000; + FS_operate(fs_handle->handle, fs_handle->snat_latency, 0, FS_OP_SET, cost_time); } } @@ -249,5 +435,6 @@ int main(int argc, char* argv[]) { struct mgw_handle *handle = mgw_init(); mgw_run(handle); + mgw_destroy(handle); } |
