-
Notifications
You must be signed in to change notification settings - Fork 2
/
dump.c
453 lines (290 loc) · 18.4 KB
/
dump.c
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright (c) 2010 Axel Neumann
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "bmx.h"
#include "msg.h"
#include "ip.h"
#include "dump.h"
#include "schedule.h"
#include "plugin.h"
#include "tools.h"
#define CODE_CATEGORY_NAME "traffic"
#define IMPROVE_ROUNDOFF 10
static int64_t curr_dump_period = DEF_DUMP_PERIOD;
static int32_t next_dump_period = DEF_DUMP_PERIOD;
static int32_t prev_dump_period = DEF_DUMP_PERIOD;
static int32_t dump_regression_exp = DEF_DUMP_REGRESSION_EXP;
static uint32_t dump_iteration = 0;
static IDM_T dump_terminating = NO;
static int32_t data_dev_plugin_registry = FAILURE;
static struct dump_data dump_all;
STATIC_FUNC
void update_traffic_statistics_data(struct dump_data *data)
{
uint16_t i, t;
for (i = 0; i < DUMP_DIRECTION_ARRSZ; i++) {
for (t = 0; t < DUMP_TYPE_ARRSZ; t++) {
data->pre_all[i][t] = (((int64_t)(data->tmp_all[i][t])) * 1000) / curr_dump_period;
data->avg_all[i][t] -= (data->avg_all[i][t] >> dump_regression_exp);
data->avg_all[i][t] += ((data->pre_all[i][t]) >> dump_regression_exp);
data->tmp_all[i][t] = 0;
}
for (t = 0; t < FRAME_TYPE_ARRSZ; t++) {
data->pre_frame[i][t] = (((int64_t)(data->tmp_frame[i][t])) * 1000) / curr_dump_period;
data->avg_frame[i][t] -= (data->avg_frame[i][t] >> dump_regression_exp);
data->avg_frame[i][t] += ((data->pre_frame[i][t]) >> dump_regression_exp);
data->tmp_frame[i][t] = 0;
}
}
}
STATIC_FUNC
void update_traffic_statistics_task(void *data)
{
struct dev_node *dev;
struct avl_node *an = NULL;
dump_iteration++;
curr_dump_period = prev_dump_period;
update_traffic_statistics_data( &dump_all );
while ((dev = avl_iterate_item(&dev_name_tree, &an))) {
struct dump_data **dump_dev_plugin_data =
(struct dump_data **) (get_plugin_data(dev, PLUGIN_DATA_DEV, data_dev_plugin_registry));
if (*dump_dev_plugin_data)
update_traffic_statistics_data(*dump_dev_plugin_data);
}
task_register(next_dump_period, update_traffic_statistics_task, NULL, -300348);
prev_dump_period = next_dump_period;
}
STATIC_FUNC
void dump(struct packet_buff *pb)
{
assertion(-500760, (XOR((pb->i.oif), (pb->i.iif))));
IDM_T direction = pb->i.iif ? DUMP_DIRECTION_IN : DUMP_DIRECTION_OUT;
struct dev_node *dev = pb->i.iif ? pb->i.iif : pb->i.oif;
struct packet_header *phdr = &pb->packet.header;
struct dump_data **dev_plugin_data =
(struct dump_data **) (get_plugin_data(dev, PLUGIN_DATA_DEV, data_dev_plugin_registry));
assertion(-500761, (*dev_plugin_data));
uint16_t plength = ntohs(phdr->pkt_length);
dbgf_dump(DBGT_NONE, "%s srcIP=%-16s dev=%-12s udpPayload=%-d",
direction == DUMP_DIRECTION_IN ? "in " : "out", pb->i.llip_str, dev->label_cfg.str, plength);
dbgf_dump(DBGT_NONE, "%s data: %s",
direction == DUMP_DIRECTION_IN ? "in " : "out", memAsHexString(((uint8_t*) phdr), plength));
dbgf_dump(DBGT_NONE, " bmx_version pkt_sqn local_id next frame...");
dbgf_dump(DBGT_NONE, " reserved link_adv_sqn dev_idx");
dbgf_dump(DBGT_NONE, " pkt_length");
dbgf_dump(DBGT_NONE, " transmitterIID");
(*dev_plugin_data)->tmp_all[direction][DUMP_TYPE_UDP_PAYLOAD] += (plength << IMPROVE_ROUNDOFF);
dump_all.tmp_all[direction][DUMP_TYPE_UDP_PAYLOAD] += (plength << IMPROVE_ROUNDOFF);
dbgf_dump(DBGT_NONE,
"%s headerVersion=%-2d reserved=%-2X IID=%-5d headerSize=%-4zu",
direction == DUMP_DIRECTION_IN ? "in " : "out",
phdr->bmx_version, phdr->reserved, ntohs(phdr->transmitterIID), sizeof (struct packet_header));
(*dev_plugin_data)->tmp_all[direction][DUMP_TYPE_PACKET_HEADER] += (sizeof (struct packet_header) << IMPROVE_ROUNDOFF);
dump_all.tmp_all[direction][DUMP_TYPE_PACKET_HEADER] += (sizeof (struct packet_header) << IMPROVE_ROUNDOFF);
struct rx_frame_iterator it = {
.caller = __func__, .on = NULL, .cn = NULL, .op = 0, .pb = NULL,
.handls = packet_frame_handler, .handl_max = FRAME_TYPE_MAX, .process_filter = FRAME_TYPE_PROCESS_NONE,
.data = ((uint8_t*) phdr), .frame_type = -1,
.frames_in = (((uint8_t*) phdr) + sizeof (struct packet_header)),
.frames_length = (plength - sizeof (struct packet_header)), .frames_pos = 0
};
int32_t iterator_result;
uint16_t frame_length = 0;
uint16_t pkt_pos = sizeof (struct packet_header);
while ((iterator_result = rx_frame_iterate(&it)) > TLV_RX_DATA_DONE) {
char tnum[4];
char *tname;
int16_t frame_msgs = -1;
if (!(tname = packet_frame_handler[it.frame_type].name)) {
sprintf(tnum, "%d", it.frame_type);
tname = tnum;
}
frame_length = it.frame_data_length +
(it.is_short_header ? sizeof (struct frame_header_short) : sizeof (struct frame_header_long));
if (packet_frame_handler[it.frame_type].fixed_msg_size) {
frame_msgs = it.frame_msgs_length / packet_frame_handler[it.frame_type].min_msg_size;
}
dbgf_dump(DBGT_NONE, "%s frame_type=%-12s data_header_size=%-3d msgs=%-3d frame_length=%-4d",
direction == DUMP_DIRECTION_IN ? "in " : "out", tname,
packet_frame_handler[it.frame_type].data_header_size, frame_msgs, frame_length);
dbgf_dump(DBGT_NONE, "%s data [%3d...%3d]:%s",
direction == DUMP_DIRECTION_IN ? "in hex" : "out hex",
pkt_pos, pkt_pos + frame_length - 1, memAsHexString(((uint8_t*) phdr) + pkt_pos, frame_length));
//assertion(-500991, (packet_frame_handler[it.frame_type].min_msg_size));
assertion(-500992, (it.frame_msgs_length >= 0));
(*dev_plugin_data)->tmp_frame[direction][it.frame_type] += (frame_length << IMPROVE_ROUNDOFF);
dump_all.tmp_frame[direction][it.frame_type] += (frame_length << IMPROVE_ROUNDOFF);
(*dev_plugin_data)->tmp_all[direction][DUMP_TYPE_FRAME_HEADER] += ((frame_length - it.frame_data_length) << IMPROVE_ROUNDOFF);
dump_all.tmp_all[direction][DUMP_TYPE_FRAME_HEADER] += ((frame_length - it.frame_data_length) << IMPROVE_ROUNDOFF);
pkt_pos += frame_length;
}
if (iterator_result != TLV_RX_DATA_DONE) {
frame_length = it.frame_data_length +
(it.is_short_header ? sizeof (struct frame_header_short) : sizeof (struct frame_header_long));
dbgf_dump(DBGT_NONE, "%s ERROR frame_type=%d frame_length=%d frame_data_lenghth=%d short_header=%d iterator_result=%d - ignoring further frames!!",
direction == DUMP_DIRECTION_IN ? "in " : "out", it.frame_type, frame_length, it.frame_data_length, it.is_short_header, iterator_result);
dbgf_dump(DBGT_NONE, "%s data [%3d...%3d]:%s",
direction == DUMP_DIRECTION_IN ? "in hex" : "out hex",
pkt_pos, pkt_pos + frame_length - 1, memAsHexString(((uint8_t*) phdr) + pkt_pos, frame_length));
}
assertion(-500990, (IMPLIES(direction == DUMP_DIRECTION_OUT, iterator_result == TLV_RX_DATA_DONE)));
}
STATIC_FUNC
void dbg_traffic_statistics(struct dump_data *data, struct ctrl_node *cn, char* dbg_name)
{
uint16_t t;
dbg_printf(cn, "%-20s \n", dbg_name);
for (t = 0; t < DUMP_TYPE_ARRSZ; t++) {
dbg_printf(cn, "%13s %5d (%3d) %5d (%3d) %5d (%3d) | %5d (%3d) %5d (%3d) %5d (%3d)\n",
t == DUMP_TYPE_UDP_PAYLOAD ? "UDP_PAYLOAD" : (t == DUMP_TYPE_PACKET_HEADER ? "PACKET_HEADER" : "FRAME_HEADER"),
((data->pre_all[DUMP_DIRECTION_IN][t] + data->pre_all[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
((((data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD]) || (data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD])) ?
(((data->pre_all[DUMP_DIRECTION_IN][t] + data->pre_all[DUMP_DIRECTION_OUT][t])*100) /
(data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] + data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD])) : 0)),
((data->pre_all[DUMP_DIRECTION_IN][t]) >> IMPROVE_ROUNDOFF),
((data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] ?
((data->pre_all[DUMP_DIRECTION_IN][t]*100) / data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD]) : 0)),
((data->pre_all[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
((data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD] ?
((data->pre_all[DUMP_DIRECTION_OUT][t]*100) / data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD]) : 0)),
((data->avg_all[DUMP_DIRECTION_IN][t] + data->avg_all[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
(((data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] || data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD]) ?
(((data->avg_all[DUMP_DIRECTION_IN][t] + data->avg_all[DUMP_DIRECTION_OUT][t])*100) /
(data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] + data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD])) : 0)),
((data->avg_all[DUMP_DIRECTION_IN][t]) >> IMPROVE_ROUNDOFF),
((data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] ?
((data->avg_all[DUMP_DIRECTION_IN][t]*100) / data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD]) : 0)),
((data->avg_all[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
((data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD] ?
((data->avg_all[DUMP_DIRECTION_OUT][t]*100) / data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD]) : 0))
);
}
for (t = 0; t < FRAME_TYPE_NOP; t++) {
if (packet_frame_handler[t].name || data->avg_frame[DUMP_DIRECTION_IN][t] || data->avg_frame[DUMP_DIRECTION_OUT][t]) {
char tnum[4];
char *tname = packet_frame_handler[t].name;
if (!tname) {
sprintf(tnum, "%d", t);
tname = tnum;
}
dbg_printf(cn, "%13s %5d (%3d) %5d (%3d) %5d (%3d) | %5d (%3d) %5d (%3d) %5d (%3d)\n",
tname,
((data->pre_frame[DUMP_DIRECTION_IN][t] + data->pre_frame[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
((data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] || data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD] ?
(((data->pre_frame[DUMP_DIRECTION_IN][t] + data->pre_frame[DUMP_DIRECTION_OUT][t])*100) /
(data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] + data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD])) : 0)),
((data->pre_frame[DUMP_DIRECTION_IN][t]) >> IMPROVE_ROUNDOFF),
((data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] ?
((data->pre_frame[DUMP_DIRECTION_IN][t]*100) / data->pre_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD]) : 0)),
((data->pre_frame[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
((data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD] ?
((data->pre_frame[DUMP_DIRECTION_OUT][t]*100) / data->pre_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD]) : 0)),
((data->avg_frame[DUMP_DIRECTION_IN][t] + data->avg_frame[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
((data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] || data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD] ?
(((data->avg_frame[DUMP_DIRECTION_IN][t] + data->avg_frame[DUMP_DIRECTION_OUT][t])*100) /
(data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] + data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD])) : 0)),
((data->avg_frame[DUMP_DIRECTION_IN][t]) >> IMPROVE_ROUNDOFF),
((data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD] ?
((data->avg_frame[DUMP_DIRECTION_IN][t]*100) / data->avg_all[DUMP_DIRECTION_IN][DUMP_TYPE_UDP_PAYLOAD]) : 0)),
((data->avg_frame[DUMP_DIRECTION_OUT][t]) >> IMPROVE_ROUNDOFF),
((data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD] ?
((data->avg_frame[DUMP_DIRECTION_OUT][t]*100) / data->avg_all[DUMP_DIRECTION_OUT][DUMP_TYPE_UDP_PAYLOAD]) : 0))
);
}
}
}
STATIC_FUNC
int32_t opt_traffic_statistics(uint8_t cmd, uint8_t _save, struct opt_type *opt, struct opt_parent *patch, struct ctrl_node *cn)
{
if ( cmd == OPT_APPLY ) {
struct dev_node *dev;
struct avl_node *an = NULL;
dbg_printf(cn, "iteration=%d as [Bytes/sec], averaged over %.1f secs and as weighted averages\n",
dump_iteration, (float)curr_dump_period / 1000);
dbg_printf(cn, "%20s ( %% ) in ( %% ) out ( %% ) | all ( %% ) in ( %% ) out ( %% )\n"," ");
if (!strcmp(patch->val, ARG_DUMP_ALL) || !strcmp(patch->val, ARG_DUMP_SUMMARY))
dbg_traffic_statistics(&dump_all, cn, ARG_DUMP_ALL);
while ((dev = avl_iterate_item(&dev_name_tree, &an))) {
if (!strcmp(patch->val, ARG_DUMP_ALL) || !strcmp(patch->val, dev->label_cfg.str)) {
struct dump_data **dump_dev_plugin_data = (struct dump_data **)
(get_plugin_data(dev, PLUGIN_DATA_DEV, data_dev_plugin_registry));
if (dev->active && *dump_dev_plugin_data)
dbg_traffic_statistics(*dump_dev_plugin_data, cn, dev->label_cfg.str);
}
}
dbg_printf(cn, "\n");
}
return SUCCESS;
}
STATIC_FUNC
struct opt_type dump_options[]=
{
// ord parent long_name shrt Attributes *ival min max default *func,*syntax,*help
{ODI, 0, ARG_DUMP_PERIOD, 0, 9,1, A_PS1, A_ADM, A_DYI, A_ARG, A_ANY, &next_dump_period, MIN_DUMP_PERIOD, MAX_DUMP_PERIOD, DEF_DUMP_PERIOD,0, 0,
ARG_VALUE_FORM, "set duration in ms for how long traffic is measured for calculating interval averages"}
,
{ODI, 0, ARG_DUMP_REGRESSION_EXP, 0, 9,1, A_PS1, A_ADM, A_DYI, A_ARG, A_ANY, &dump_regression_exp,MIN_DUMP_REGRESSION_EXP,MAX_DUMP_REGRESSION_EXP,DEF_DUMP_REGRESSION_EXP,0,0,
ARG_VALUE_FORM, "set regression exponent for traffic-dump statistics "}
,
{ODI, 0, ARG_DUMP, 0, 9,2, A_PS1, A_USR, A_DYN, A_ARG, A_ANY, 0, 0, 0, 0,0, opt_traffic_statistics,
"<DEV>", "show traffic statistics for given device name, summary, or all\n"}
};
STATIC_FUNC
void init_cleanup_dev_traffic_data(int32_t cb_id, void* devp)
{
struct dev_node *dev;
struct avl_node *an;
for (an = NULL; (dev = avl_iterate_item(&dev_name_tree, &an));) {
struct dump_data **dump_dev_plugin_data = (struct dump_data **) (get_plugin_data(dev, PLUGIN_DATA_DEV, data_dev_plugin_registry));
if (dump_terminating || (!dev->active && *dump_dev_plugin_data)) {
if (*dump_dev_plugin_data)
debugFree(*dump_dev_plugin_data, -300305);
*dump_dev_plugin_data = NULL;
} else if (dev->active && !(*dump_dev_plugin_data)) {
*dump_dev_plugin_data = debugMallocReset(sizeof (struct dump_data), -300306);
}
}
}
STATIC_FUNC
void cleanup_dump( void )
{
dump_terminating = YES;
init_cleanup_dev_traffic_data(0, NULL);
set_packet_hook(dump, DEL);
}
STATIC_FUNC
int32_t init_dump( void )
{
memset(&dump_all, 0, sizeof (struct dump_data));
data_dev_plugin_registry = get_plugin_data_registry(PLUGIN_DATA_DEV);
register_options_array(dump_options, sizeof ( dump_options), CODE_CATEGORY_NAME);
task_register(next_dump_period, update_traffic_statistics_task, NULL, -300349);
set_packet_hook(dump, ADD);
return SUCCESS;
}
struct plugin *dump_get_plugin( void ) {
static struct plugin dump_plugin;
memset( &dump_plugin, 0, sizeof ( struct plugin ) );
dump_plugin.plugin_name = CODE_CATEGORY_NAME;
dump_plugin.plugin_size = sizeof ( struct plugin );
dump_plugin.cb_init = init_dump;
dump_plugin.cb_cleanup = cleanup_dump;
dump_plugin.cb_plugin_handler[PLUGIN_CB_BMX_DEV_EVENT] = init_cleanup_dev_traffic_data;
return &dump_plugin;
}