File: trace.c

package info (click to toggle)
tcng 10b-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,644 kB
  • sloc: ansic: 19,040; pascal: 4,640; yacc: 2,619; sh: 1,914; perl: 1,546; lex: 772; makefile: 751
file content (562 lines) | stat: -rw-r--r-- 14,775 bytes parent folder | download | duplicates (4)
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
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * trace.c - Tracing functions
 *
 * Written 2001-2004 by Werner Almesberger
 * Copyright 2001 EPFL-ICA, Network Robots
 * Copyright 2002 Bivio Networks, Werner Almesberger
 * Copyright 2003,2004 Werner Almesberger
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <memutil.h>

#include "tcsim.h"
#include "tckernel.h"
#include "timer.h"

#include <linux/config.h>
#include <asm/system.h> /* for local_bh_*able */
#include <net/pkt_sched.h>
#include <net/pkt_cls.h>


extern int do_tcf_police(struct sk_buff *skb,struct tcf_police *p);
extern int do_register_qdisc(struct Qdisc_ops *qops);
extern int do_register_tcf_proto_ops(struct tcf_proto_ops *ops);
extern struct Qdisc_ops pfifo_fast_ops;
extern struct Qdisc_ops ingress_qdisc_ops;


static int level = 0;
static int ingress_num; /* ingress entry */

static struct tcf_proto_ops orig_cls_ops[100];
static struct Qdisc_ops orig_sch_ops[100];


#if KVERSIONNUM >= 0x20416	/* gratuitous interface change in 2.4.22 :-( */
#define SCH_DROP_UNSIGNED unsigned
#else
#define SCH_DROP_UNSIGNED
#endif


/* ---------------------- Classification and policing ---------------------- */


static const char *police_res(int ret)
{
    static char buf[20];

    switch (ret) {
	case TC_POLICE_UNSPEC:
	    return "UNSPEC (-1)";
	case TC_POLICE_OK:
	    return "OK (0)";
	case TC_POLICE_RECLASSIFY:
	    return "RECLASSIFY (1)";
	case TC_POLICE_SHOT:
	    return "SHOT (2)";
	default:
	    sprintf(buf,"%d",ret);
	    return buf;
    }
}


/*
 * Most of this code comes from net/sched/police.c
 */


#define L2T(t,L)   ((t)->data[(L)>>(t)->rate.cell_log])


int tcf_police(struct sk_buff *skb,struct tcf_police *p)
{
    int ret;

    if (verbose > 2) {
	print_time(now);
	printf(" p : %s %d : <%d>",print_skb(skb),skb->len,level);
	if (p->ewma_rate)
	    printf(" ewma %lu >? %lu",(unsigned long) p->stats.bps,
	      (unsigned long) p->ewma_rate);
	if (p->R_tab) {
	    psched_time_t now;
	    long toks;

	    PSCHED_GET_TIME(now);
	    toks = PSCHED_TDIFF_SAFE(now,p->t_c,p->burst,0);
	    if (p->P_tab) {
		long ptoks = toks+p->ptoks;

		if (ptoks > L2T(p->P_tab,p->mtu)) ptoks = L2T(p->P_tab,p->mtu);
		printf(" peak %ld >? %ld",
		  (unsigned long) L2T(p->P_tab,skb->len),(unsigned long) ptoks);
	    }
	    toks += p->toks;
	    if (toks > p->burst) toks = p->burst;
	    printf(" rate %ld >? %ld",(unsigned long) L2T(p->R_tab,skb->len),
	      (unsigned long) toks);
	    
	}
	printf(" mtu %lu\n",(unsigned long) p->mtu);
    }
    ret = do_tcf_police(skb,p);
    if (verbose > 0) {
	print_time(now);
	printf(" p : %s %d : <%d> returns %s\n",print_skb(skb),skb->len,level,
	  police_res(ret));
    }
    return ret;
}


void trace_u32_match(struct sk_buff *skb,u32 handle,int pos,int off2);
  /* extern in klib */

void trace_u32_match(struct sk_buff *skb,u32 handle,int pos,int off2)
{
    if (verbose > 2) {
	print_time(now);
	printf(" c : %s %d : <%d> u32 handle %x:%x:%x match "
	  "skb->nh.raw+%d(+%d)\n",print_skb(skb),skb->len,level,
	  TC_U32_USERHTID(handle),TC_U32_HASH(handle),TC_U32_NODE(handle),pos,
	  off2);
    }
}


void trace_u32_offset(struct sk_buff *skb,u32 handle,int pos,u16 mask);
  /* extern in klib */

void trace_u32_offset(struct sk_buff *skb,u32 handle,int pos,u16 mask)
{
    if (verbose > 2) {
	print_time(now);
	printf(" c : %s %d : <%d> u32 handle %x:%x:%x offset "
	  "skb->nh.raw+%d & 0x%x\n",print_skb(skb),skb->len,level,
	  TC_U32_USERHTID(handle),TC_U32_HASH(handle),TC_U32_NODE(handle),pos,
	  (unsigned) mask);
    }
}


static int classify_wrapper(int n, struct sk_buff *skb, struct tcf_proto *tp,
  struct tcf_result *res)
{
    int ret;

    if (verbose > 1) {
	print_time(now);
	printf(" c : %s %d : <%d> calling %s at %x:%x, prio 0x%08lx\n",
	  print_skb(skb),skb->len,level,tp->ops->kind,
	  (int) (TC_H_MAJ(tp->classid) >> 16),
	  (int) TC_H_MIN(tp->classid),(unsigned long) tp->prio);
    }
    level++;
    ret = orig_cls_ops[n].classify(skb,tp,res);
    level--;
    if (verbose > 0) {
	print_time(now);
	printf(" c : %s %d : <%d> %s at %x:%x returns %s",print_skb(skb),
	  skb->len,level,tp->ops->kind,(int) (TC_H_MAJ(tp->classid) >> 16),
	  (int) TC_H_MIN(tp->classid),police_res(ret));
	if (ret != TC_POLICE_OK && ret != TC_POLICE_RECLASSIFY)
	    putchar('\n');
	else printf(" (%x:%x, 0x%lu)\n",(int) (TC_H_MAJ(res->classid) >> 16),
	      (int) TC_H_MIN(res->classid),(unsigned long) res->class);
    }
    return ret;
}


/* ------------------ Enqueuing, dequeuing, and dropping ------------------- */


const char *enqueue_res(int ret)
{
    static char buf[20];

    switch (ret) {
	case NET_XMIT_SUCCESS:
	    return "SUCCESS (0)";
	case NET_XMIT_DROP:
	    return "DROP (1)";
	case NET_XMIT_CN:
	    return "CN (2)";
	case NET_XMIT_POLICED:
	    return "POLICED (3)";
	case NET_XMIT_BYPASS:
	    return "BYPASS (4)";
	default:
	    sprintf(buf,"%d",ret);
	    return buf;
    }
}


const char *ingress_res(int ret)
{
    static char buf[20];

    switch (ret) {
	case NF_DROP:
	    return "NF_DROP (0)";
	case NF_ACCEPT:
	    return "NF_ACCEPT (1)";
	default:
	    sprintf(buf,"%d",ret);
	    return buf;
    }
}


static struct reshape_entry {
    struct Qdisc *q;
    int (*fn)(struct sk_buff *skb,struct Qdisc *q);
    struct reshape_entry *next;
} *reshape_entries = NULL;


static void reshape_add(struct Qdisc *q,
  int (*fn)(struct sk_buff *skb,struct Qdisc *q))
{
    struct reshape_entry **walk;

    for (walk = &reshape_entries; *walk; walk = &(*walk)->next)
	if ((*walk)->q == q) break;
    if (!*walk) {
	*walk = alloc_t(struct reshape_entry);
	(*walk)->q = q;
	(*walk)->next = NULL;
    }
    (*walk)->fn = fn;
}
  

static int (*reshape_lookup(struct Qdisc *q))(struct sk_buff *,struct Qdisc *)
{
    struct reshape_entry *walk;

    for (walk = reshape_entries; walk->q != q; walk = walk->next);
    return walk->fn;
}


static int reshape_wrapper(struct sk_buff *skb,struct Qdisc *q)
{
    int ret;

    if (verbose > 1) {
	print_time(now);
	printf(" s : %s %d : <%d> calling\n",print_skb(skb),skb->len,level);
    }
    level++;;
    ret = reshape_lookup(q)(skb,q);
    level--;
    if (verbose > 0) {
	print_time(now);
	printf(" s : %s %d : <%d> returns %d (%s)\n",print_skb(skb),skb->len,
	  level,ret,ret ? "discard" : "enqueued");
    }
    return ret;
}


static int enqueue_wrapper(int n,struct sk_buff *skb,struct Qdisc *q)
{
    int ret;
    int len = skb->len; /* enqueue may kfree the skb */
    unsigned long skb_id;
    int (*orig_reshape_fail)(struct sk_buff *skb,struct Qdisc *q) = NULL;

    skb_id = get_skb_id(skb);
    if (verbose > 1) {
	print_time(now);
	printf(" e : %s %d : <%d> calling %s (%x:%x)\n",print_skb_id(skb_id),
	  skb->len,level,q->ops->id,(int) (TC_H_MAJ(q->handle) >> 16),
	  (int) TC_H_MIN(q->handle));
    }
    if (q->reshape_fail) {
	orig_reshape_fail = q->reshape_fail;
	reshape_add(q,q->reshape_fail);
	q->reshape_fail = reshape_wrapper;
    }
    level++;;
    ret = orig_sch_ops[n].enqueue(skb,q);
    level--;
    if (q->reshape_fail) q->reshape_fail = orig_reshape_fail;
    if (verbose > 0) {
	print_time(now);
	printf(" e : %s %d : <%d> %s (%x:%x) returns %s\n",print_skb_id(skb_id),
	  len,level,q->ops->id,(int) (TC_H_MAJ(q->handle) >> 16),
	  (int) TC_H_MIN(q->handle),enqueue_res(ret));
    }
    return ret;
}


static int ingress_wrapper(struct sk_buff *skb,struct Qdisc *q)
{
    int ret;
    int len = skb->len; /* enqueue may kfree the skb */
    unsigned long skb_id;

    skb_id = get_skb_id(skb);
    if (verbose > 1) {
	print_time(now);
	printf(" i : %s %d : <%d> calling %s (%x:%x)\n",print_skb_id(skb_id),
	  skb->len,level,q->ops->id,(int) (TC_H_MAJ(q->handle) >> 16),
	  (int) TC_H_MIN(q->handle));
    }
    level++;;
    ret = orig_sch_ops[ingress_num].enqueue(skb,q);
    level--;
    if (verbose > 0) {
	print_time(now);
	printf(" i : %s %d : <%d> %s (%x:%x) returns %s\n",print_skb_id(skb_id),
	  len,level,q->ops->id,(int) (TC_H_MAJ(q->handle) >> 16),
	  (int) TC_H_MIN(q->handle),ingress_res(ret));
    }
    return ret;
}


static struct sk_buff *dequeue_wrapper(int n,struct Qdisc *q)
{
    struct sk_buff *skb;

    if (verbose > 1) {
	print_time(now);
	printf(" d : 0x0 0 : <%d> calling %s (%x:%x)\n",level,q->ops->id,
	  (int) (TC_H_MAJ(q->handle) >> 16),(int) TC_H_MIN(q->handle));
    }
    level++;
    skb = orig_sch_ops[n].dequeue(q);
    level--;
    if (verbose > 1 && !skb) {
	print_time(now);
	printf(" d : 0x0 0 : <%d> %s (%x:%x)\n",level,q->ops->id,
	  (int) (TC_H_MAJ(q->handle) >> 16),(int) TC_H_MIN(q->handle));
    }
    if (skb && verbose > 0) {
	print_time(now);
	printf(" d : %s %d : <%d> %s (%x:%x)\n",print_skb(skb),skb->len,level,
	  q->ops->id,(int) (TC_H_MAJ(q->handle) >> 16),
	  (int) TC_H_MIN(q->handle));
    }
    return skb;
}


static int requeue_wrapper(int n,struct sk_buff *skb,struct Qdisc *q)
{
    int ret;
    int len = skb->len; /* requeue may kfree the skb */
    unsigned long skb_id;

    skb_id = get_skb_id(skb);
    if (verbose > 1) {
	print_time(now);
	printf(" r : %s %d : <%d> calling %s (%x:%x)\n",print_skb_id(skb_id),
	  skb->len,level,q->ops->id,(int) (TC_H_MAJ(q->handle) >> 16),
	  (int) TC_H_MIN(q->handle));
    }
    level++;;
    ret = orig_sch_ops[n].requeue(skb,q);
    level--;
    if (verbose > 0) {
	print_time(now);
	printf(" r : %s %d : <%d> %s (%x:%x) returns %s\n",
	  print_skb_id(skb_id),len,level,q->ops->id,
	  (int) (TC_H_MAJ(q->handle) >> 16),(int) TC_H_MIN(q->handle),
	  enqueue_res(ret));
    }
    return ret;
}


static SCH_DROP_UNSIGNED int drop_wrapper(int n,struct Qdisc *q)
{
    int ret;

    if (verbose > 1) {
	print_time(now);
	printf(" x : 0x0 0 : <%d> calling %s (%x:%x)\n",level,q->ops->id,
	  (int) (TC_H_MAJ(q->handle) >> 16),(int) TC_H_MIN(q->handle));
    }
    level++;
    ret = orig_sch_ops[n].drop(q);
    level--;
    if (verbose > 0) {
	print_time(now);
	printf(" x : 0x0 0 : <%d> %s (%x:%x) returns %d (%sdropped)\n",level,
	  q->ops->id,(int) (TC_H_MAJ(q->handle) >> 16),
	  (int) TC_H_MIN(q->handle),ret,ret ? "" : "not ");
    }
    return ret;
}


/* -------------------------- Wrapper repository --------------------------- */


/*
 * Such a large number of wrappers may seem excessive at the moment, but once
 * we get custom-built elements, each interface may have a few "private"
 * elements, so large simulations may indeed need lots of wrappers.
 */

#define NUM_CLS_WRAPPERS 100
#define NUM_SCH_WRAPPERS 100


#define CLS_WRAPPERS(nn) \
  static int classify_wrapper_##nn(struct sk_buff *skb,struct tcf_proto *tp, \
    struct tcf_result *res) \
  { \
    return classify_wrapper(1##nn-100,skb,tp,res); \
  }

#define CLS_WRAPPERS_10(n) \
  CLS_WRAPPERS(n##0) CLS_WRAPPERS(n##1) CLS_WRAPPERS(n##2) CLS_WRAPPERS(n##3) \
  CLS_WRAPPERS(n##4) CLS_WRAPPERS(n##5) CLS_WRAPPERS(n##6) CLS_WRAPPERS(n##7) \
  CLS_WRAPPERS(n##8) CLS_WRAPPERS(n##9)

#define CLS_WRAPPERS_100 \
  CLS_WRAPPERS_10(0) CLS_WRAPPERS_10(1) CLS_WRAPPERS_10(2) CLS_WRAPPERS_10(3) \
  CLS_WRAPPERS_10(4) CLS_WRAPPERS_10(5) CLS_WRAPPERS_10(6) CLS_WRAPPERS_10(7) \
  CLS_WRAPPERS_10(8) CLS_WRAPPERS_10(9)

CLS_WRAPPERS_100

#define SCH_WRAPPERS(nn) \
  static int enqueue_wrapper_##nn(struct sk_buff *skb,struct Qdisc *q) \
  { \
    return enqueue_wrapper(1##nn-100,skb,q); \
  } \
  static struct sk_buff *dequeue_wrapper_##nn(struct Qdisc *q) \
  { \
    return dequeue_wrapper(1##nn-100,q); \
  } \
  static int requeue_wrapper_##nn(struct sk_buff *skb,struct Qdisc *q) \
  { \
    return requeue_wrapper(1##nn-100,skb,q); \
  } \
  static SCH_DROP_UNSIGNED int drop_wrapper_##nn(struct Qdisc *q) \
  { \
    return drop_wrapper(1##nn-100,q); \
  }

#define SCH_WRAPPERS_10(n) \
  SCH_WRAPPERS(n##0) SCH_WRAPPERS(n##1) SCH_WRAPPERS(n##2) SCH_WRAPPERS(n##3) \
  SCH_WRAPPERS(n##4) SCH_WRAPPERS(n##5) SCH_WRAPPERS(n##6) SCH_WRAPPERS(n##7) \
  SCH_WRAPPERS(n##8) SCH_WRAPPERS(n##9)

#define SCH_WRAPPERS_100 \
  SCH_WRAPPERS_10(0) SCH_WRAPPERS_10(1) SCH_WRAPPERS_10(2) SCH_WRAPPERS_10(3) \
  SCH_WRAPPERS_10(4) SCH_WRAPPERS_10(5) SCH_WRAPPERS_10(6) SCH_WRAPPERS_10(7) \
  SCH_WRAPPERS_10(8) SCH_WRAPPERS_10(9)

SCH_WRAPPERS_100

#define INIT_CLS_OPS(nn) \
  { .classify = classify_wrapper_##nn },

#define INIT_CLS_OPS_10(n) \
  INIT_CLS_OPS(n##0) INIT_CLS_OPS(n##1) INIT_CLS_OPS(n##2) INIT_CLS_OPS(n##3) \
  INIT_CLS_OPS(n##4) INIT_CLS_OPS(n##5) INIT_CLS_OPS(n##6) INIT_CLS_OPS(n##7) \
  INIT_CLS_OPS(n##8) INIT_CLS_OPS(n##9)

#define INIT_CLS_OPS_100 \
  INIT_CLS_OPS_10(0) INIT_CLS_OPS_10(1) INIT_CLS_OPS_10(2) INIT_CLS_OPS_10(3) \
  INIT_CLS_OPS_10(4) INIT_CLS_OPS_10(5) INIT_CLS_OPS_10(6) INIT_CLS_OPS_10(7) \
  INIT_CLS_OPS_10(8) INIT_CLS_OPS_10(9)

#define INIT_SCH_OPS(nn) \
  { .enqueue = enqueue_wrapper_##nn, .dequeue = dequeue_wrapper_##nn, \
    .requeue = requeue_wrapper_##nn, .drop = drop_wrapper_##nn },

#define INIT_SCH_OPS_10(n) \
  INIT_SCH_OPS(n##0) INIT_SCH_OPS(n##1) INIT_SCH_OPS(n##2) INIT_SCH_OPS(n##3) \
  INIT_SCH_OPS(n##4) INIT_SCH_OPS(n##5) INIT_SCH_OPS(n##6) INIT_SCH_OPS(n##7) \
  INIT_SCH_OPS(n##8) INIT_SCH_OPS(n##9)

#define INIT_SCH_OPS_100 \
  INIT_SCH_OPS_10(0) INIT_SCH_OPS_10(1) INIT_SCH_OPS_10(2) INIT_SCH_OPS_10(3) \
  INIT_SCH_OPS_10(4) INIT_SCH_OPS_10(5) INIT_SCH_OPS_10(6) INIT_SCH_OPS_10(7) \
  INIT_SCH_OPS_10(8) INIT_SCH_OPS_10(9)
  
static const struct tcf_proto_ops my_cls_ops[NUM_CLS_WRAPPERS] =
  { INIT_CLS_OPS_100 };
static const struct Qdisc_ops my_sch_ops[NUM_SCH_WRAPPERS] =
  { INIT_SCH_OPS_100 };


static int wrap_qdisc(struct Qdisc_ops *ops)
{
    int n;

    for (n = 0; n < NUM_SCH_WRAPPERS; n++)
	if (!*orig_sch_ops[n].id) break;
    if (n == NUM_SCH_WRAPPERS) errorf("out of qdisc wrappers");
    orig_sch_ops[n] = *ops;
    ops->enqueue = my_sch_ops[n].enqueue;
    ops->dequeue = my_sch_ops[n].dequeue;
    ops->requeue = my_sch_ops[n].requeue;
    ops->drop = my_sch_ops[n].drop;
#ifdef CONFIG_NET_SCH_INGRESS
    if (ops == &ingress_qdisc_ops) {
	ingress_num = n;
	ops->enqueue = ingress_wrapper;
    }
#endif
    return n;
}


static int wrap_filter(struct tcf_proto_ops *ops)
{
    int n;

    for (n = 0; n < NUM_CLS_WRAPPERS; n++)
	if (!*orig_cls_ops[n].kind) break;
    if (n == NUM_CLS_WRAPPERS) errorf("out of filter wrappers");
    orig_cls_ops[n] = *ops;
    ops->classify = my_cls_ops[n].classify;
    return n;
}


int register_qdisc(struct Qdisc_ops *qops)
{
    int error;

    error = do_register_qdisc(qops);
    if (error) return error;
    (void) wrap_qdisc(qops);
    return 0;
}

int register_tcf_proto_ops(struct tcf_proto_ops *ops)
{
    int error;

    error = do_register_tcf_proto_ops(ops);
    if (error) return error;
    (void) wrap_filter(ops);
    return 0;
}


void setup_tracing(void)
{
    (void) wrap_qdisc(&noop_qdisc_ops);
    (void) wrap_qdisc(&pfifo_fast_ops);
}