1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
#include <stdio.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <MESA/MESA_prof_load.h>
#include <MESA/swarmkv.h>
#include <fieldstat.h>
#include "log.h"
#include "utils.h"
#include "shaper.h"
#include "shaper_stat.h"
#include "shaper_global_stat.h"
#define SHAPER_STAT_ROW_NAME "traffic_shaping_rule_hits"
#define SHAPER_STAT_REFRESH_TIME_US 10000 //10 ms
struct shaper_stat_conf {
int enable_backgroud_thread;
int output_interval_ms;
char telegraf_ip[16];
short telegraf_port;
};
thread_local struct fieldstat_tag tags[TAG_IDX_MAX] =
{
[TAG_VSYS_ID_IDX] = {.key = "vsys_id", .value_type = 0},
[TAG_RULE_ID_IDX] = {.key = "rule_id", .value_type = 0},
[TAG_PROFILE_ID_IDX] = {.key = "profile_id", .value_type = 0},
[TAG_PRIORITY_IDX] = {.key = "priority", .value_type = 0},
[TAG_PROFILE_TYPE_IDX] = {.key = "profile_type", .value_type = 2}
};
void shaper_stat_destroy(struct shaping_stat *stat)
{
if (!stat) {
return;
}
if (stat->instance) {
fieldstat_dynamic_instance_free(stat->instance);
}
free(stat);
return;
}
static int shaper_stat_conf_load(struct shaper_stat_conf *conf)
{
memset(conf, 0, sizeof(struct shaper_stat_conf));
MESA_load_profile_string_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "LINE_PROTOCOL_SERVER_IP", conf->telegraf_ip, sizeof(conf->telegraf_ip), "127.0.0.1");
MESA_load_profile_short_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "LINE_PROTOCOL_SERVER_PORT", &conf->telegraf_port, 8200);
MESA_load_profile_int_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "FIELDSTAT_OUTPUT_INTERVAL_MS", &conf->output_interval_ms, 500);
MESA_load_profile_int_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "FIELDSTAT_ENABLE_BACKGRUND_THREAD", &conf->enable_backgroud_thread, 1);
return 0;
}
struct shaping_stat* shaper_stat_init(int thread_num)
{
struct shaping_stat *stat = NULL;
int column_num;
struct shaper_stat_conf conf;
const char *column_name[] = {"in_max_latency_us", "in_queue_len", "out_max_latency_us", "out_queue_len", //first line is gauge, second line is counter
"in_pkts", "in_bytes", "in_drop_pkts", "out_pkts", "out_bytes", "out_drop_pkts"};
enum field_type column_type[] = {FIELD_TYPE_COUNTER, FIELD_TYPE_GAUGE, FIELD_TYPE_COUNTER, FIELD_TYPE_GAUGE,
FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER};
column_num = sizeof(column_name)/sizeof(column_name[0]);
if (column_num != STAT_COLUNM_IDX_MAX) {
LOG_ERROR("%s: shaping init fieldstat failed, column_num %d != index num %d", LOG_TAG_STAT, column_num, STAT_COLUNM_IDX_MAX);
goto ERROR;
}
if (shaper_stat_conf_load(&conf) != 0) {
LOG_ERROR("%s: shaping init metric conf failed", LOG_TAG_STAT);
goto ERROR;
}
stat = (struct shaping_stat *)calloc(1, sizeof(struct shaping_stat));
stat->instance = fieldstat_dynamic_instance_new("shaping_engine", thread_num);
if (stat->instance == NULL) {
LOG_ERROR("%s: shaping init fieldstat instance failed", LOG_TAG_STAT);
goto ERROR;
}
fieldstat_dynamic_set_output_interval(stat->instance, conf.output_interval_ms);
fieldstat_dynamic_set_line_protocol_server(stat->instance, conf.telegraf_ip, conf.telegraf_port);
if (conf.enable_backgroud_thread == 0) {
fieldstat_dynamic_disable_background_thread(stat->instance);
}
stat->table_id = fieldstat_register_dynamic_table(stat->instance, "shaping_metric", column_name, column_type, column_num, stat->column_ids);
if (stat->table_id < 0) {
LOG_ERROR("%s: shaping fieldstat register table failed", LOG_TAG_STAT);
goto ERROR;
}
fieldstat_dynamic_instance_start(stat->instance);
return stat;
ERROR:
if (stat) {
if (stat->instance) {
fieldstat_dynamic_instance_free(stat->instance);
}
free(stat);
}
return NULL;
}
static void shaper_stat_tags_build(int vsys_id, int rule_id, int profile_id, int priority, int profile_type)
{
tags[TAG_VSYS_ID_IDX].value_int = vsys_id;
tags[TAG_RULE_ID_IDX].value_int = rule_id;
tags[TAG_PROFILE_ID_IDX].value_int = profile_id;
tags[TAG_PRIORITY_IDX].value_int = priority;
if (profile_type == PROFILE_IN_RULE_TYPE_PRIMARY) {
tags[TAG_PROFILE_TYPE_IDX].value_str = "primary";
} else {
tags[TAG_PROFILE_TYPE_IDX].value_str = "borrow";
}
return;
}
static void shaper_stat_swarmkv_hincrby_cb(const struct swarmkv_reply *reply, void * cb_arg)
{
struct shaping_hincrby_cb_arg *arg = (struct shaping_hincrby_cb_arg *)cb_arg;
struct shaping_thread_ctx *ctx = arg->ctx;
struct shaping_global_stat *global_stat = ctx->ref_ctx->global_stat;
struct timespec curr_time;
long long curr_time_us;
clock_gettime(CLOCK_MONOTONIC, &curr_time);
curr_time_us = curr_time.tv_sec * MICRO_SECONDS_PER_SEC + curr_time.tv_nsec / NANO_SECONDS_PER_MICRO_SEC;
shaper_global_stat_swarmkv_latency_update(global_stat, curr_time_us - arg->start_time_us);
shaper_global_stat_async_callback_inc(&ctx->thread_global_stat);
shaper_global_stat_hincrby_callback_inc(&ctx->thread_global_stat);
if (reply->type != SWARMKV_REPLY_INTEGER) {
shaper_global_stat_async_hincrby_failed_inc(&ctx->thread_global_stat);
arg->start_time_us = curr_time_us;
shaper_global_stat_async_invoke_inc(&ctx->thread_global_stat);//hincrby failed, retry
shaper_global_stat_hincrby_invoke_inc(&ctx->thread_global_stat);
LOG_DEBUG("%s: shaping stat hincrby failed, retry for profile id %d priority %d, operate queue_len %lld", LOG_TAG_STAT, arg->profile_id, arg->priority, arg->queue_len);
swarmkv_async_command(ctx->swarmkv_db, shaper_stat_swarmkv_hincrby_cb, arg, "HINCRBY tsg-shaping-%d priority-%d %lld", arg->profile_id, arg->priority, arg->queue_len);
return;
}
free(cb_arg);
return;
}
static void shaper_stat_priority_queue_len_refresh(struct shaping_thread_ctx *ctx, struct shaping_profile_hash_node *profile_hash_node, int priority, long long curr_time_us)
{
if (profile_hash_node->local_queue_len[priority] == 0) {
return;
}
if (curr_time_us - profile_hash_node->local_queue_len_update_time_us[priority] < SHAPER_STAT_REFRESH_TIME_US) {
return;
}
struct shaping_hincrby_cb_arg *arg = (struct shaping_hincrby_cb_arg *)calloc(1, sizeof(struct shaping_hincrby_cb_arg));
arg->ctx = ctx;
arg->start_time_us = curr_time_us;
arg->profile_id = profile_hash_node->id;
arg->priority = priority;
arg->queue_len = profile_hash_node->local_queue_len[priority];
shaper_global_stat_async_invoke_inc(&ctx->thread_global_stat);
shaper_global_stat_hincrby_invoke_inc(&ctx->thread_global_stat);
swarmkv_async_command(ctx->swarmkv_db, shaper_stat_swarmkv_hincrby_cb, arg, "HINCRBY tsg-shaping-%d priority-%d %lld", arg->profile_id, arg->priority, arg->queue_len);
profile_hash_node->local_queue_len_update_time_us[priority] = curr_time_us;
profile_hash_node->local_queue_len[priority] = 0;
return;
}
void shaper_stat_priority_queue_len_refresh_all(struct shaping_thread_ctx *ctx, struct shaping_profile_hash_node *profile_hash_node)
{
struct timespec curr_time;
long long curr_time_us;
clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_time);
curr_time_us = curr_time.tv_sec * MICRO_SECONDS_PER_SEC + curr_time.tv_nsec / NANO_SECONDS_PER_MICRO_SEC;
for (int i = 0; i < SHAPING_PRIORITY_NUM_MAX; i++) {
shaper_stat_priority_queue_len_refresh(ctx, profile_hash_node, i, curr_time_us);
}
return;
}
static void shaper_stat_profile_metirc_refresh(struct shaping_thread_ctx *ctx, struct shaping_rule_info *rule, struct shaping_profile_info *profile, int profile_type, int need_refresh_stat, int need_update_guage, long long curr_time_us)
{
struct shaping_stat_for_profile *profile_stat = &profile->stat;
struct shaping_stat *stat = ctx->stat;
int priority = profile->priority;
int thread_id = ctx->thread_index;
unsigned long long old_latency;
if (need_update_guage) {
profile->hash_node->local_queue_len[priority] += profile_stat->priority_queue_len;
profile_stat->priority_queue_len = 0;
shaper_stat_priority_queue_len_refresh(ctx, profile->hash_node, priority, curr_time_us);
}
if (!need_refresh_stat) {
return;
}
shaper_stat_tags_build(rule->vsys_id, rule->id, profile->id, priority, profile_type);
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_DROP_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.drop_pkts, tags, TAG_IDX_MAX, thread_id);
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.pkts, tags, TAG_IDX_MAX, thread_id);
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_BYTES_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.bytes, tags, TAG_IDX_MAX, thread_id);
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_DROP_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.drop_pkts, tags, TAG_IDX_MAX, thread_id);
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.pkts, tags, TAG_IDX_MAX, thread_id);
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_BYTES_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.bytes, tags, TAG_IDX_MAX, thread_id);
old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, tags, TAG_IDX_MAX, thread_id);
if (profile_stat->in.max_latency > old_latency) {
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.max_latency - old_latency, tags, TAG_IDX_MAX, thread_id);
}
old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, tags, TAG_IDX_MAX, thread_id);
if (profile_stat->out.max_latency > old_latency) {
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.max_latency - old_latency, tags, TAG_IDX_MAX, thread_id);
}
if (need_update_guage) {
if (profile_type == PROFILE_IN_RULE_TYPE_PRIMARY) {
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.queue_len, tags, TAG_IDX_MAX, thread_id);
fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.queue_len, tags, TAG_IDX_MAX, thread_id);
}
memset(profile_stat, 0, sizeof(struct shaping_stat_for_profile));
} else {
profile_stat->in.pkts = 0;
profile_stat->in.bytes = 0;
profile_stat->in.drop_pkts = 0;
profile_stat->in.max_latency = 0;
profile_stat->out.pkts = 0;
profile_stat->out.bytes = 0;
profile_stat->out.drop_pkts = 0;
profile_stat->out.max_latency = 0;
}
return;
}
void shaper_stat_refresh(struct shaping_thread_ctx *ctx, struct shaping_flow *sf, int force)
{
struct shaping_rule_info *rule;
struct timespec curr_time;
int need_refresh = 0;
long long curr_time_us;
clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_time);
curr_time_us = curr_time.tv_sec * MICRO_SECONDS_PER_SEC + curr_time.tv_nsec / NANO_SECONDS_PER_MICRO_SEC;
if (force) {
need_refresh = 1;
} else {
if (curr_time_us - sf->stat_update_time_us >= SHAPER_STAT_REFRESH_TIME_US) {
need_refresh = 1;
sf->stat_update_time_us = curr_time_us;
}
}
int need_update_guage = sf->processed_pkts > CONFIRM_PRIORITY_PKTS ? 1 : 0;
if (!need_refresh && !need_update_guage) {
return;
}
for (int i = 0; i < sf->rule_num; i++) {
rule = &sf->matched_rule_infos[i];
shaper_stat_profile_metirc_refresh(ctx, rule, &rule->primary, PROFILE_IN_RULE_TYPE_PRIMARY, need_refresh, need_update_guage, curr_time_us);
for (int j = 0; j < rule->borrowing_num; j++) {
shaper_stat_profile_metirc_refresh(ctx, rule, &rule->borrowing[j], PROFILE_IN_RULE_TYPE_BORROW, need_refresh, need_update_guage, curr_time_us);
}
}
return;
}
void shaper_stat_drop_inc(struct shaping_stat_for_profile *profile_stat, unsigned char direction, int thread_id)
{
if (direction == SHAPING_DIR_IN) {
profile_stat->in.drop_pkts++;
} else {
profile_stat->out.drop_pkts++;
}
return;
}
void shaper_stat_forward_inc(struct shaping_stat_for_profile *profile_stat, unsigned char direction, int pkt_len, int thread_id)
{
if (direction == SHAPING_DIR_IN) {
profile_stat->in.pkts++;
profile_stat->in.bytes += pkt_len;
} else {
profile_stat->out.pkts++;
profile_stat->out.bytes += pkt_len;
}
return;
}
void shaper_stat_forward_all_rule_inc(struct shaping_stat *stat, struct shaping_flow *sf, unsigned char direction, int pkt_len, int thread_id)
{
struct shaping_rule_info *rule;
int i;
for (i = 0; i < sf->rule_num; i++) {
rule = &sf->matched_rule_infos[i];
if (!rule->is_enabled) {
continue;
}
shaper_stat_forward_inc(&rule->primary.stat, direction, pkt_len, thread_id);
}
return;
}
void shaper_stat_queueing_pkt_inc(struct shaping_stat_for_profile *profile_stat, unsigned char direction, int thread_id)
{
if (direction == SHAPING_DIR_IN) {
profile_stat->in.queue_len++;
} else {
profile_stat->out.queue_len++;
}
profile_stat->priority_queue_len++;
return;
}
void shaper_stat_queueing_pkt_dec(struct shaping_stat_for_profile *profile_stat, unsigned char direction, int thread_id)
{
if (direction == SHAPING_DIR_IN) {
profile_stat->in.queue_len--;
} else {
profile_stat->out.queue_len--;
}
profile_stat->priority_queue_len--;
return;
}
void shaper_stat_queueing_pkt_inc_for_rule(struct shaping_rule_info *rule, unsigned char direction, int thread_id)
{
shaper_stat_queueing_pkt_inc(&rule->primary.stat, direction, thread_id);
for (int i = 0; i < rule->borrowing_num; i++) {
shaper_stat_queueing_pkt_inc(&rule->borrowing[i].stat, direction, thread_id);
}
return;
}
void shaper_stat_queueing_pkt_dec_for_rule(struct shaping_rule_info *rule, unsigned char direction, int thread_id)
{
shaper_stat_queueing_pkt_dec(&rule->primary.stat, direction, thread_id);
for (int i = 0; i < rule->borrowing_num; i++) {
shaper_stat_queueing_pkt_dec(&rule->borrowing[i].stat, direction, thread_id);
}
return;
}
void shaper_stat_max_latency_update(struct shaping_stat_for_profile *profile_stat, unsigned char direction, unsigned long long latency, int thread_id)
{
if (direction == SHAPING_DIR_IN) {
if (profile_stat->in.max_latency < latency) {
profile_stat->in.max_latency = latency;
}
} else {
if (profile_stat->out.max_latency < latency) {
profile_stat->out.max_latency = latency;
}
}
return;
}
|