2023-04-25 10:13:38 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <tfe_fieldstat.h>
|
|
|
|
|
|
2023-05-06 19:04:06 +08:00
|
|
|
#include "tfe_stream.h"
|
|
|
|
|
#include "tfe_resource.h"
|
2024-01-19 10:52:42 +08:00
|
|
|
#include "tfe_packet_io.h"
|
2023-05-06 19:04:06 +08:00
|
|
|
|
2024-01-19 10:52:42 +08:00
|
|
|
void tfe_set_intercept_metric(struct tfe_fieldstat_metric_t *fieldstat, struct session_ctx *s_ctx, int thread_id, int is_session_close)
|
2023-05-06 19:04:06 +08:00
|
|
|
{
|
|
|
|
|
int ret;
|
2024-02-22 15:59:32 +08:00
|
|
|
int hit_count = 0;
|
2023-05-06 19:04:06 +08:00
|
|
|
uint16_t out_size;
|
2024-01-19 10:52:42 +08:00
|
|
|
struct tfe_cmsg *cmsg = s_ctx->cmsg;
|
|
|
|
|
struct timespec current_time;
|
2023-05-06 19:04:06 +08:00
|
|
|
|
|
|
|
|
if (cmsg == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 15:59:32 +08:00
|
|
|
if (s_ctx->metric_hit == 0) {
|
|
|
|
|
int flag = 0;
|
|
|
|
|
flag = tfe_cmsg_get_flag(cmsg);
|
|
|
|
|
if ((flag & TFE_CMSG_FLAG_USER0) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
s_ctx->metric_hit = 1;
|
|
|
|
|
hit_count = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 10:52:42 +08:00
|
|
|
if (!is_session_close)
|
|
|
|
|
{
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
|
|
|
|
|
if (current_time.tv_sec - s_ctx->metrics_last_time.tv_sec < 1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s_ctx->metrics_last_time = current_time;
|
|
|
|
|
int downstream_dir = s_ctx->c2s_info.is_e2i_dir;
|
|
|
|
|
int downstream_rx_pkts = s_ctx->c2s_info.rx.n_pkts - s_ctx->c2s_info.rx_send_complete.n_pkts;
|
|
|
|
|
int downstream_rx_bytes = s_ctx->c2s_info.rx.n_bytes - s_ctx->c2s_info.rx_send_complete.n_bytes;
|
|
|
|
|
int upstream_dir = s_ctx->s2c_info.is_e2i_dir;
|
|
|
|
|
int upstream_rx_pkts = s_ctx->s2c_info.rx.n_pkts - s_ctx->s2c_info.rx_send_complete.n_pkts;
|
|
|
|
|
int upstream_rx_bytes = s_ctx->s2c_info.rx.n_bytes - s_ctx->s2c_info.rx_send_complete.n_bytes;
|
|
|
|
|
s_ctx->c2s_info.rx_send_complete = s_ctx->c2s_info.rx;
|
|
|
|
|
s_ctx->s2c_info.rx_send_complete = s_ctx->s2c_info.rx;
|
|
|
|
|
|
2023-05-06 19:04:06 +08:00
|
|
|
int vsys_id = 0;
|
|
|
|
|
ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_POLICY_VSYS_ID, (unsigned char *)&vsys_id, sizeof(vsys_id), &out_size);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "failed at fetch vsys_id from cmsg: %s", strerror(-ret));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t rule_id = 0;
|
|
|
|
|
ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_POLICY_ID, (unsigned char *)&rule_id, sizeof(rule_id), &out_size);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "failed at fetch rule_id from cmsg: %s", strerror(-ret));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t hit_no_intercept = 0;
|
|
|
|
|
ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_HIT_NO_INTERCEPT, (unsigned char *)&hit_no_intercept, sizeof(hit_no_intercept), &out_size);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "failed at fetch hit_no_intercept from cmsg: %s", strerror(-ret));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int in_pkts = 0;
|
|
|
|
|
int in_bytes = 0;
|
|
|
|
|
int out_pkts = 0;
|
|
|
|
|
int out_bytes = 0;
|
|
|
|
|
|
|
|
|
|
// incoming : E2I 的流量
|
2024-01-19 10:52:42 +08:00
|
|
|
// outgoing : I2E 的流量
|
2023-05-06 19:04:06 +08:00
|
|
|
// first_ctr_packet_dir <==> client hello packet dir
|
2024-01-03 13:14:38 +08:00
|
|
|
// 1: E2I 0:I2E
|
|
|
|
|
if (downstream_dir == 1)
|
2023-05-06 19:04:06 +08:00
|
|
|
{
|
2024-01-19 10:52:42 +08:00
|
|
|
in_pkts += downstream_rx_pkts;
|
|
|
|
|
in_bytes += downstream_rx_bytes;
|
2023-05-06 19:04:06 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-19 10:52:42 +08:00
|
|
|
out_pkts += downstream_rx_pkts;
|
|
|
|
|
out_bytes += downstream_rx_bytes;
|
2023-05-06 19:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-03 13:14:38 +08:00
|
|
|
if (upstream_dir == 1)
|
|
|
|
|
{
|
2024-01-19 10:52:42 +08:00
|
|
|
in_pkts += upstream_rx_pkts;
|
|
|
|
|
in_bytes += upstream_rx_bytes;
|
2024-01-03 13:14:38 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-19 10:52:42 +08:00
|
|
|
out_pkts += upstream_rx_pkts;
|
|
|
|
|
out_bytes += upstream_rx_bytes;
|
2024-01-03 13:14:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-06 19:04:06 +08:00
|
|
|
int nr_tags = 0;
|
|
|
|
|
struct fieldstat_tag temp_tags[TAG_MAX] = {0};
|
|
|
|
|
|
|
|
|
|
temp_tags[nr_tags].key = "vsys_id";
|
|
|
|
|
temp_tags[nr_tags].value_type = 0;
|
|
|
|
|
temp_tags[nr_tags].value_int = vsys_id;
|
|
|
|
|
nr_tags++;
|
|
|
|
|
|
|
|
|
|
temp_tags[nr_tags].key = "rule_id";
|
|
|
|
|
temp_tags[nr_tags].value_type = 0;
|
|
|
|
|
temp_tags[nr_tags].value_int = rule_id;
|
|
|
|
|
nr_tags++;
|
|
|
|
|
|
|
|
|
|
uint8_t pinning_status = 0;
|
|
|
|
|
if (tfe_cmsg_get_value(cmsg, TFE_CMSG_SSL_PINNING_STATE, (unsigned char *)&pinning_status, sizeof(pinning_status), &out_size) == 0)
|
|
|
|
|
{
|
|
|
|
|
temp_tags[nr_tags].key = "pinning_status";
|
|
|
|
|
temp_tags[nr_tags].value_type = 0;
|
|
|
|
|
temp_tags[nr_tags].value_int = pinning_status;
|
|
|
|
|
nr_tags++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// action : 2 Intercept; 3 No Intercept
|
|
|
|
|
temp_tags[nr_tags].key = "action";
|
|
|
|
|
temp_tags[nr_tags].value_type = 0;
|
|
|
|
|
temp_tags[nr_tags].value_int = (hit_no_intercept == 1 ? 3 : 2);
|
|
|
|
|
nr_tags++;
|
|
|
|
|
|
|
|
|
|
// sub_action not need for intercept metrics
|
|
|
|
|
|
|
|
|
|
if (hit_count > 0)
|
|
|
|
|
{
|
2023-05-26 18:50:18 +08:00
|
|
|
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_HIT_COUNT], "proxy_rule_hits", hit_count, temp_tags, (size_t)nr_tags, thread_id);
|
2023-05-06 19:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_pkts > 0)
|
|
|
|
|
{
|
2023-05-26 18:50:18 +08:00
|
|
|
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_IN_PKTS], "proxy_rule_hits", in_pkts, temp_tags, (size_t)nr_tags, thread_id);
|
2023-05-06 19:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_bytes > 0)
|
|
|
|
|
{
|
2023-05-26 18:50:18 +08:00
|
|
|
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_IN_BYTES], "proxy_rule_hits", in_bytes, temp_tags, (size_t)nr_tags, thread_id);
|
2023-05-06 19:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (out_pkts > 0)
|
|
|
|
|
{
|
2023-05-26 18:50:18 +08:00
|
|
|
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_OUT_PKTS], "proxy_rule_hits", out_pkts, temp_tags, (size_t)nr_tags, thread_id);
|
2023-05-06 19:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (out_bytes > 0)
|
|
|
|
|
{
|
2023-05-26 18:50:18 +08:00
|
|
|
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_OUT_BYTES], "proxy_rule_hits", out_bytes, temp_tags, (size_t)nr_tags, thread_id);
|
2023-05-06 19:04:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 17:51:57 +08:00
|
|
|
int tfe_fieldstat_metric_incrby(struct tfe_fieldstat_metric_t *fieldstat, unsigned int column_id, long long value, const struct fieldstat_tag tags[], int n_tags, int thread_id)
|
2023-04-25 10:13:38 +08:00
|
|
|
{
|
2023-05-10 17:51:57 +08:00
|
|
|
return fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, column_id, "proxy_rule_hits", value, tags, (size_t)n_tags, thread_id);
|
2023-04-25 10:13:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tfe_fieldstat_metric_t *tfe_fieldstat_metric_create(char *telegraf_ip, int telegraf_port, char *app_name, int cycle, int max_thread, void *local_logger)
|
|
|
|
|
{
|
|
|
|
|
int i=0;
|
|
|
|
|
|
2023-05-10 17:51:57 +08:00
|
|
|
struct fieldstat_tag metric_tags[TAG_MAX - 1] = {{"vsys_id", 0, -1}, {"rule_id", 0, -1}, {"action", 0, -1}, {"sub_action", 2, -1}};
|
2023-04-25 10:13:38 +08:00
|
|
|
const char *column_field[COLUMN_MAX] = {"hit_count", "in_bytes", "out_bytes", "in_pkts", "out_pkts"};
|
|
|
|
|
enum field_type column_type[COLUMN_MAX] = {FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER};
|
|
|
|
|
|
|
|
|
|
struct tfe_fieldstat_metric_t *fieldstat = ALLOC(struct tfe_fieldstat_metric_t, 1);
|
|
|
|
|
|
|
|
|
|
fieldstat->instance = fieldstat_dynamic_instance_new(app_name, max_thread);
|
|
|
|
|
if(!fieldstat->instance)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(local_logger, "fieldstat3 dynamic instance init failed.");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fieldstat->max_thread=max_thread;
|
|
|
|
|
fieldstat_dynamic_set_line_protocol_server(fieldstat->instance, telegraf_ip, telegraf_port);
|
|
|
|
|
fieldstat_dynamic_set_output_interval(fieldstat->instance, cycle);
|
|
|
|
|
|
|
|
|
|
fieldstat->table_id = fieldstat_register_dynamic_table(fieldstat->instance, "proxy_rule_hits", column_field, column_type, (size_t)COLUMN_MAX, fieldstat->column_array);
|
|
|
|
|
if(fieldstat->table_id < 0)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(local_logger, "fieldstat3 register dynamic table failed.");
|
|
|
|
|
FREE(&fieldstat);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fieldstat->tags = ALLOC(struct fieldstat_tag*, max_thread);
|
|
|
|
|
for (i = 0; i < max_thread; i++)
|
|
|
|
|
{
|
2023-05-10 17:51:57 +08:00
|
|
|
fieldstat->tags[i] = ALLOC(struct fieldstat_tag, TAG_MAX-1);
|
|
|
|
|
memcpy(fieldstat->tags[i], metric_tags, sizeof(struct fieldstat_tag) * (size_t)(TAG_MAX-1));
|
2023-04-25 10:13:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fieldstat_dynamic_instance_start(fieldstat->instance);
|
|
|
|
|
return fieldstat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tfe_fieldstat_metric_destroy(struct tfe_fieldstat_metric_t *fieldstat)
|
|
|
|
|
{
|
|
|
|
|
if(fieldstat)
|
|
|
|
|
{
|
|
|
|
|
if(fieldstat->instance)
|
|
|
|
|
{
|
|
|
|
|
fieldstat_dynamic_instance_free(fieldstat->instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < fieldstat->max_thread; i++)
|
|
|
|
|
{
|
|
|
|
|
if (fieldstat->tags[i])
|
|
|
|
|
{
|
|
|
|
|
FREE(&fieldstat->tags[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
FREE(&fieldstat->tags);
|
|
|
|
|
FREE(&fieldstat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|