From e362d521d6cf2b9257e31efa60ab898b9ab2a618 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Thu, 7 Mar 2024 16:17:36 +0800 Subject: [PATCH] =?UTF-8?q?TFE=E5=A2=9E=E5=8A=A0decrypted=20traffic=20mirr?= =?UTF-8?q?or=E7=9A=84=E7=9B=91=E6=8E=A7=E6=8C=87=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../traffic-mirror/include/traffic_mirror.h | 24 +++++++++ plugin/business/traffic-mirror/src/entry.cpp | 50 ++++++++++++++++++- .../business/traffic-mirror/src/rebuild.cpp | 9 +++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/plugin/business/traffic-mirror/include/traffic_mirror.h b/plugin/business/traffic-mirror/include/traffic_mirror.h index a062f64..5423ca2 100644 --- a/plugin/business/traffic-mirror/include/traffic_mirror.h +++ b/plugin/business/traffic-mirror/include/traffic_mirror.h @@ -19,6 +19,28 @@ enum traffic_mirror_target_addr_type TRAFFIC_MIRROR_TARGET_BY_ETHER_ADDR, }; +enum +{ + TRAFFIC_MIRR_STAT_SUCC_BYTES, + TRAFFIC_MIRR_STAT_SUCC_PKTS, + + TRAFFIC_MIRR_STAT_FAIL_BYTES, + TRAFFIC_MIRR_STAT_FAIL_PKTS, + + TRAFFIC_MIRR_STAT_MAX, +}; + +struct traffic_mirror_stat +{ + screen_stat_handle_t fs_handle; + + uint64_t stat_idx[TRAFFIC_MIRR_STAT_MAX]; + uint64_t stat_val[TRAFFIC_MIRR_STAT_MAX]; + + struct event_base *evbase; + struct event *ev; +}; + struct traffic_mirror_instance { void * logger; @@ -35,6 +57,7 @@ struct traffic_mirror_instance char default_ether_addr_dst; struct traffic_mirror_ethdev * ethdev; + struct traffic_mirror_stat *stat; }; struct policy_table_ex_data @@ -108,6 +131,7 @@ struct traffic_mirror_rebuild_target int rewrite_as_target_vlan; }; +extern struct traffic_mirror_instance *g_traffic_mirror_instance; struct traffic_mirror_ethdev * traffic_mirror_ethdev_pcap_create(const char * str_ethdev, void * logger); struct traffic_mirror_ethdev * traffic_mirror_ethdev_mr4_create(const char * str_ethdev, const char * str_app_name, unsigned int nr_threads, void * logger); diff --git a/plugin/business/traffic-mirror/src/entry.cpp b/plugin/business/traffic-mirror/src/entry.cpp index e9376bd..0701054 100644 --- a/plugin/business/traffic-mirror/src/entry.cpp +++ b/plugin/business/traffic-mirror/src/entry.cpp @@ -23,6 +23,53 @@ struct traffic_mirror_me struct traffic_mirror_instance __g_traffic_mirror_instance; struct traffic_mirror_instance * g_traffic_mirror_instance = &__g_traffic_mirror_instance; +static void stat_output_cb(evutil_socket_t fd, short what, void *arg) +{ + struct traffic_mirror_stat *handle = (struct traffic_mirror_stat *)arg; + for (int i = 0; i < TRAFFIC_MIRR_STAT_MAX; i++) + { + FS_operate(handle->fs_handle, handle->stat_idx[i], 0, FS_OP_SET, ATOMIC_READ(&(handle->stat_val[i]))); + } +} + +struct traffic_mirror_stat *traffic_mirror_stat_new() +{ + struct traffic_mirror_stat *handle = (struct traffic_mirror_stat *)calloc(1, sizeof(struct traffic_mirror_stat)); + if (handle == NULL) + { + return NULL; + } + + struct timeval gc_delay = {5, 0}; + const char *stat_name[TRAFFIC_MIRR_STAT_MAX] = { 0 }; + stat_name[TRAFFIC_MIRR_STAT_SUCC_BYTES] = "mirr_succ_B"; + stat_name[TRAFFIC_MIRR_STAT_SUCC_PKTS] = "mirr_succ_P"; + stat_name[TRAFFIC_MIRR_STAT_FAIL_BYTES] = "mirr_fail_B"; + stat_name[TRAFFIC_MIRR_STAT_FAIL_PKTS] = "mirr_fail_P"; + handle->fs_handle = tfe_proxy_get_fs_handle(); + handle->evbase = tfe_proxy_get_gc_evbase(); + for (int i = 0; i < TRAFFIC_MIRR_STAT_MAX; i++) + { + handle->stat_idx[i] = FS_register(handle->fs_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, stat_name[i]); + } + handle->ev = event_new(handle->evbase, -1, EV_PERSIST, stat_output_cb, handle); + evtimer_add(handle->ev, &gc_delay); + + return handle; +} + +void traffic_mirror_stat_free(struct traffic_mirror_stat *handle) +{ + if (handle) + { + if (handle->ev) + { + event_free(handle->ev); + } + free(handle); + } +} + void policy_table_ex_data_free(struct policy_table_ex_data * object) { if ((__sync_sub_and_fetch(&object->atomic_refcnt, 1) == 0)) free(object); @@ -437,7 +484,8 @@ int traffic_mirror_init(struct tfe_proxy * proxy) { int result = 0; struct traffic_mirror_instance * instance = g_traffic_mirror_instance; - instance->logger = tfe_proxy_get_error_logger(); + instance->logger = tfe_proxy_get_error_logger(); + instance->stat = traffic_mirror_stat_new(); /* Using PANGU-HTTP's profile */ MESA_load_profile_uint_def("./conf/tfe/tfe.conf", "traffic_mirror", "enable", &(instance->enable), 1); diff --git a/plugin/business/traffic-mirror/src/rebuild.cpp b/plugin/business/traffic-mirror/src/rebuild.cpp index 5cb5524..b595256 100644 --- a/plugin/business/traffic-mirror/src/rebuild.cpp +++ b/plugin/business/traffic-mirror/src/rebuild.cpp @@ -346,8 +346,13 @@ static void tcp_segment_send_to_target_group(struct tfe_stream_addr * addr, stru int ret = traffic_mirror_ethdev_finish(ethdev, tid, pkt_len); if (unlikely(ret < 0)) { - //TODO: 统计计数 - return; + ATOMIC_ADD(&(g_traffic_mirror_instance->stat->stat_val[TRAFFIC_MIRR_STAT_FAIL_PKTS]), 1); + ATOMIC_ADD(&(g_traffic_mirror_instance->stat->stat_val[TRAFFIC_MIRR_STAT_FAIL_BYTES]), pkt_len); + } + else + { + ATOMIC_ADD(&(g_traffic_mirror_instance->stat->stat_val[TRAFFIC_MIRR_STAT_SUCC_PKTS]), 1); + ATOMIC_ADD(&(g_traffic_mirror_instance->stat->stat_val[TRAFFIC_MIRR_STAT_SUCC_BYTES]), pkt_len); } }