blob: 9b7af9f1272f88ff91795420ae8c35374da7c073 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Stephen Hemminger798b6b12006-10-22 20:16:57 -07007 * 2 of the License.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Many of the algorithms and ideas for this came from
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090010 * NIST Net which is not copyrighted.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Authors: Stephen Hemminger <shemminger@osdl.org>
13 * Catalin(ux aka Dino) BOIE <catab at umbrella dot ro>
14 */
15
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/skbuff.h>
David S. Miller78776d32011-02-24 22:48:13 -080023#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/rtnetlink.h>
25
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070026#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/pkt_sched.h>
28
stephen hemminger250a65f2011-02-23 13:04:22 +000029#define VERSION "1.3"
Stephen Hemmingereb229c42005-11-03 13:49:01 -080030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* Network Emulation Queuing algorithm.
32 ====================================
33
34 Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
35 Network Emulation Tool
36 [2] Luigi Rizzo, DummyNet for FreeBSD
37
38 ----------------------------------------------------------------
39
40 This started out as a simple way to delay outgoing packets to
41 test TCP but has grown to include most of the functionality
42 of a full blown network emulator like NISTnet. It can delay
43 packets and add random jitter (and correlation). The random
44 distribution can be loaded from a table as well to provide
45 normal, Pareto, or experimental curves. Packet loss,
46 duplication, and reordering can also be emulated.
47
48 This qdisc does not do classification that can be handled in
49 layering other disciplines. It does not need to do bandwidth
50 control either since that can be handled by using token
51 bucket or other rate control.
stephen hemminger661b7972011-02-23 13:04:21 +000052
53 Correlated Loss Generator models
54
55 Added generation of correlated loss according to the
56 "Gilbert-Elliot" model, a 4-state markov model.
57
58 References:
59 [1] NetemCLG Home http://netgroup.uniroma2.it/NetemCLG
60 [2] S. Salsano, F. Ludovici, A. Ordine, "Definition of a general
61 and intuitive loss model for packet networks and its implementation
62 in the Netem module in the Linux kernel", available in [1]
63
64 Authors: Stefano Salsano <stefano.salsano at uniroma2.it
65 Fabio Ludovici <fabio.ludovici at yahoo.it>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066*/
67
68struct netem_sched_data {
69 struct Qdisc *qdisc;
Patrick McHardy59cb5c62007-03-16 01:20:31 -070070 struct qdisc_watchdog watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Stephen Hemmingerb4076212007-03-22 12:16:21 -070072 psched_tdiff_t latency;
73 psched_tdiff_t jitter;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u32 loss;
76 u32 limit;
77 u32 counter;
78 u32 gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 u32 duplicate;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -070080 u32 reorder;
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -080081 u32 corrupt;
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +000082 u32 rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 struct crndstate {
Stephen Hemmingerb4076212007-03-22 12:16:21 -070085 u32 last;
86 u32 rho;
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -080087 } delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 struct disttable {
90 u32 size;
91 s16 table[0];
92 } *delay_dist;
stephen hemminger661b7972011-02-23 13:04:21 +000093
94 enum {
95 CLG_RANDOM,
96 CLG_4_STATES,
97 CLG_GILB_ELL,
98 } loss_model;
99
100 /* Correlated Loss Generation models */
101 struct clgstate {
102 /* state of the Markov chain */
103 u8 state;
104
105 /* 4-states and Gilbert-Elliot models */
106 u32 a1; /* p13 for 4-states or p for GE */
107 u32 a2; /* p31 for 4-states or r for GE */
108 u32 a3; /* p32 for 4-states or h for GE */
109 u32 a4; /* p14 for 4-states or 1-k for GE */
110 u32 a5; /* p23 used only in 4-states */
111 } clg;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115/* Time stamp put into socket buffer control block */
116struct netem_skb_cb {
117 psched_time_t time_to_send;
118};
119
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700120static inline struct netem_skb_cb *netem_skb_cb(struct sk_buff *skb)
121{
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700122 BUILD_BUG_ON(sizeof(skb->cb) <
123 sizeof(struct qdisc_skb_cb) + sizeof(struct netem_skb_cb));
124 return (struct netem_skb_cb *)qdisc_skb_cb(skb)->data;
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* init_crandom - initialize correlated random number generator
128 * Use entropy source for initial seed.
129 */
130static void init_crandom(struct crndstate *state, unsigned long rho)
131{
132 state->rho = rho;
133 state->last = net_random();
134}
135
136/* get_crandom - correlated random number generator
137 * Next number depends on last value.
138 * rho is scaled to avoid floating point.
139 */
Stephen Hemmingerb4076212007-03-22 12:16:21 -0700140static u32 get_crandom(struct crndstate *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 u64 value, rho;
143 unsigned long answer;
144
Stephen Hemmingerbb2f8cc2007-03-23 00:12:09 -0700145 if (state->rho == 0) /* no correlation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return net_random();
147
148 value = net_random();
149 rho = (u64)state->rho + 1;
150 answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
151 state->last = answer;
152 return answer;
153}
154
stephen hemminger661b7972011-02-23 13:04:21 +0000155/* loss_4state - 4-state model loss generator
156 * Generates losses according to the 4-state Markov chain adopted in
157 * the GI (General and Intuitive) loss model.
158 */
159static bool loss_4state(struct netem_sched_data *q)
160{
161 struct clgstate *clg = &q->clg;
162 u32 rnd = net_random();
163
164 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300165 * Makes a comparison between rnd and the transition
stephen hemminger661b7972011-02-23 13:04:21 +0000166 * probabilities outgoing from the current state, then decides the
167 * next state and if the next packet has to be transmitted or lost.
168 * The four states correspond to:
169 * 1 => successfully transmitted packets within a gap period
170 * 4 => isolated losses within a gap period
171 * 3 => lost packets within a burst period
172 * 2 => successfully transmitted packets within a burst period
173 */
174 switch (clg->state) {
175 case 1:
176 if (rnd < clg->a4) {
177 clg->state = 4;
178 return true;
179 } else if (clg->a4 < rnd && rnd < clg->a1) {
180 clg->state = 3;
181 return true;
182 } else if (clg->a1 < rnd)
183 clg->state = 1;
184
185 break;
186 case 2:
187 if (rnd < clg->a5) {
188 clg->state = 3;
189 return true;
190 } else
191 clg->state = 2;
192
193 break;
194 case 3:
195 if (rnd < clg->a3)
196 clg->state = 2;
197 else if (clg->a3 < rnd && rnd < clg->a2 + clg->a3) {
198 clg->state = 1;
199 return true;
200 } else if (clg->a2 + clg->a3 < rnd) {
201 clg->state = 3;
202 return true;
203 }
204 break;
205 case 4:
206 clg->state = 1;
207 break;
208 }
209
210 return false;
211}
212
213/* loss_gilb_ell - Gilbert-Elliot model loss generator
214 * Generates losses according to the Gilbert-Elliot loss model or
215 * its special cases (Gilbert or Simple Gilbert)
216 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300217 * Makes a comparison between random number and the transition
stephen hemminger661b7972011-02-23 13:04:21 +0000218 * probabilities outgoing from the current state, then decides the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300219 * next state. A second random number is extracted and the comparison
stephen hemminger661b7972011-02-23 13:04:21 +0000220 * with the loss probability of the current state decides if the next
221 * packet will be transmitted or lost.
222 */
223static bool loss_gilb_ell(struct netem_sched_data *q)
224{
225 struct clgstate *clg = &q->clg;
226
227 switch (clg->state) {
228 case 1:
229 if (net_random() < clg->a1)
230 clg->state = 2;
231 if (net_random() < clg->a4)
232 return true;
233 case 2:
234 if (net_random() < clg->a2)
235 clg->state = 1;
236 if (clg->a3 > net_random())
237 return true;
238 }
239
240 return false;
241}
242
243static bool loss_event(struct netem_sched_data *q)
244{
245 switch (q->loss_model) {
246 case CLG_RANDOM:
247 /* Random packet drop 0 => none, ~0 => all */
248 return q->loss && q->loss >= get_crandom(&q->loss_cor);
249
250 case CLG_4_STATES:
251 /* 4state loss model algorithm (used also for GI model)
252 * Extracts a value from the markov 4 state loss generator,
253 * if it is 1 drops a packet and if needed writes the event in
254 * the kernel logs
255 */
256 return loss_4state(q);
257
258 case CLG_GILB_ELL:
259 /* Gilbert-Elliot loss model algorithm
260 * Extracts a value from the Gilbert-Elliot loss generator,
261 * if it is 1 drops a packet and if needed writes the event in
262 * the kernel logs
263 */
264 return loss_gilb_ell(q);
265 }
266
267 return false; /* not reached */
268}
269
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/* tabledist - return a pseudo-randomly distributed value with mean mu and
272 * std deviation sigma. Uses table lookup to approximate the desired
273 * distribution, and a uniformly-distributed pseudo-random source.
274 */
Stephen Hemmingerb4076212007-03-22 12:16:21 -0700275static psched_tdiff_t tabledist(psched_tdiff_t mu, psched_tdiff_t sigma,
276 struct crndstate *state,
277 const struct disttable *dist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Stephen Hemmingerb4076212007-03-22 12:16:21 -0700279 psched_tdiff_t x;
280 long t;
281 u32 rnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (sigma == 0)
284 return mu;
285
286 rnd = get_crandom(state);
287
288 /* default uniform distribution */
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900289 if (dist == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return (rnd % (2*sigma)) - sigma + mu;
291
292 t = dist->table[rnd % dist->size];
293 x = (sigma % NETEM_DIST_SCALE) * t;
294 if (x >= 0)
295 x += NETEM_DIST_SCALE/2;
296 else
297 x -= NETEM_DIST_SCALE/2;
298
299 return x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
300}
301
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000302static psched_time_t packet_len_2_sched_time(unsigned int len, u32 rate)
303{
304 return PSCHED_NS2TICKS((u64)len * NSEC_PER_SEC / rate);
305}
306
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700307/*
308 * Insert one skb into qdisc.
309 * Note: parent depends on return value to account for queue length.
310 * NET_XMIT_DROP: queue length didn't change.
311 * NET_XMIT_SUCCESS: one skb was queued.
312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
314{
315 struct netem_sched_data *q = qdisc_priv(sch);
Guillaume Chazarain89e1df72006-07-21 14:45:25 -0700316 /* We don't fill cb now as skb_unshare() may invalidate it */
317 struct netem_skb_cb *cb;
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700318 struct sk_buff *skb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int ret;
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700320 int count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700322 /* Random duplication */
323 if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
324 ++count;
325
stephen hemminger661b7972011-02-23 13:04:21 +0000326 /* Drop packet? */
327 if (loss_event(q))
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700328 --count;
329
330 if (count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 sch->qstats.drops++;
332 kfree_skb(skb);
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700333 return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
David S. Miller4e8a5202006-10-22 21:00:33 -0700336 skb_orphan(skb);
337
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700338 /*
339 * If we need to duplicate packet, then re-insert at top of the
340 * qdisc tree, since parent queuer expects that only one
341 * skb will be queued.
342 */
343 if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
David S. Miller7698b4f2008-07-16 01:42:40 -0700344 struct Qdisc *rootq = qdisc_root(sch);
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700345 u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
346 q->duplicate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700348 qdisc_enqueue_root(skb2, rootq);
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700349 q->duplicate = dupsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800352 /*
353 * Randomized packet corruption.
354 * Make copy if needed since we are modifying
355 * If packet is going to be hardware checksummed, then
356 * do it now in software before we mangle it.
357 */
358 if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
Joe Perchesf64f9e72009-11-29 16:55:45 -0800359 if (!(skb = skb_unshare(skb, GFP_ATOMIC)) ||
360 (skb->ip_summed == CHECKSUM_PARTIAL &&
361 skb_checksum_help(skb))) {
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800362 sch->qstats.drops++;
363 return NET_XMIT_DROP;
364 }
365
366 skb->data[net_random() % skb_headlen(skb)] ^= 1<<(net_random() % 8);
367 }
368
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700369 cb = netem_skb_cb(skb);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000370 if (q->gap == 0 || /* not doing reordering */
371 q->counter < q->gap || /* inside last reordering gap */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800372 q->reorder < get_crandom(&q->reorder_cor)) {
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700373 psched_time_t now;
Stephen Hemminger07aaa112005-11-03 13:43:07 -0800374 psched_tdiff_t delay;
375
376 delay = tabledist(q->latency, q->jitter,
377 &q->delay_cor, q->delay_dist);
378
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700379 now = psched_get_time();
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000380
381 if (q->rate) {
382 struct sk_buff_head *list = &q->qdisc->q;
383
384 delay += packet_len_2_sched_time(skb->len, q->rate);
385
386 if (!skb_queue_empty(list)) {
387 /*
388 * Last packet in queue is reference point (now).
389 * First packet in queue is already in flight,
390 * calculate this time bonus and substract
391 * from delay.
392 */
393 delay -= now - netem_skb_cb(skb_peek(list))->time_to_send;
394 now = netem_skb_cb(skb_peek_tail(list))->time_to_send;
395 }
396 }
397
Patrick McHardy7c59e252007-03-23 11:27:45 -0700398 cb->time_to_send = now + delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ++q->counter;
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700400 ret = qdisc_enqueue(skb, q->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 } else {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900402 /*
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700403 * Do re-ordering by putting one out of N packets at the front
404 * of the queue.
405 */
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700406 cb->time_to_send = psched_get_time();
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700407 q->counter = 0;
Jarek Poplawski8ba25da2008-11-02 00:36:03 -0700408
409 __skb_queue_head(&q->qdisc->q, skb);
410 q->qdisc->qstats.backlog += qdisc_pkt_len(skb);
411 q->qdisc->qstats.requeues++;
412 ret = NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000415 if (ret != NET_XMIT_SUCCESS) {
416 if (net_xmit_drop_count(ret)) {
417 sch->qstats.drops++;
418 return ret;
419 }
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000422 sch->q.qlen++;
423 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000426static unsigned int netem_drop(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy6d037a22006-03-20 19:00:49 -0800429 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick McHardy6d037a22006-03-20 19:00:49 -0800431 if (q->qdisc->ops->drop && (len = q->qdisc->ops->drop(q->qdisc)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sch->q.qlen--;
433 sch->qstats.drops++;
434 }
435 return len;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static struct sk_buff *netem_dequeue(struct Qdisc *sch)
439{
440 struct netem_sched_data *q = qdisc_priv(sch);
441 struct sk_buff *skb;
442
Eric Dumazetfd245a42011-01-20 05:27:16 +0000443 if (qdisc_is_throttled(sch))
Stephen Hemminger11274e52007-03-22 12:17:42 -0700444 return NULL;
445
Jarek Poplawski03c05f02008-10-31 00:46:19 -0700446 skb = q->qdisc->ops->peek(q->qdisc);
Stephen Hemminger771018e2005-05-03 16:24:32 -0700447 if (skb) {
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700448 const struct netem_skb_cb *cb = netem_skb_cb(skb);
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700449 psched_time_t now = psched_get_time();
Stephen Hemminger771018e2005-05-03 16:24:32 -0700450
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700451 /* if more time remaining? */
Patrick McHardy104e0872007-03-23 11:28:07 -0700452 if (cb->time_to_send <= now) {
Jarek Poplawski77be1552008-10-31 00:47:01 -0700453 skb = qdisc_dequeue_peeked(q->qdisc);
454 if (unlikely(!skb))
Jarek Poplawski03c05f02008-10-31 00:46:19 -0700455 return NULL;
456
Jarek Poplawski8caf1532009-04-17 10:08:49 +0000457#ifdef CONFIG_NET_CLS_ACT
458 /*
459 * If it's at ingress let's pretend the delay is
460 * from the network (tstamp will be updated).
461 */
462 if (G_TC_FROM(skb->tc_verd) & AT_INGRESS)
463 skb->tstamp.tv64 = 0;
464#endif
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000465
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700466 sch->q.qlen--;
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000467 qdisc_unthrottled(sch);
468 qdisc_bstats_update(sch, skb);
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700469 return skb;
470 }
Stephen Hemminger11274e52007-03-22 12:17:42 -0700471
Stephen Hemminger11274e52007-03-22 12:17:42 -0700472 qdisc_watchdog_schedule(&q->watchdog, cb->time_to_send);
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700473 }
474
475 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static void netem_reset(struct Qdisc *sch)
479{
480 struct netem_sched_data *q = qdisc_priv(sch);
481
482 qdisc_reset(q->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 sch->q.qlen = 0;
Patrick McHardy59cb5c62007-03-16 01:20:31 -0700484 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
stephen hemminger6373a9a2011-02-23 13:04:18 +0000487static void dist_free(struct disttable *d)
488{
489 if (d) {
490 if (is_vmalloc_addr(d))
491 vfree(d);
492 else
493 kfree(d);
494 }
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*
498 * Distribution data is a variable size payload containing
499 * signed 16 bit values.
500 */
Patrick McHardy1e904742008-01-22 22:11:17 -0800501static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct netem_sched_data *q = qdisc_priv(sch);
stephen hemminger6373a9a2011-02-23 13:04:18 +0000504 size_t n = nla_len(attr)/sizeof(__s16);
Patrick McHardy1e904742008-01-22 22:11:17 -0800505 const __s16 *data = nla_data(attr);
David S. Miller7698b4f2008-07-16 01:42:40 -0700506 spinlock_t *root_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct disttable *d;
508 int i;
stephen hemminger6373a9a2011-02-23 13:04:18 +0000509 size_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
stephen hemmingerdf173bd2011-02-23 13:04:19 +0000511 if (n > NETEM_DIST_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EINVAL;
513
stephen hemminger6373a9a2011-02-23 13:04:18 +0000514 s = sizeof(struct disttable) + n * sizeof(s16);
515 d = kmalloc(s, GFP_KERNEL);
516 if (!d)
517 d = vmalloc(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!d)
519 return -ENOMEM;
520
521 d->size = n;
522 for (i = 0; i < n; i++)
523 d->table[i] = data[i];
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900524
Jarek Poplawski102396a2008-08-29 14:21:52 -0700525 root_lock = qdisc_root_sleeping_lock(sch);
David S. Miller7698b4f2008-07-16 01:42:40 -0700526
527 spin_lock_bh(root_lock);
stephen hemminger6373a9a2011-02-23 13:04:18 +0000528 dist_free(q->delay_dist);
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800529 q->delay_dist = d;
David S. Miller7698b4f2008-07-16 01:42:40 -0700530 spin_unlock_bh(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return 0;
532}
533
Stephen Hemminger265eb672008-11-03 21:13:26 -0800534static void get_correlation(struct Qdisc *sch, const struct nlattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800537 const struct tc_netem_corr *c = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 init_crandom(&q->delay_cor, c->delay_corr);
540 init_crandom(&q->loss_cor, c->loss_corr);
541 init_crandom(&q->dup_cor, c->dup_corr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Stephen Hemminger265eb672008-11-03 21:13:26 -0800544static void get_reorder(struct Qdisc *sch, const struct nlattr *attr)
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700545{
546 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800547 const struct tc_netem_reorder *r = nla_data(attr);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700548
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700549 q->reorder = r->probability;
550 init_crandom(&q->reorder_cor, r->correlation);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700551}
552
Stephen Hemminger265eb672008-11-03 21:13:26 -0800553static void get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800554{
555 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800556 const struct tc_netem_corrupt *r = nla_data(attr);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800557
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800558 q->corrupt = r->probability;
559 init_crandom(&q->corrupt_cor, r->correlation);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800560}
561
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000562static void get_rate(struct Qdisc *sch, const struct nlattr *attr)
563{
564 struct netem_sched_data *q = qdisc_priv(sch);
565 const struct tc_netem_rate *r = nla_data(attr);
566
567 q->rate = r->rate;
568}
569
stephen hemminger661b7972011-02-23 13:04:21 +0000570static int get_loss_clg(struct Qdisc *sch, const struct nlattr *attr)
571{
572 struct netem_sched_data *q = qdisc_priv(sch);
573 const struct nlattr *la;
574 int rem;
575
576 nla_for_each_nested(la, attr, rem) {
577 u16 type = nla_type(la);
578
579 switch(type) {
580 case NETEM_LOSS_GI: {
581 const struct tc_netem_gimodel *gi = nla_data(la);
582
583 if (nla_len(la) != sizeof(struct tc_netem_gimodel)) {
584 pr_info("netem: incorrect gi model size\n");
585 return -EINVAL;
586 }
587
588 q->loss_model = CLG_4_STATES;
589
590 q->clg.state = 1;
591 q->clg.a1 = gi->p13;
592 q->clg.a2 = gi->p31;
593 q->clg.a3 = gi->p32;
594 q->clg.a4 = gi->p14;
595 q->clg.a5 = gi->p23;
596 break;
597 }
598
599 case NETEM_LOSS_GE: {
600 const struct tc_netem_gemodel *ge = nla_data(la);
601
602 if (nla_len(la) != sizeof(struct tc_netem_gemodel)) {
603 pr_info("netem: incorrect gi model size\n");
604 return -EINVAL;
605 }
606
607 q->loss_model = CLG_GILB_ELL;
608 q->clg.state = 1;
609 q->clg.a1 = ge->p;
610 q->clg.a2 = ge->r;
611 q->clg.a3 = ge->h;
612 q->clg.a4 = ge->k1;
613 break;
614 }
615
616 default:
617 pr_info("netem: unknown loss type %u\n", type);
618 return -EINVAL;
619 }
620 }
621
622 return 0;
623}
624
Patrick McHardy27a34212008-01-23 20:35:39 -0800625static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
626 [TCA_NETEM_CORR] = { .len = sizeof(struct tc_netem_corr) },
627 [TCA_NETEM_REORDER] = { .len = sizeof(struct tc_netem_reorder) },
628 [TCA_NETEM_CORRUPT] = { .len = sizeof(struct tc_netem_corrupt) },
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000629 [TCA_NETEM_RATE] = { .len = sizeof(struct tc_netem_rate) },
stephen hemminger661b7972011-02-23 13:04:21 +0000630 [TCA_NETEM_LOSS] = { .type = NLA_NESTED },
Patrick McHardy27a34212008-01-23 20:35:39 -0800631};
632
Thomas Graf2c10b322008-09-02 17:30:27 -0700633static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
634 const struct nla_policy *policy, int len)
635{
636 int nested_len = nla_len(nla) - NLA_ALIGN(len);
637
stephen hemminger661b7972011-02-23 13:04:21 +0000638 if (nested_len < 0) {
639 pr_info("netem: invalid attributes len %d\n", nested_len);
Thomas Graf2c10b322008-09-02 17:30:27 -0700640 return -EINVAL;
stephen hemminger661b7972011-02-23 13:04:21 +0000641 }
642
Thomas Graf2c10b322008-09-02 17:30:27 -0700643 if (nested_len >= nla_attr_size(0))
644 return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
645 nested_len, policy);
stephen hemminger661b7972011-02-23 13:04:21 +0000646
Thomas Graf2c10b322008-09-02 17:30:27 -0700647 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
648 return 0;
649}
650
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800651/* Parse netlink message to set options */
Patrick McHardy1e904742008-01-22 22:11:17 -0800652static int netem_change(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardyb03f4672008-01-23 20:32:21 -0800655 struct nlattr *tb[TCA_NETEM_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct tc_netem_qopt *qopt;
657 int ret;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900658
Patrick McHardyb03f4672008-01-23 20:32:21 -0800659 if (opt == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -EINVAL;
661
Thomas Graf2c10b322008-09-02 17:30:27 -0700662 qopt = nla_data(opt);
663 ret = parse_attr(tb, TCA_NETEM_MAX, opt, netem_policy, sizeof(*qopt));
Patrick McHardyb03f4672008-01-23 20:32:21 -0800664 if (ret < 0)
665 return ret;
666
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700667 ret = fifo_set_limit(q->qdisc, qopt->limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (ret) {
stephen hemminger250a65f2011-02-23 13:04:22 +0000669 pr_info("netem: can't set fifo limit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return ret;
671 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 q->latency = qopt->latency;
674 q->jitter = qopt->jitter;
675 q->limit = qopt->limit;
676 q->gap = qopt->gap;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700677 q->counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 q->loss = qopt->loss;
679 q->duplicate = qopt->duplicate;
680
Stephen Hemmingerbb2f8cc2007-03-23 00:12:09 -0700681 /* for compatibility with earlier versions.
682 * if gap is set, need to assume 100% probability
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700683 */
Stephen Hemmingera362e0a2007-03-22 12:15:45 -0700684 if (q->gap)
685 q->reorder = ~0;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700686
Stephen Hemminger265eb672008-11-03 21:13:26 -0800687 if (tb[TCA_NETEM_CORR])
688 get_correlation(sch, tb[TCA_NETEM_CORR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Patrick McHardyb03f4672008-01-23 20:32:21 -0800690 if (tb[TCA_NETEM_DELAY_DIST]) {
691 ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
692 if (ret)
693 return ret;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Stephen Hemminger265eb672008-11-03 21:13:26 -0800696 if (tb[TCA_NETEM_REORDER])
697 get_reorder(sch, tb[TCA_NETEM_REORDER]);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800698
Stephen Hemminger265eb672008-11-03 21:13:26 -0800699 if (tb[TCA_NETEM_CORRUPT])
700 get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000702 if (tb[TCA_NETEM_RATE])
703 get_rate(sch, tb[TCA_NETEM_RATE]);
704
stephen hemminger661b7972011-02-23 13:04:21 +0000705 q->loss_model = CLG_RANDOM;
706 if (tb[TCA_NETEM_LOSS])
707 ret = get_loss_clg(sch, tb[TCA_NETEM_LOSS]);
708
709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Stephen Hemminger300ce172005-10-30 13:47:34 -0800712/*
713 * Special case version of FIFO queue for use by netem.
714 * It queues in order based on timestamps in skb's
715 */
716struct fifo_sched_data {
717 u32 limit;
Stephen Hemminger075aa572007-03-22 12:17:05 -0700718 psched_time_t oldest;
Stephen Hemminger300ce172005-10-30 13:47:34 -0800719};
720
721static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
722{
723 struct fifo_sched_data *q = qdisc_priv(sch);
724 struct sk_buff_head *list = &sch->q;
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700725 psched_time_t tnext = netem_skb_cb(nskb)->time_to_send;
Stephen Hemminger300ce172005-10-30 13:47:34 -0800726 struct sk_buff *skb;
727
728 if (likely(skb_queue_len(list) < q->limit)) {
Stephen Hemminger075aa572007-03-22 12:17:05 -0700729 /* Optimize for add at tail */
Patrick McHardy104e0872007-03-23 11:28:07 -0700730 if (likely(skb_queue_empty(list) || tnext >= q->oldest)) {
Stephen Hemminger075aa572007-03-22 12:17:05 -0700731 q->oldest = tnext;
732 return qdisc_enqueue_tail(nskb, sch);
733 }
734
Stephen Hemminger300ce172005-10-30 13:47:34 -0800735 skb_queue_reverse_walk(list, skb) {
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700736 const struct netem_skb_cb *cb = netem_skb_cb(skb);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800737
Patrick McHardy104e0872007-03-23 11:28:07 -0700738 if (tnext >= cb->time_to_send)
Stephen Hemminger300ce172005-10-30 13:47:34 -0800739 break;
740 }
741
742 __skb_queue_after(list, skb, nskb);
743
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700744 sch->qstats.backlog += qdisc_pkt_len(nskb);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800745
746 return NET_XMIT_SUCCESS;
747 }
748
Stephen Hemminger075aa572007-03-22 12:17:05 -0700749 return qdisc_reshape_fail(nskb, sch);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800750}
751
Patrick McHardy1e904742008-01-22 22:11:17 -0800752static int tfifo_init(struct Qdisc *sch, struct nlattr *opt)
Stephen Hemminger300ce172005-10-30 13:47:34 -0800753{
754 struct fifo_sched_data *q = qdisc_priv(sch);
755
756 if (opt) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800757 struct tc_fifo_qopt *ctl = nla_data(opt);
758 if (nla_len(opt) < sizeof(*ctl))
Stephen Hemminger300ce172005-10-30 13:47:34 -0800759 return -EINVAL;
760
761 q->limit = ctl->limit;
762 } else
David S. Miller5ce2d482008-07-08 17:06:30 -0700763 q->limit = max_t(u32, qdisc_dev(sch)->tx_queue_len, 1);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800764
Patrick McHardya0849802007-03-23 11:28:30 -0700765 q->oldest = PSCHED_PASTPERFECT;
Stephen Hemminger300ce172005-10-30 13:47:34 -0800766 return 0;
767}
768
769static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
770{
771 struct fifo_sched_data *q = qdisc_priv(sch);
772 struct tc_fifo_qopt opt = { .limit = q->limit };
773
Patrick McHardy1e904742008-01-22 22:11:17 -0800774 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800775 return skb->len;
776
Patrick McHardy1e904742008-01-22 22:11:17 -0800777nla_put_failure:
Stephen Hemminger300ce172005-10-30 13:47:34 -0800778 return -1;
779}
780
Eric Dumazet20fea082007-11-14 01:44:41 -0800781static struct Qdisc_ops tfifo_qdisc_ops __read_mostly = {
Stephen Hemminger300ce172005-10-30 13:47:34 -0800782 .id = "tfifo",
783 .priv_size = sizeof(struct fifo_sched_data),
784 .enqueue = tfifo_enqueue,
785 .dequeue = qdisc_dequeue_head,
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700786 .peek = qdisc_peek_head,
Stephen Hemminger300ce172005-10-30 13:47:34 -0800787 .drop = qdisc_queue_drop,
788 .init = tfifo_init,
789 .reset = qdisc_reset_queue,
790 .change = tfifo_init,
791 .dump = tfifo_dump,
792};
793
Patrick McHardy1e904742008-01-22 22:11:17 -0800794static int netem_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct netem_sched_data *q = qdisc_priv(sch);
797 int ret;
798
799 if (!opt)
800 return -EINVAL;
801
Patrick McHardy59cb5c62007-03-16 01:20:31 -0700802 qdisc_watchdog_init(&q->watchdog, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
stephen hemminger661b7972011-02-23 13:04:21 +0000804 q->loss_model = CLG_RANDOM;
Changli Gao3511c912010-10-16 13:04:08 +0000805 q->qdisc = qdisc_create_dflt(sch->dev_queue, &tfifo_qdisc_ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800806 TC_H_MAKE(sch->handle, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!q->qdisc) {
stephen hemminger250a65f2011-02-23 13:04:22 +0000808 pr_notice("netem: qdisc create tfifo qdisc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -ENOMEM;
810 }
811
812 ret = netem_change(sch, opt);
813 if (ret) {
stephen hemminger250a65f2011-02-23 13:04:22 +0000814 pr_info("netem: change failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 qdisc_destroy(q->qdisc);
816 }
817 return ret;
818}
819
820static void netem_destroy(struct Qdisc *sch)
821{
822 struct netem_sched_data *q = qdisc_priv(sch);
823
Patrick McHardy59cb5c62007-03-16 01:20:31 -0700824 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 qdisc_destroy(q->qdisc);
stephen hemminger6373a9a2011-02-23 13:04:18 +0000826 dist_free(q->delay_dist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
stephen hemminger661b7972011-02-23 13:04:21 +0000829static int dump_loss_model(const struct netem_sched_data *q,
830 struct sk_buff *skb)
831{
832 struct nlattr *nest;
833
834 nest = nla_nest_start(skb, TCA_NETEM_LOSS);
835 if (nest == NULL)
836 goto nla_put_failure;
837
838 switch (q->loss_model) {
839 case CLG_RANDOM:
840 /* legacy loss model */
841 nla_nest_cancel(skb, nest);
842 return 0; /* no data */
843
844 case CLG_4_STATES: {
845 struct tc_netem_gimodel gi = {
846 .p13 = q->clg.a1,
847 .p31 = q->clg.a2,
848 .p32 = q->clg.a3,
849 .p14 = q->clg.a4,
850 .p23 = q->clg.a5,
851 };
852
853 NLA_PUT(skb, NETEM_LOSS_GI, sizeof(gi), &gi);
854 break;
855 }
856 case CLG_GILB_ELL: {
857 struct tc_netem_gemodel ge = {
858 .p = q->clg.a1,
859 .r = q->clg.a2,
860 .h = q->clg.a3,
861 .k1 = q->clg.a4,
862 };
863
864 NLA_PUT(skb, NETEM_LOSS_GE, sizeof(ge), &ge);
865 break;
866 }
867 }
868
869 nla_nest_end(skb, nest);
870 return 0;
871
872nla_put_failure:
873 nla_nest_cancel(skb, nest);
874 return -1;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
878{
879 const struct netem_sched_data *q = qdisc_priv(sch);
stephen hemminger861d7f72011-02-23 13:04:17 +0000880 struct nlattr *nla = (struct nlattr *) skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct tc_netem_qopt qopt;
882 struct tc_netem_corr cor;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700883 struct tc_netem_reorder reorder;
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800884 struct tc_netem_corrupt corrupt;
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000885 struct tc_netem_rate rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 qopt.latency = q->latency;
888 qopt.jitter = q->jitter;
889 qopt.limit = q->limit;
890 qopt.loss = q->loss;
891 qopt.gap = q->gap;
892 qopt.duplicate = q->duplicate;
Patrick McHardy1e904742008-01-22 22:11:17 -0800893 NLA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 cor.delay_corr = q->delay_cor.rho;
896 cor.loss_corr = q->loss_cor.rho;
897 cor.dup_corr = q->dup_cor.rho;
Patrick McHardy1e904742008-01-22 22:11:17 -0800898 NLA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700899
900 reorder.probability = q->reorder;
901 reorder.correlation = q->reorder_cor.rho;
Patrick McHardy1e904742008-01-22 22:11:17 -0800902 NLA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700903
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800904 corrupt.probability = q->corrupt;
905 corrupt.correlation = q->corrupt_cor.rho;
Patrick McHardy1e904742008-01-22 22:11:17 -0800906 NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800907
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000908 rate.rate = q->rate;
909 NLA_PUT(skb, TCA_NETEM_RATE, sizeof(rate), &rate);
910
stephen hemminger661b7972011-02-23 13:04:21 +0000911 if (dump_loss_model(q, skb) != 0)
912 goto nla_put_failure;
913
stephen hemminger861d7f72011-02-23 13:04:17 +0000914 return nla_nest_end(skb, nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Patrick McHardy1e904742008-01-22 22:11:17 -0800916nla_put_failure:
stephen hemminger861d7f72011-02-23 13:04:17 +0000917 nlmsg_trim(skb, nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -1;
919}
920
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000921static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
922 struct sk_buff *skb, struct tcmsg *tcm)
923{
924 struct netem_sched_data *q = qdisc_priv(sch);
925
926 if (cl != 1) /* only one class */
927 return -ENOENT;
928
929 tcm->tcm_handle |= TC_H_MIN(1);
930 tcm->tcm_info = q->qdisc->handle;
931
932 return 0;
933}
934
935static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
936 struct Qdisc **old)
937{
938 struct netem_sched_data *q = qdisc_priv(sch);
939
940 if (new == NULL)
941 new = &noop_qdisc;
942
943 sch_tree_lock(sch);
944 *old = q->qdisc;
945 q->qdisc = new;
946 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
947 qdisc_reset(*old);
948 sch_tree_unlock(sch);
949
950 return 0;
951}
952
953static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
954{
955 struct netem_sched_data *q = qdisc_priv(sch);
956 return q->qdisc;
957}
958
959static unsigned long netem_get(struct Qdisc *sch, u32 classid)
960{
961 return 1;
962}
963
964static void netem_put(struct Qdisc *sch, unsigned long arg)
965{
966}
967
968static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
969{
970 if (!walker->stop) {
971 if (walker->count >= walker->skip)
972 if (walker->fn(sch, 1, walker) < 0) {
973 walker->stop = 1;
974 return;
975 }
976 walker->count++;
977 }
978}
979
980static const struct Qdisc_class_ops netem_class_ops = {
981 .graft = netem_graft,
982 .leaf = netem_leaf,
983 .get = netem_get,
984 .put = netem_put,
985 .walk = netem_walk,
986 .dump = netem_dump_class,
987};
988
Eric Dumazet20fea082007-11-14 01:44:41 -0800989static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 .id = "netem",
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000991 .cl_ops = &netem_class_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 .priv_size = sizeof(struct netem_sched_data),
993 .enqueue = netem_enqueue,
994 .dequeue = netem_dequeue,
Jarek Poplawski77be1552008-10-31 00:47:01 -0700995 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 .drop = netem_drop,
997 .init = netem_init,
998 .reset = netem_reset,
999 .destroy = netem_destroy,
1000 .change = netem_change,
1001 .dump = netem_dump,
1002 .owner = THIS_MODULE,
1003};
1004
1005
1006static int __init netem_module_init(void)
1007{
Stephen Hemmingereb229c42005-11-03 13:49:01 -08001008 pr_info("netem: version " VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return register_qdisc(&netem_qdisc_ops);
1010}
1011static void __exit netem_module_exit(void)
1012{
1013 unregister_qdisc(&netem_qdisc_ops);
1014}
1015module_init(netem_module_init)
1016module_exit(netem_module_exit)
1017MODULE_LICENSE("GPL");