From f4ef8c317d3c9cdcc77cb8cad1c9bf44db8dec25 Mon Sep 17 00:00:00 2001 From: fumingwei Date: Wed, 7 Jun 2023 20:25:32 +0800 Subject: [PATCH] temp commit --- conf/kni/kni.conf | 6 ++ entry/include/kni_entry.h | 2 + entry/include/kni_fieldstat.h | 64 ++++++++++++ entry/src/kni_entry.cpp | 23 +++++ entry/src/kni_fieldstat.cpp | 183 ++++++++++++++++++++++++++++++++++ 5 files changed, 278 insertions(+) create mode 100644 entry/include/kni_fieldstat.h create mode 100644 entry/src/kni_fieldstat.cpp diff --git a/conf/kni/kni.conf b/conf/kni/kni.conf index bc5a6af..2e34eda 100644 --- a/conf/kni/kni.conf +++ b/conf/kni/kni.conf @@ -167,3 +167,9 @@ tcp_passthrough = 0 [share_session_attribute] SESSION_ATTRIBUTE_LABEL=TSG_MASTER_INTERNAL_LABEL + +[proxy_hits] +interval_ms=1000 +telegraf_port=8400 +telegraf_ip=127.0.0.1 +app_name="proxy_rule_hits" diff --git a/entry/include/kni_entry.h b/entry/include/kni_entry.h index 9a93b14..dea67cf 100644 --- a/entry/include/kni_entry.h +++ b/entry/include/kni_entry.h @@ -8,6 +8,7 @@ #include "tfe_mgr.h" #include #include "kni_iouring.h" +#include "kni_fieldstat.h" #define BURST_MAX 1 #define CALLER_SAPP 0 @@ -247,6 +248,7 @@ struct kni_handle{ struct proxy_tcp_option pxy_tcp_option; // int session_attribute_id; int log_level; + struct proxy_fieldstat *proxy_fieldstat; }; struct traceid2pme_search_cb_args{ diff --git a/entry/include/kni_fieldstat.h b/entry/include/kni_fieldstat.h new file mode 100644 index 0000000..bafa40e --- /dev/null +++ b/entry/include/kni_fieldstat.h @@ -0,0 +1,64 @@ +#ifndef _KNI_FIELDSTAT_METRIC_H +#define _KNI_FIELDSTAT_METRIC_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include + + +enum proxy_metrics_column +{ + PROXY_METRIC_COLUMN_HIT_COUNT = 0, + PROXY_METRIC_COLUMN_IN_BYTES, + PROXY_METRIC_COLUMN_OUT_BYTES, + PROXY_METRIC_COLUMN_IN_PKTS, + PROXY_METRIC_COLUMN_OUT_PKTS, + PROXY_METRIC_COLUMN_MAX +}; + + +struct proxy_fieldstat +{ + int table_id; + int n_thread; + unsigned int column_ids[PROXY_METRIC_COLUMN_MAX]; + struct fieldstat_dynamic_instance *instance; +}; + +struct proxy_metric_tag +{ + int vsys_id; + uint64_t rule_id; + uint8_t action; + uint8_t pinning_status; +}; + +// struct proxy_fieldstat *proxy_fieldstat_new(char *app_name, int n_thread, +// int interval_ms, +// const char *telegraf_ip, +// unsigned short telegraf_port, +// void *local_logger); + +// void proxy_fieldstat_free(struct proxy_fieldstat *pxy_fs); + +void proxy_set_metric_value(struct proxy_fieldstat *pxy_fs, + struct proxy_metric_tag *metric_tags, + enum proxy_metrics_column column, + int value, int thread_id); + +struct proxy_fieldstat *proxy_fieldstat_init(const char *profile, + const char *section, + int n_thread, + void *logger); + +void *proxy_fieldstat_destory(struct proxy_fieldstat *pxy_fs, void *logger); +#ifdef __cpluscplus +} + + +#endif + +#endif diff --git a/entry/src/kni_entry.cpp b/entry/src/kni_entry.cpp index a5394a8..448db6f 100644 --- a/entry/src/kni_entry.cpp +++ b/entry/src/kni_entry.cpp @@ -2186,6 +2186,19 @@ static int wrapped_kni_cmsg_get(struct pme_info *pmeinfo, struct kni_cmsg *cmsg, } +static void set_proxy_metric(struct pme_info *pmeinfo, int thread_id) +{ + void *logger = g_kni_handle->local_logger; + struct proxy_metric_tag metric_tag; + memset(&metric_tag, 0, sizeof(struct proxy_metric_tag)); + metric_tag.action = pmeinfo->maat_rule.aciton; + metric_tag.rule_id = pmeinfo->maat_rule.rule_id; + metric_tag.vsys_id = pmeinfo->maat_rule.vsys_id; + metric_tag.pinning_status = pmeinfo->ssl_intercept_state; + + proxy_set_metric_value(g_kni_handle->proxy_fieldstat, &metric_tag, + PROXY_METRIC_COLUMN_HIT_COUNT, pmeinfo->, thread_id); +} static long traceid2pme_htable_search_cb(void *data, const uchar *key, uint size, void *user_args){ struct traceid2pme_search_cb_args *args = (struct traceid2pme_search_cb_args*)user_args; @@ -2883,6 +2896,16 @@ extern "C" int kni_init(){ } g_kni_fs_handle = fs_handle; + int n_pxy_fs_thread = g_kni_handle->thread_count + 1; + g_kni_handle->proxy_fieldstat = proxy_fieldstat_init(profile, "proxy_hits", + n_pxy_fs_thread, + local_logger); + if(g_kni_handle->proxy_fieldstat == NULL) + { + KNI_LOG_ERROR(local_logger, "Failed at init proxy fieldstat"); + goto error_out; + } + //init traceid2pme_htable struct kni_htable_opt opt; memset(&opt, 0, sizeof(opt)); diff --git a/entry/src/kni_fieldstat.cpp b/entry/src/kni_fieldstat.cpp new file mode 100644 index 0000000..184cfa9 --- /dev/null +++ b/entry/src/kni_fieldstat.cpp @@ -0,0 +1,183 @@ +#include +#include "kni_fieldstat.h" +#include "kni_utils.h + +static int read_metric_tags(struct proxy_metric_tag *metric_tags, + struct fieldstat_tag tags[]) +{ + int n_tags = 0; + + int vsys_id = 0; + uint64_t rule_id = 0; + uint8_t hit_no_intercept = 0; + uint8_t pinning_status = 0; + + tags[n_tags].key = "vsys_id"; + tags[n_tags].value_type = 0; + tags[n_tags].value_int = metric_tags->vsys_id; + n_tags++; + + tags[n_tags].key = "rule_id"; + tags[n_tags].value_type = 0; + tags[n_tags].value_int = metric_tags->rule_id; + n_tags++; + + if(metric_tags->pinning_status == 1) //TODO + { + tags[n_tags].key = "pinning_status"; + tags[n_tags].value_type = 0; + tags[n_tags].value_int = metric_tags->pinning_status; + n_tags++; + } + + tags[n_tags].key = "action"; + tags[n_tags].value_type = 0; + // tags[n_tags].value_int = (hit_no_intercept == 1 ? 3 : 2); + tags[n_tags].value_int = metric->action; + n_tags++; + + return n_tags; +} + + +void proxy_set_metric_value(struct proxy_fieldstat *pxy_fs, + struct proxy_metric_tag *metric_tags, + enum proxy_metrics_column column, + int value, int thread_id) +{ + int n_tags = 0; + const char *metric_row_name = "proxy_rule_hits"; + struct fieldstat_tag metric_tags[5] = {0}; + + if(value < 1) + { + return; + } + + n_tags = read_metric_tags(metric_tags); + fieldstat_dynamic_table_metric_value_incrby(pxy_fs->instance, + pxy_fs->table_id, + pxy_fs->column_ids[column], + metric_row_name, value, + metric_tags,(size_t)n_tags, + thread_id); +} + +struct proxy_fieldstat *proxy_fieldstat_new(char *app_name, int n_thread, + int interval_ms, + const char *telegraf_ip, + unsigned short telegraf_port, + void *local_logger) +{ + struct proxy_fieldstat *pxy_fs = NULL; + + const char *column_field[PROXY_METRIC_COLUMN_MAX] = { + "hit_count", "in_bytes","out_bytes", "in_pkts","out_pkts"}; + enum field_type column_type[PROXY_METRIC_COLUMN_MAX] = { + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER + }; + + pxy_fs = calloc(1, sizeof(struct proxy_fieldstat)); + pxy_fs->instance = fieldstat_dynamic_table_instance_new(app_name, n_thread); + + if(pxy_fs->instance == NULL) + { + goto error; + } + + pxy_fs->n_thread = n_thread; + fieldstat_dynamic_set_line_protocol_server(pxy_fs->instance, telegraf_ip, + telegraf_port); + fieldstat_dynamic_set_output_interval(pxy_fs->instance, interval_ms); + + pxy_fs->table_id = fieldstat_register_dynamic_table( + pxy_fs->instance, + "proxy_rule_hits", + column_field, + column_type, + (size_t)PROXY_METRIC_COLUMN_MAX, + pxy_fs->column_ids); + + if(pxy_fs->table_id < 0) + { + goto error; + } + + fieldstat_dynamic_instance_start(pxy_fs->instance); + return pxy_fs; + +error: + if(pxy_fs) + { + free(pxy_fs); + pxy_fs = NULL; + } + return NULL; +} + +void proxy_fieldstat_free(struct proxy_fieldstat *pxy_fs) +{ + if(pxy_fs) + { + if(pxy_fs->instance) + { + fieldstat_dynamic_instance_free(pxy_fs->instance); + pxy_fs->instance = NULL; + } + free(pxy_fs); + pxy_fs = NULL; + } +} + + +struct proxy_fieldstat *proxy_fieldstat_init(const char *profile, + const char *section, + int n_thread, + void *logger); +{ + int interval_ms = 0; + unsigned short telegraf_port = 0; + char telegraf_ip[KNI_ADDR_MAX] = {0}; + char app_name[KNI_STRING_MAX] = {0}; + struct proxy_fieldstat *pxy_fs = NULL; + + MESA_load_profile_short_nodef(profile, section, "telegraf_port", + (short *)&(telegraf_port)); + MESA_load_profile_string_nodef(profile, section, "telegraf_ip", telegraf_ip, + sizeof(telegraf_ip)); + MESA_load_profile_string_def(profile, section, "app_name", app_name, + sizeof(app_name), "proxy_rule_hits"); + MESA_load_profile_int_def(profile, section, "interval_ms", &interval_ms, + 1000); + + pxy_fs = proxy_fieldstat_new(app_name, n_thread, interval_ms, telegraf_ip, + telegraf_port, logger); + if (pxy_fs == NULL) + { + TFE_LOG_ERROR(logger, "proxy fieldstat init failed, error to create fieldstat metric."); + return NULL; + } + KNI_LOG_ERROR(logger, "proxy fieldstat telegraf_ip : %s", telegraf_ip); + KNI_LOG_ERROR(logger, "proxy fieldstat telegraf_port : %d", telegraf_port); + KNI_LOG_ERROR(logger, "proxy fieldstat app_name : %s", app_name); + KNI_LOG_ERROR(logger, "proxy fieldstat interval_ms : %d", interval_ms); + + return pxy_fs; + } + + +void *proxy_fieldstat_destory(struct proxy_fieldstat * pxy_fs, void *logger) +{ + if(pxy_fs) + { + proxy_fieldstat_free(pxy_fs); + pxy_fs = NULL; + } + KNI_LOG_ERROR(logger, "Destory proxy fieldstat!"); +} + +