[NETEM]: use PSCHED_LESS
[deliverable/linux.git] / net / sched / sch_netem.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/sch_netem.c Network emulator
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Many of the algorithms and ideas for this came from
10 * NIST Net which is not copyrighted.
11 *
12 * Authors: Stephen Hemminger <shemminger@osdl.org>
13 * Catalin(ux aka Dino) BOIE <catab at umbrella dot ro>
14 */
15
16#include <linux/config.h>
17#include <linux/module.h>
18#include <linux/bitops.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <linux/rtnetlink.h>
25
26#include <net/pkt_sched.h>
27
28/* Network Emulation Queuing algorithm.
29 ====================================
30
31 Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
32 Network Emulation Tool
33 [2] Luigi Rizzo, DummyNet for FreeBSD
34
35 ----------------------------------------------------------------
36
37 This started out as a simple way to delay outgoing packets to
38 test TCP but has grown to include most of the functionality
39 of a full blown network emulator like NISTnet. It can delay
40 packets and add random jitter (and correlation). The random
41 distribution can be loaded from a table as well to provide
42 normal, Pareto, or experimental curves. Packet loss,
43 duplication, and reordering can also be emulated.
44
45 This qdisc does not do classification that can be handled in
46 layering other disciplines. It does not need to do bandwidth
47 control either since that can be handled by using token
48 bucket or other rate control.
49
50 The simulator is limited by the Linux timer resolution
51 and will create packet bursts on the HZ boundary (1ms).
52*/
53
54struct netem_sched_data {
55 struct Qdisc *qdisc;
1da177e4
LT
56 struct timer_list timer;
57
58 u32 latency;
59 u32 loss;
60 u32 limit;
61 u32 counter;
62 u32 gap;
63 u32 jitter;
64 u32 duplicate;
0dca51d3 65 u32 reorder;
1da177e4
LT
66
67 struct crndstate {
68 unsigned long last;
69 unsigned long rho;
0dca51d3 70 } delay_cor, loss_cor, dup_cor, reorder_cor;
1da177e4
LT
71
72 struct disttable {
73 u32 size;
74 s16 table[0];
75 } *delay_dist;
76};
77
78/* Time stamp put into socket buffer control block */
79struct netem_skb_cb {
80 psched_time_t time_to_send;
81};
82
83/* init_crandom - initialize correlated random number generator
84 * Use entropy source for initial seed.
85 */
86static void init_crandom(struct crndstate *state, unsigned long rho)
87{
88 state->rho = rho;
89 state->last = net_random();
90}
91
92/* get_crandom - correlated random number generator
93 * Next number depends on last value.
94 * rho is scaled to avoid floating point.
95 */
96static unsigned long get_crandom(struct crndstate *state)
97{
98 u64 value, rho;
99 unsigned long answer;
100
101 if (state->rho == 0) /* no correllation */
102 return net_random();
103
104 value = net_random();
105 rho = (u64)state->rho + 1;
106 answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
107 state->last = answer;
108 return answer;
109}
110
111/* tabledist - return a pseudo-randomly distributed value with mean mu and
112 * std deviation sigma. Uses table lookup to approximate the desired
113 * distribution, and a uniformly-distributed pseudo-random source.
114 */
115static long tabledist(unsigned long mu, long sigma,
116 struct crndstate *state, const struct disttable *dist)
117{
118 long t, x;
119 unsigned long rnd;
120
121 if (sigma == 0)
122 return mu;
123
124 rnd = get_crandom(state);
125
126 /* default uniform distribution */
127 if (dist == NULL)
128 return (rnd % (2*sigma)) - sigma + mu;
129
130 t = dist->table[rnd % dist->size];
131 x = (sigma % NETEM_DIST_SCALE) * t;
132 if (x >= 0)
133 x += NETEM_DIST_SCALE/2;
134 else
135 x -= NETEM_DIST_SCALE/2;
136
137 return x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
138}
139
0afb51e7
SH
140/*
141 * Insert one skb into qdisc.
142 * Note: parent depends on return value to account for queue length.
143 * NET_XMIT_DROP: queue length didn't change.
144 * NET_XMIT_SUCCESS: one skb was queued.
145 */
1da177e4
LT
146static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
147{
148 struct netem_sched_data *q = qdisc_priv(sch);
0f9f32ac 149 struct netem_skb_cb *cb = (struct netem_skb_cb *)skb->cb;
0afb51e7 150 struct sk_buff *skb2;
1da177e4 151 int ret;
0afb51e7 152 int count = 1;
1da177e4 153
771018e7 154 pr_debug("netem_enqueue skb=%p\n", skb);
1da177e4 155
0afb51e7
SH
156 /* Random duplication */
157 if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
158 ++count;
159
1da177e4 160 /* Random packet drop 0 => none, ~0 => all */
0afb51e7
SH
161 if (q->loss && q->loss >= get_crandom(&q->loss_cor))
162 --count;
163
164 if (count == 0) {
1da177e4
LT
165 sch->qstats.drops++;
166 kfree_skb(skb);
0afb51e7 167 return NET_XMIT_DROP;
1da177e4
LT
168 }
169
0afb51e7
SH
170 /*
171 * If we need to duplicate packet, then re-insert at top of the
172 * qdisc tree, since parent queuer expects that only one
173 * skb will be queued.
174 */
175 if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
176 struct Qdisc *rootq = sch->dev->qdisc;
177 u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
178 q->duplicate = 0;
179
180 rootq->enqueue(skb2, rootq);
181 q->duplicate = dupsave;
1da177e4
LT
182 }
183
0dca51d3
SH
184 if (q->gap == 0 /* not doing reordering */
185 || q->counter < q->gap /* inside last reordering gap */
186 || q->reorder < get_crandom(&q->reorder_cor)) {
0f9f32ac 187 psched_time_t now;
07aaa115
SH
188 psched_tdiff_t delay;
189
190 delay = tabledist(q->latency, q->jitter,
191 &q->delay_cor, q->delay_dist);
192
0f9f32ac 193 PSCHED_GET_TIME(now);
07aaa115 194 PSCHED_TADD2(now, delay, cb->time_to_send);
1da177e4
LT
195 ++q->counter;
196 ret = q->qdisc->enqueue(skb, q->qdisc);
197 } else {
0dca51d3
SH
198 /*
199 * Do re-ordering by putting one out of N packets at the front
200 * of the queue.
201 */
0f9f32ac 202 PSCHED_GET_TIME(cb->time_to_send);
0dca51d3 203 q->counter = 0;
0f9f32ac 204 ret = q->qdisc->ops->requeue(skb, q->qdisc);
1da177e4
LT
205 }
206
207 if (likely(ret == NET_XMIT_SUCCESS)) {
208 sch->q.qlen++;
209 sch->bstats.bytes += skb->len;
210 sch->bstats.packets++;
211 } else
212 sch->qstats.drops++;
213
d5d75cd6 214 pr_debug("netem: enqueue ret %d\n", ret);
1da177e4
LT
215 return ret;
216}
217
218/* Requeue packets but don't change time stamp */
219static int netem_requeue(struct sk_buff *skb, struct Qdisc *sch)
220{
221 struct netem_sched_data *q = qdisc_priv(sch);
222 int ret;
223
224 if ((ret = q->qdisc->ops->requeue(skb, q->qdisc)) == 0) {
225 sch->q.qlen++;
226 sch->qstats.requeues++;
227 }
228
229 return ret;
230}
231
232static unsigned int netem_drop(struct Qdisc* sch)
233{
234 struct netem_sched_data *q = qdisc_priv(sch);
235 unsigned int len;
236
237 if ((len = q->qdisc->ops->drop(q->qdisc)) != 0) {
238 sch->q.qlen--;
239 sch->qstats.drops++;
240 }
241 return len;
242}
243
1da177e4
LT
244static struct sk_buff *netem_dequeue(struct Qdisc *sch)
245{
246 struct netem_sched_data *q = qdisc_priv(sch);
247 struct sk_buff *skb;
248
249 skb = q->qdisc->dequeue(q->qdisc);
771018e7 250 if (skb) {
0f9f32ac
SH
251 const struct netem_skb_cb *cb
252 = (const struct netem_skb_cb *)skb->cb;
253 psched_time_t now;
0f9f32ac
SH
254
255 /* if more time remaining? */
256 PSCHED_GET_TIME(now);
07aaa115
SH
257
258 if (PSCHED_TLESS(cb->time_to_send, now)) {
0f9f32ac
SH
259 pr_debug("netem_dequeue: return skb=%p\n", skb);
260 sch->q.qlen--;
261 sch->flags &= ~TCQ_F_THROTTLED;
262 return skb;
07aaa115
SH
263 } else {
264 psched_tdiff_t delay = PSCHED_TDIFF(cb->time_to_send, now);
0f9f32ac 265
07aaa115
SH
266 if (q->qdisc->ops->requeue(skb, q->qdisc) != NET_XMIT_SUCCESS) {
267 sch->qstats.drops++;
771018e7 268
07aaa115
SH
269 /* After this qlen is confused */
270 printk(KERN_ERR "netem: queue discpline %s could not requeue\n",
271 q->qdisc->ops->id);
272
273 sch->q.qlen--;
274 }
275
276 mod_timer(&q->timer, jiffies + PSCHED_US2JIFFIE(delay));
277 sch->flags |= TCQ_F_THROTTLED;
278 }
0f9f32ac
SH
279 }
280
281 return NULL;
1da177e4
LT
282}
283
284static void netem_watchdog(unsigned long arg)
285{
286 struct Qdisc *sch = (struct Qdisc *)arg;
1da177e4 287
771018e7
SH
288 pr_debug("netem_watchdog qlen=%d\n", sch->q.qlen);
289 sch->flags &= ~TCQ_F_THROTTLED;
290 netif_schedule(sch->dev);
1da177e4
LT
291}
292
293static void netem_reset(struct Qdisc *sch)
294{
295 struct netem_sched_data *q = qdisc_priv(sch);
296
297 qdisc_reset(q->qdisc);
1da177e4 298 sch->q.qlen = 0;
771018e7 299 sch->flags &= ~TCQ_F_THROTTLED;
1da177e4
LT
300 del_timer_sync(&q->timer);
301}
302
303static int set_fifo_limit(struct Qdisc *q, int limit)
304{
305 struct rtattr *rta;
306 int ret = -ENOMEM;
307
308 rta = kmalloc(RTA_LENGTH(sizeof(struct tc_fifo_qopt)), GFP_KERNEL);
309 if (rta) {
310 rta->rta_type = RTM_NEWQDISC;
311 rta->rta_len = RTA_LENGTH(sizeof(struct tc_fifo_qopt));
312 ((struct tc_fifo_qopt *)RTA_DATA(rta))->limit = limit;
313
314 ret = q->ops->change(q, rta);
315 kfree(rta);
316 }
317 return ret;
318}
319
320/*
321 * Distribution data is a variable size payload containing
322 * signed 16 bit values.
323 */
324static int get_dist_table(struct Qdisc *sch, const struct rtattr *attr)
325{
326 struct netem_sched_data *q = qdisc_priv(sch);
327 unsigned long n = RTA_PAYLOAD(attr)/sizeof(__s16);
328 const __s16 *data = RTA_DATA(attr);
329 struct disttable *d;
330 int i;
331
332 if (n > 65536)
333 return -EINVAL;
334
335 d = kmalloc(sizeof(*d) + n*sizeof(d->table[0]), GFP_KERNEL);
336 if (!d)
337 return -ENOMEM;
338
339 d->size = n;
340 for (i = 0; i < n; i++)
341 d->table[i] = data[i];
342
343 spin_lock_bh(&sch->dev->queue_lock);
344 d = xchg(&q->delay_dist, d);
345 spin_unlock_bh(&sch->dev->queue_lock);
346
347 kfree(d);
348 return 0;
349}
350
351static int get_correlation(struct Qdisc *sch, const struct rtattr *attr)
352{
353 struct netem_sched_data *q = qdisc_priv(sch);
354 const struct tc_netem_corr *c = RTA_DATA(attr);
355
356 if (RTA_PAYLOAD(attr) != sizeof(*c))
357 return -EINVAL;
358
359 init_crandom(&q->delay_cor, c->delay_corr);
360 init_crandom(&q->loss_cor, c->loss_corr);
361 init_crandom(&q->dup_cor, c->dup_corr);
362 return 0;
363}
364
0dca51d3
SH
365static int get_reorder(struct Qdisc *sch, const struct rtattr *attr)
366{
367 struct netem_sched_data *q = qdisc_priv(sch);
368 const struct tc_netem_reorder *r = RTA_DATA(attr);
369
370 if (RTA_PAYLOAD(attr) != sizeof(*r))
371 return -EINVAL;
372
373 q->reorder = r->probability;
374 init_crandom(&q->reorder_cor, r->correlation);
375 return 0;
376}
377
1da177e4
LT
378static int netem_change(struct Qdisc *sch, struct rtattr *opt)
379{
380 struct netem_sched_data *q = qdisc_priv(sch);
381 struct tc_netem_qopt *qopt;
382 int ret;
383
384 if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
385 return -EINVAL;
386
387 qopt = RTA_DATA(opt);
388 ret = set_fifo_limit(q->qdisc, qopt->limit);
389 if (ret) {
390 pr_debug("netem: can't set fifo limit\n");
391 return ret;
392 }
393
394 q->latency = qopt->latency;
395 q->jitter = qopt->jitter;
396 q->limit = qopt->limit;
397 q->gap = qopt->gap;
0dca51d3 398 q->counter = 0;
1da177e4
LT
399 q->loss = qopt->loss;
400 q->duplicate = qopt->duplicate;
401
0dca51d3
SH
402 /* for compatiablity with earlier versions.
403 * if gap is set, need to assume 100% probablity
404 */
405 q->reorder = ~0;
406
1da177e4
LT
407 /* Handle nested options after initial queue options.
408 * Should have put all options in nested format but too late now.
409 */
410 if (RTA_PAYLOAD(opt) > sizeof(*qopt)) {
411 struct rtattr *tb[TCA_NETEM_MAX];
412 if (rtattr_parse(tb, TCA_NETEM_MAX,
413 RTA_DATA(opt) + sizeof(*qopt),
414 RTA_PAYLOAD(opt) - sizeof(*qopt)))
415 return -EINVAL;
416
417 if (tb[TCA_NETEM_CORR-1]) {
418 ret = get_correlation(sch, tb[TCA_NETEM_CORR-1]);
419 if (ret)
420 return ret;
421 }
422
423 if (tb[TCA_NETEM_DELAY_DIST-1]) {
424 ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST-1]);
425 if (ret)
426 return ret;
427 }
0dca51d3
SH
428 if (tb[TCA_NETEM_REORDER-1]) {
429 ret = get_reorder(sch, tb[TCA_NETEM_REORDER-1]);
430 if (ret)
431 return ret;
432 }
1da177e4
LT
433 }
434
435
436 return 0;
437}
438
439static int netem_init(struct Qdisc *sch, struct rtattr *opt)
440{
441 struct netem_sched_data *q = qdisc_priv(sch);
442 int ret;
443
444 if (!opt)
445 return -EINVAL;
446
1da177e4
LT
447 init_timer(&q->timer);
448 q->timer.function = netem_watchdog;
449 q->timer.data = (unsigned long) sch;
1da177e4
LT
450
451 q->qdisc = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
452 if (!q->qdisc) {
453 pr_debug("netem: qdisc create failed\n");
454 return -ENOMEM;
455 }
456
457 ret = netem_change(sch, opt);
458 if (ret) {
459 pr_debug("netem: change failed\n");
460 qdisc_destroy(q->qdisc);
461 }
462 return ret;
463}
464
465static void netem_destroy(struct Qdisc *sch)
466{
467 struct netem_sched_data *q = qdisc_priv(sch);
468
469 del_timer_sync(&q->timer);
470 qdisc_destroy(q->qdisc);
471 kfree(q->delay_dist);
472}
473
474static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
475{
476 const struct netem_sched_data *q = qdisc_priv(sch);
477 unsigned char *b = skb->tail;
478 struct rtattr *rta = (struct rtattr *) b;
479 struct tc_netem_qopt qopt;
480 struct tc_netem_corr cor;
0dca51d3 481 struct tc_netem_reorder reorder;
1da177e4
LT
482
483 qopt.latency = q->latency;
484 qopt.jitter = q->jitter;
485 qopt.limit = q->limit;
486 qopt.loss = q->loss;
487 qopt.gap = q->gap;
488 qopt.duplicate = q->duplicate;
489 RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
490
491 cor.delay_corr = q->delay_cor.rho;
492 cor.loss_corr = q->loss_cor.rho;
493 cor.dup_corr = q->dup_cor.rho;
494 RTA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
0dca51d3
SH
495
496 reorder.probability = q->reorder;
497 reorder.correlation = q->reorder_cor.rho;
498 RTA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
499
1da177e4
LT
500 rta->rta_len = skb->tail - b;
501
502 return skb->len;
503
504rtattr_failure:
505 skb_trim(skb, b - skb->data);
506 return -1;
507}
508
509static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
510 struct sk_buff *skb, struct tcmsg *tcm)
511{
512 struct netem_sched_data *q = qdisc_priv(sch);
513
514 if (cl != 1) /* only one class */
515 return -ENOENT;
516
517 tcm->tcm_handle |= TC_H_MIN(1);
518 tcm->tcm_info = q->qdisc->handle;
519
520 return 0;
521}
522
523static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
524 struct Qdisc **old)
525{
526 struct netem_sched_data *q = qdisc_priv(sch);
527
528 if (new == NULL)
529 new = &noop_qdisc;
530
531 sch_tree_lock(sch);
532 *old = xchg(&q->qdisc, new);
533 qdisc_reset(*old);
534 sch->q.qlen = 0;
535 sch_tree_unlock(sch);
536
537 return 0;
538}
539
540static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
541{
542 struct netem_sched_data *q = qdisc_priv(sch);
543 return q->qdisc;
544}
545
546static unsigned long netem_get(struct Qdisc *sch, u32 classid)
547{
548 return 1;
549}
550
551static void netem_put(struct Qdisc *sch, unsigned long arg)
552{
553}
554
555static int netem_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
556 struct rtattr **tca, unsigned long *arg)
557{
558 return -ENOSYS;
559}
560
561static int netem_delete(struct Qdisc *sch, unsigned long arg)
562{
563 return -ENOSYS;
564}
565
566static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
567{
568 if (!walker->stop) {
569 if (walker->count >= walker->skip)
570 if (walker->fn(sch, 1, walker) < 0) {
571 walker->stop = 1;
572 return;
573 }
574 walker->count++;
575 }
576}
577
578static struct tcf_proto **netem_find_tcf(struct Qdisc *sch, unsigned long cl)
579{
580 return NULL;
581}
582
583static struct Qdisc_class_ops netem_class_ops = {
584 .graft = netem_graft,
585 .leaf = netem_leaf,
586 .get = netem_get,
587 .put = netem_put,
588 .change = netem_change_class,
589 .delete = netem_delete,
590 .walk = netem_walk,
591 .tcf_chain = netem_find_tcf,
592 .dump = netem_dump_class,
593};
594
595static struct Qdisc_ops netem_qdisc_ops = {
596 .id = "netem",
597 .cl_ops = &netem_class_ops,
598 .priv_size = sizeof(struct netem_sched_data),
599 .enqueue = netem_enqueue,
600 .dequeue = netem_dequeue,
601 .requeue = netem_requeue,
602 .drop = netem_drop,
603 .init = netem_init,
604 .reset = netem_reset,
605 .destroy = netem_destroy,
606 .change = netem_change,
607 .dump = netem_dump,
608 .owner = THIS_MODULE,
609};
610
611
612static int __init netem_module_init(void)
613{
614 return register_qdisc(&netem_qdisc_ops);
615}
616static void __exit netem_module_exit(void)
617{
618 unregister_qdisc(&netem_qdisc_ops);
619}
620module_init(netem_module_init)
621module_exit(netem_module_exit)
622MODULE_LICENSE("GPL");
This page took 0.100296 seconds and 5 git commands to generate.