-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump-classifier.c
549 lines (438 loc) · 10.7 KB
/
dump-classifier.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* Copyright (c) 2018, Intel Corporation
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include <argp.h>
#include <inttypes.h>
#include <pcap.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NSEC_TO_SEC 1e9
#define NUM_FILTERS 8
#define NUM_ENTRIES 64
#define MAX_ARGS 100
/* TAPRIO */
enum {
TC_TAPRIO_CMD_SET_GATES = 0x00,
TC_TAPRIO_CMD_SET_AND_HOLD = 0x01,
TC_TAPRIO_CMD_SET_AND_RELEASE = 0x02,
};
#define NEXT_ARG() \
do { \
argv++; \
if (--argc <= 0) { \
fprintf(stderr, "Incomplete command\n"); \
exit(-1); \
} \
} while (0)
enum traffic_flags {
TRAFFIC_FLAGS_TXTIME,
};
struct tc_filter {
char *name;
struct bpf_program prog;
unsigned int flags;
};
struct sched_entry {
uint8_t command;
uint32_t gatemask;
uint32_t interval;
};
struct schedule {
struct sched_entry entries[NUM_ENTRIES];
int64_t base_time;
size_t current_entry;
size_t num_entries;
int64_t cycle_time;
};
static struct argp_option options[] = {
{"batch-file", 's', "BATCH_FILE", 0, "File containing the taprio configuration" },
{"dump-file", 'd', "DUMP_FILE", 0, "File containing the tcpdump dump" },
{"filters-file", 'f', "FILTERS_FILE", 0, "File containing the classfication filters" },
{ 0 }
};
static struct tc_filter traffic_filters[NUM_FILTERS];
static FILE *batch_file, *dump_file, *filters_file;
static struct schedule schedule;
static error_t parser(int key, char *arg, struct argp_state *state)
{
switch (key) {
case 's':
batch_file = fopen(arg, "r");
if (!batch_file) {
perror("Could not open file, fopen");
exit(EXIT_FAILURE);
}
break;
case 'd':
dump_file = fopen(arg, "r");
if (!dump_file) {
perror("Could not open file, fopen");
exit(EXIT_FAILURE);
}
break;
case 'f':
filters_file = fopen(arg, "r");
if (!filters_file) {
perror("Could not open file, fopen");
exit(EXIT_FAILURE);
}
break;
}
return 0;
}
static struct argp argp = { options, parser };
static void usage(void)
{
fprintf(stderr, "dump-classifier -s <tc batch file> -d <dump-file> -f <filters-file>\n");
}
/* split command line into argument vector */
int makeargs(char *line, char *argv[], int max_args)
{
static const char ws[] = " \t\r\n";
char *cp = line;
int argc = 0;
while (*cp) {
/* skip leading whitespace */
cp += strspn(cp, ws);
if (*cp == '\0')
break;
if (argc >= (max_args - 1))
return -1;
/* word begins with quote */
if (*cp == '\'' || *cp == '"') {
char quote = *cp++;
argv[argc++] = cp;
/* find ending quote */
cp = strchr(cp, quote);
if (cp == NULL) {
fprintf(stderr, "Unterminated quoted string\n");
exit(1);
}
} else {
argv[argc++] = cp;
/* find end of word */
cp += strcspn(cp, ws);
if (*cp == '\0')
break;
}
/* seperate words */
*cp++ = 0;
}
argv[argc] = NULL;
return argc;
}
/* Like glibc getline but handle continuation lines and comments */
ssize_t getcmdline(char **linep, size_t *lenp, FILE *in)
{
ssize_t cc;
char *cp;
cc = getline(linep, lenp, in);
if (cc < 0)
return cc; /* eof or error */
cp = strchr(*linep, '#');
if (cp)
*cp = '\0';
while ((cp = strstr(*linep, "\\\n")) != NULL) {
char *line1 = NULL;
size_t len1 = 0;
ssize_t cc1;
cc1 = getline(&line1, &len1, in);
if (cc1 < 0) {
fprintf(stderr, "Missing continuation line\n");
return cc1;
}
*cp = 0;
cp = strchr(line1, '#');
if (cp)
*cp = '\0';
*lenp = strlen(*linep) + strlen(line1) + 1;
*linep = realloc(*linep, *lenp);
if (!*linep) {
fprintf(stderr, "Out of memory\n");
*lenp = 0;
return -1;
}
cc += cc1 - 2;
strcat(*linep, line1);
free(line1);
}
return cc;
}
static int parse_filters(pcap_t *handle, FILE *file,
struct tc_filter *filters, size_t num_filters)
{
char *name, *expression;
size_t i = 0;
int err;
while (i < num_filters && fscanf(file, "%ms :: %m[^\n]s\n",
&name, &expression) != EOF) {
struct tc_filter *filter = &filters[i];
filter->name = name;
err = pcap_compile(handle, &filter->prog, expression,
1, PCAP_NETMASK_UNKNOWN);
if (err < 0) {
pcap_perror(handle, "pcap_compile");
return -EINVAL;
}
i++;
}
return i;
}
static int str_to_entry_cmd(const char *str)
{
if (strcmp(str, "S") == 0)
return TC_TAPRIO_CMD_SET_GATES;
if (strcmp(str, "H") == 0)
return TC_TAPRIO_CMD_SET_AND_HOLD;
if (strcmp(str, "R") == 0)
return TC_TAPRIO_CMD_SET_AND_RELEASE;
return -1;
}
int get_u32(uint32_t *val, const char *arg, int base)
{
unsigned long res;
char *ptr;
if (!arg || !*arg)
return -1;
res = strtoul(arg, &ptr, base);
/* empty string or trailing non-digits */
if (!ptr || ptr == arg || *ptr)
return -1;
/* overflow */
if (res == ULONG_MAX && errno == ERANGE)
return -1;
/* in case UL > 32 bits */
if (res > 0xFFFFFFFFUL)
return -1;
*val = res;
return 0;
}
int get_s64(int64_t *val, const char *arg, int base)
{
long res;
char *ptr;
errno = 0;
if (!arg || !*arg)
return -1;
res = strtoll(arg, &ptr, base);
if (!ptr || ptr == arg || *ptr)
return -1;
if ((res == LLONG_MIN || res == LLONG_MAX) && errno == ERANGE)
return -1;
if (res > INT64_MAX || res < INT64_MIN)
return -1;
*val = res;
return 0;
}
static int parse_batch_file(FILE *file, struct schedule *schedule, size_t max_entries)
{
int argc;
char *arguments[MAX_ARGS];
char **argv;
size_t len;
char *line = NULL;
int err;
if (getcmdline(&line, &len, file) < 0) {
fprintf(stderr, "Could not read batch file\n");
exit(EXIT_FAILURE);
}
argc = makeargs(line, arguments, MAX_ARGS);
if (argc < 0) {
fprintf(stderr, "Could not parse arguments\n");
return -1;
}
argv = arguments;
while (argc > 0) {
if (strcmp(*argv, "sched-entry") == 0) {
struct sched_entry *e;
if (schedule->num_entries >= max_entries) {
fprintf(stderr, "The maximum number of schedule entries is %zu\n", max_entries);
return -1;
}
e = &schedule->entries[schedule->num_entries];
NEXT_ARG();
err = str_to_entry_cmd(*argv);
if (err < 0) {
fprintf(stderr, "Could not parse command (found %s)\n", *argv);
return -1;
}
e->command = err;
NEXT_ARG();
if (get_u32(&e->gatemask, *argv, 16)) {
fprintf(stderr, "Could not parse gatemask (found %s)\n", *argv);
return -1;
}
NEXT_ARG();
if (get_u32(&e->interval, *argv, 0)) {
fprintf(stderr, "Could not parse interval (found %s)\n", *argv);
return -1;
}
schedule->num_entries++;
} else if (strcmp(*argv, "base-time") == 0) {
NEXT_ARG();
if (get_s64(&schedule->base_time, *argv, 10)) {
fprintf(stderr, "Could not parse base-time (found %s)\n", *argv);
return -1;
}
}
argc--; argv++;
}
return 0;
}
/* libpcap re-uses the timeval struct for nanosecond resolution when
* PCAP_TSTAMP_PRECISION_NANO is specified.
*/
static uint64_t tv_to_nanos(const struct timeval *tv)
{
return tv->tv_sec * NSEC_TO_SEC + tv->tv_usec;
}
static struct sched_entry *next_entry(struct schedule *schedule)
{
schedule->current_entry++;
if (schedule->current_entry >= schedule->num_entries)
schedule->current_entry = 0;
return &schedule->entries[schedule->current_entry];
}
static struct sched_entry *first_entry(struct schedule *schedule)
{
schedule->current_entry = 0;
return &schedule->entries[0];
}
static struct sched_entry *advance_until(struct schedule *schedule,
uint64_t ts, uint64_t *now)
{
struct sched_entry *first, *entry;
uint64_t cycle = 0;
uint64_t n;
entry = first = first_entry(schedule);
if (!schedule->cycle_time) {
do {
cycle += entry->interval;
entry = next_entry(schedule);
} while (entry != first);
schedule->cycle_time = cycle;
}
cycle = schedule->cycle_time;
n = (ts - schedule->base_time) / cycle;
*now = schedule->base_time + (n * cycle);
do {
if (*now + entry->interval > ts)
break;
*now += entry->interval;
entry = next_entry(schedule);
} while (true);
return entry;
}
static int match_packet(const struct tc_filter *filters, int num_filters,
const struct pcap_pkthdr *hdr,
const uint8_t *frame)
{
int err;
int i;
for (i = 0; i < num_filters; i++) {
const struct tc_filter *f = &filters[i];
err = pcap_offline_filter(&f->prog, hdr, frame);
if (!err) {
/* The filter for traffic class 'i' doesn't
* match the packet
*/
continue;
}
return i;
}
/* returning 'num_filters' means that the packet matches none
* of the filters, so it's a Best Effort packet.
*/
return num_filters;
}
static int classify_frames(pcap_t *handle, const struct tc_filter *tc_filters,
int num_filters, struct schedule *schedule)
{
struct sched_entry *entry;
struct pcap_pkthdr *hdr;
const uint8_t *frame;
uint64_t now, ts;
int err;
now = schedule->base_time;
/* Ignore frames until we get to the base_time of the
* schedule. */
do {
err = pcap_next_ex(handle, &hdr, &frame);
if (err < 0) {
pcap_perror(handle, "pcap_next_ex");
return -EINVAL;
}
ts = tv_to_nanos(&hdr->ts);
} while (ts <= now);
do {
const char *name, *ontime;
int64_t offset;
int tc;
ts = tv_to_nanos(&hdr->ts);
entry = advance_until(schedule, ts, &now);
tc = match_packet(tc_filters, num_filters, hdr, frame);
if (tc < num_filters)
name = tc_filters[tc].name;
else
name = "BE";
if (entry->gatemask & (1 << tc))
ontime = "ontime";
else
ontime = "late";
offset = ts - now;
/* XXX: what more information might we need? */
printf("%" PRIu64 " %" PRIu64 " \"%s\" \"%s\" %" PRId64 " %#x\n",
ts, now, name, ontime, offset, entry->gatemask);
} while (pcap_next_ex(handle, &hdr, &frame) >= 0);
return 0;
}
static void free_filters(struct tc_filter *filters, int num_filters)
{
int i;
for (i = 0; i < num_filters; i++) {
struct tc_filter *f = &filters[i];
free(f->name);
}
}
int main(int argc, char **argv)
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
int err, num;
argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (!dump_file || !batch_file || !filters_file) {
usage();
exit(EXIT_FAILURE);
}
err = parse_batch_file(batch_file, &schedule, NUM_ENTRIES);
if (err < 0) {
fprintf(stderr, "Could not parse schedule file (or file empty)\n");
exit(EXIT_FAILURE);
}
handle = pcap_fopen_offline_with_tstamp_precision(
dump_file, PCAP_TSTAMP_PRECISION_NANO, errbuf);
if (!handle) {
fprintf(stderr, "Could not parse dump file\n");
exit(EXIT_FAILURE);
}
num = parse_filters(handle, filters_file,
traffic_filters, NUM_FILTERS);
if (err < 0) {
fprintf(stderr, "Could not filters file\n");
exit(EXIT_FAILURE);
}
err = classify_frames(handle, traffic_filters, num, &schedule);
if (err < 0) {
fprintf(stderr, "Could not classify frames\n");
exit(EXIT_FAILURE);
}
free_filters(traffic_filters, num);
pcap_close(handle);
return 0;
}