blob: 3bfd73344f76380b40230ac3920e67ba2031959b [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{
Eric Dumazetfc33cc72011-11-30 23:32:14 +0000304 u64 ticks = (u64)len * NSEC_PER_SEC;
305
306 do_div(ticks, rate);
307 return PSCHED_NS2TICKS(ticks);
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000308}
309
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700310/*
311 * Insert one skb into qdisc.
312 * Note: parent depends on return value to account for queue length.
313 * NET_XMIT_DROP: queue length didn't change.
314 * NET_XMIT_SUCCESS: one skb was queued.
315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
317{
318 struct netem_sched_data *q = qdisc_priv(sch);
Guillaume Chazarain89e1df72006-07-21 14:45:25 -0700319 /* We don't fill cb now as skb_unshare() may invalidate it */
320 struct netem_skb_cb *cb;
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700321 struct sk_buff *skb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int ret;
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700323 int count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700325 /* Random duplication */
326 if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
327 ++count;
328
stephen hemminger661b7972011-02-23 13:04:21 +0000329 /* Drop packet? */
330 if (loss_event(q))
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700331 --count;
332
333 if (count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 sch->qstats.drops++;
335 kfree_skb(skb);
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700336 return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
David S. Miller4e8a5202006-10-22 21:00:33 -0700339 skb_orphan(skb);
340
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700341 /*
342 * If we need to duplicate packet, then re-insert at top of the
343 * qdisc tree, since parent queuer expects that only one
344 * skb will be queued.
345 */
346 if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
David S. Miller7698b4f2008-07-16 01:42:40 -0700347 struct Qdisc *rootq = qdisc_root(sch);
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700348 u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
349 q->duplicate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700351 qdisc_enqueue_root(skb2, rootq);
Stephen Hemminger0afb51e2005-05-26 12:53:49 -0700352 q->duplicate = dupsave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800355 /*
356 * Randomized packet corruption.
357 * Make copy if needed since we are modifying
358 * If packet is going to be hardware checksummed, then
359 * do it now in software before we mangle it.
360 */
361 if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
Joe Perchesf64f9e72009-11-29 16:55:45 -0800362 if (!(skb = skb_unshare(skb, GFP_ATOMIC)) ||
363 (skb->ip_summed == CHECKSUM_PARTIAL &&
364 skb_checksum_help(skb))) {
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800365 sch->qstats.drops++;
366 return NET_XMIT_DROP;
367 }
368
369 skb->data[net_random() % skb_headlen(skb)] ^= 1<<(net_random() % 8);
370 }
371
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700372 cb = netem_skb_cb(skb);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000373 if (q->gap == 0 || /* not doing reordering */
374 q->counter < q->gap || /* inside last reordering gap */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800375 q->reorder < get_crandom(&q->reorder_cor)) {
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700376 psched_time_t now;
Stephen Hemminger07aaa112005-11-03 13:43:07 -0800377 psched_tdiff_t delay;
378
379 delay = tabledist(q->latency, q->jitter,
380 &q->delay_cor, q->delay_dist);
381
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700382 now = psched_get_time();
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000383
384 if (q->rate) {
385 struct sk_buff_head *list = &q->qdisc->q;
386
387 delay += packet_len_2_sched_time(skb->len, q->rate);
388
389 if (!skb_queue_empty(list)) {
390 /*
391 * Last packet in queue is reference point (now).
392 * First packet in queue is already in flight,
393 * calculate this time bonus and substract
394 * from delay.
395 */
396 delay -= now - netem_skb_cb(skb_peek(list))->time_to_send;
397 now = netem_skb_cb(skb_peek_tail(list))->time_to_send;
398 }
399 }
400
Patrick McHardy7c59e252007-03-23 11:27:45 -0700401 cb->time_to_send = now + delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 ++q->counter;
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700403 ret = qdisc_enqueue(skb, q->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 } else {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900405 /*
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700406 * Do re-ordering by putting one out of N packets at the front
407 * of the queue.
408 */
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700409 cb->time_to_send = psched_get_time();
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700410 q->counter = 0;
Jarek Poplawski8ba25da2008-11-02 00:36:03 -0700411
412 __skb_queue_head(&q->qdisc->q, skb);
413 q->qdisc->qstats.backlog += qdisc_pkt_len(skb);
414 q->qdisc->qstats.requeues++;
415 ret = NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000418 if (ret != NET_XMIT_SUCCESS) {
419 if (net_xmit_drop_count(ret)) {
420 sch->qstats.drops++;
421 return ret;
422 }
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000425 sch->q.qlen++;
426 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000429static unsigned int netem_drop(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy6d037a22006-03-20 19:00:49 -0800432 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Patrick McHardy6d037a22006-03-20 19:00:49 -0800434 if (q->qdisc->ops->drop && (len = q->qdisc->ops->drop(q->qdisc)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sch->q.qlen--;
436 sch->qstats.drops++;
437 }
438 return len;
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static struct sk_buff *netem_dequeue(struct Qdisc *sch)
442{
443 struct netem_sched_data *q = qdisc_priv(sch);
444 struct sk_buff *skb;
445
Eric Dumazetfd245a42011-01-20 05:27:16 +0000446 if (qdisc_is_throttled(sch))
Stephen Hemminger11274e52007-03-22 12:17:42 -0700447 return NULL;
448
Jarek Poplawski03c05f02008-10-31 00:46:19 -0700449 skb = q->qdisc->ops->peek(q->qdisc);
Stephen Hemminger771018e2005-05-03 16:24:32 -0700450 if (skb) {
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700451 const struct netem_skb_cb *cb = netem_skb_cb(skb);
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700452 psched_time_t now = psched_get_time();
Stephen Hemminger771018e2005-05-03 16:24:32 -0700453
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700454 /* if more time remaining? */
Patrick McHardy104e0872007-03-23 11:28:07 -0700455 if (cb->time_to_send <= now) {
Jarek Poplawski77be1552008-10-31 00:47:01 -0700456 skb = qdisc_dequeue_peeked(q->qdisc);
457 if (unlikely(!skb))
Jarek Poplawski03c05f02008-10-31 00:46:19 -0700458 return NULL;
459
Jarek Poplawski8caf1532009-04-17 10:08:49 +0000460#ifdef CONFIG_NET_CLS_ACT
461 /*
462 * If it's at ingress let's pretend the delay is
463 * from the network (tstamp will be updated).
464 */
465 if (G_TC_FROM(skb->tc_verd) & AT_INGRESS)
466 skb->tstamp.tv64 = 0;
467#endif
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000468
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700469 sch->q.qlen--;
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000470 qdisc_unthrottled(sch);
471 qdisc_bstats_update(sch, skb);
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700472 return skb;
473 }
Stephen Hemminger11274e52007-03-22 12:17:42 -0700474
Stephen Hemminger11274e52007-03-22 12:17:42 -0700475 qdisc_watchdog_schedule(&q->watchdog, cb->time_to_send);
Stephen Hemminger0f9f32a2005-05-26 12:55:01 -0700476 }
477
478 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static void netem_reset(struct Qdisc *sch)
482{
483 struct netem_sched_data *q = qdisc_priv(sch);
484
485 qdisc_reset(q->qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 sch->q.qlen = 0;
Patrick McHardy59cb5c62007-03-16 01:20:31 -0700487 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
stephen hemminger6373a9a2011-02-23 13:04:18 +0000490static void dist_free(struct disttable *d)
491{
492 if (d) {
493 if (is_vmalloc_addr(d))
494 vfree(d);
495 else
496 kfree(d);
497 }
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * Distribution data is a variable size payload containing
502 * signed 16 bit values.
503 */
Patrick McHardy1e904742008-01-22 22:11:17 -0800504static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct netem_sched_data *q = qdisc_priv(sch);
stephen hemminger6373a9a2011-02-23 13:04:18 +0000507 size_t n = nla_len(attr)/sizeof(__s16);
Patrick McHardy1e904742008-01-22 22:11:17 -0800508 const __s16 *data = nla_data(attr);
David S. Miller7698b4f2008-07-16 01:42:40 -0700509 spinlock_t *root_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct disttable *d;
511 int i;
stephen hemminger6373a9a2011-02-23 13:04:18 +0000512 size_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
stephen hemmingerdf173bd2011-02-23 13:04:19 +0000514 if (n > NETEM_DIST_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EINVAL;
516
stephen hemminger6373a9a2011-02-23 13:04:18 +0000517 s = sizeof(struct disttable) + n * sizeof(s16);
518 d = kmalloc(s, GFP_KERNEL);
519 if (!d)
520 d = vmalloc(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (!d)
522 return -ENOMEM;
523
524 d->size = n;
525 for (i = 0; i < n; i++)
526 d->table[i] = data[i];
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900527
Jarek Poplawski102396a2008-08-29 14:21:52 -0700528 root_lock = qdisc_root_sleeping_lock(sch);
David S. Miller7698b4f2008-07-16 01:42:40 -0700529
530 spin_lock_bh(root_lock);
stephen hemminger6373a9a2011-02-23 13:04:18 +0000531 dist_free(q->delay_dist);
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800532 q->delay_dist = d;
David S. Miller7698b4f2008-07-16 01:42:40 -0700533 spin_unlock_bh(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
536
Stephen Hemminger265eb672008-11-03 21:13:26 -0800537static void get_correlation(struct Qdisc *sch, const struct nlattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800540 const struct tc_netem_corr *c = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 init_crandom(&q->delay_cor, c->delay_corr);
543 init_crandom(&q->loss_cor, c->loss_corr);
544 init_crandom(&q->dup_cor, c->dup_corr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Stephen Hemminger265eb672008-11-03 21:13:26 -0800547static void get_reorder(struct Qdisc *sch, const struct nlattr *attr)
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700548{
549 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800550 const struct tc_netem_reorder *r = nla_data(attr);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700551
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700552 q->reorder = r->probability;
553 init_crandom(&q->reorder_cor, r->correlation);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700554}
555
Stephen Hemminger265eb672008-11-03 21:13:26 -0800556static void get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800557{
558 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800559 const struct tc_netem_corrupt *r = nla_data(attr);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800560
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800561 q->corrupt = r->probability;
562 init_crandom(&q->corrupt_cor, r->correlation);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800563}
564
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000565static void get_rate(struct Qdisc *sch, const struct nlattr *attr)
566{
567 struct netem_sched_data *q = qdisc_priv(sch);
568 const struct tc_netem_rate *r = nla_data(attr);
569
570 q->rate = r->rate;
571}
572
stephen hemminger661b7972011-02-23 13:04:21 +0000573static int get_loss_clg(struct Qdisc *sch, const struct nlattr *attr)
574{
575 struct netem_sched_data *q = qdisc_priv(sch);
576 const struct nlattr *la;
577 int rem;
578
579 nla_for_each_nested(la, attr, rem) {
580 u16 type = nla_type(la);
581
582 switch(type) {
583 case NETEM_LOSS_GI: {
584 const struct tc_netem_gimodel *gi = nla_data(la);
585
586 if (nla_len(la) != sizeof(struct tc_netem_gimodel)) {
587 pr_info("netem: incorrect gi model size\n");
588 return -EINVAL;
589 }
590
591 q->loss_model = CLG_4_STATES;
592
593 q->clg.state = 1;
594 q->clg.a1 = gi->p13;
595 q->clg.a2 = gi->p31;
596 q->clg.a3 = gi->p32;
597 q->clg.a4 = gi->p14;
598 q->clg.a5 = gi->p23;
599 break;
600 }
601
602 case NETEM_LOSS_GE: {
603 const struct tc_netem_gemodel *ge = nla_data(la);
604
605 if (nla_len(la) != sizeof(struct tc_netem_gemodel)) {
606 pr_info("netem: incorrect gi model size\n");
607 return -EINVAL;
608 }
609
610 q->loss_model = CLG_GILB_ELL;
611 q->clg.state = 1;
612 q->clg.a1 = ge->p;
613 q->clg.a2 = ge->r;
614 q->clg.a3 = ge->h;
615 q->clg.a4 = ge->k1;
616 break;
617 }
618
619 default:
620 pr_info("netem: unknown loss type %u\n", type);
621 return -EINVAL;
622 }
623 }
624
625 return 0;
626}
627
Patrick McHardy27a34212008-01-23 20:35:39 -0800628static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
629 [TCA_NETEM_CORR] = { .len = sizeof(struct tc_netem_corr) },
630 [TCA_NETEM_REORDER] = { .len = sizeof(struct tc_netem_reorder) },
631 [TCA_NETEM_CORRUPT] = { .len = sizeof(struct tc_netem_corrupt) },
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000632 [TCA_NETEM_RATE] = { .len = sizeof(struct tc_netem_rate) },
stephen hemminger661b7972011-02-23 13:04:21 +0000633 [TCA_NETEM_LOSS] = { .type = NLA_NESTED },
Patrick McHardy27a34212008-01-23 20:35:39 -0800634};
635
Thomas Graf2c10b322008-09-02 17:30:27 -0700636static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
637 const struct nla_policy *policy, int len)
638{
639 int nested_len = nla_len(nla) - NLA_ALIGN(len);
640
stephen hemminger661b7972011-02-23 13:04:21 +0000641 if (nested_len < 0) {
642 pr_info("netem: invalid attributes len %d\n", nested_len);
Thomas Graf2c10b322008-09-02 17:30:27 -0700643 return -EINVAL;
stephen hemminger661b7972011-02-23 13:04:21 +0000644 }
645
Thomas Graf2c10b322008-09-02 17:30:27 -0700646 if (nested_len >= nla_attr_size(0))
647 return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
648 nested_len, policy);
stephen hemminger661b7972011-02-23 13:04:21 +0000649
Thomas Graf2c10b322008-09-02 17:30:27 -0700650 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
651 return 0;
652}
653
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800654/* Parse netlink message to set options */
Patrick McHardy1e904742008-01-22 22:11:17 -0800655static int netem_change(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct netem_sched_data *q = qdisc_priv(sch);
Patrick McHardyb03f4672008-01-23 20:32:21 -0800658 struct nlattr *tb[TCA_NETEM_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct tc_netem_qopt *qopt;
660 int ret;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900661
Patrick McHardyb03f4672008-01-23 20:32:21 -0800662 if (opt == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EINVAL;
664
Thomas Graf2c10b322008-09-02 17:30:27 -0700665 qopt = nla_data(opt);
666 ret = parse_attr(tb, TCA_NETEM_MAX, opt, netem_policy, sizeof(*qopt));
Patrick McHardyb03f4672008-01-23 20:32:21 -0800667 if (ret < 0)
668 return ret;
669
Patrick McHardyfb0305c2008-07-05 23:40:21 -0700670 ret = fifo_set_limit(q->qdisc, qopt->limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (ret) {
stephen hemminger250a65f2011-02-23 13:04:22 +0000672 pr_info("netem: can't set fifo limit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return ret;
674 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 q->latency = qopt->latency;
677 q->jitter = qopt->jitter;
678 q->limit = qopt->limit;
679 q->gap = qopt->gap;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700680 q->counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 q->loss = qopt->loss;
682 q->duplicate = qopt->duplicate;
683
Stephen Hemmingerbb2f8cc2007-03-23 00:12:09 -0700684 /* for compatibility with earlier versions.
685 * if gap is set, need to assume 100% probability
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700686 */
Stephen Hemmingera362e0a2007-03-22 12:15:45 -0700687 if (q->gap)
688 q->reorder = ~0;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700689
Stephen Hemminger265eb672008-11-03 21:13:26 -0800690 if (tb[TCA_NETEM_CORR])
691 get_correlation(sch, tb[TCA_NETEM_CORR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Patrick McHardyb03f4672008-01-23 20:32:21 -0800693 if (tb[TCA_NETEM_DELAY_DIST]) {
694 ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
695 if (ret)
696 return ret;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Stephen Hemminger265eb672008-11-03 21:13:26 -0800699 if (tb[TCA_NETEM_REORDER])
700 get_reorder(sch, tb[TCA_NETEM_REORDER]);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800701
Stephen Hemminger265eb672008-11-03 21:13:26 -0800702 if (tb[TCA_NETEM_CORRUPT])
703 get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000705 if (tb[TCA_NETEM_RATE])
706 get_rate(sch, tb[TCA_NETEM_RATE]);
707
stephen hemminger661b7972011-02-23 13:04:21 +0000708 q->loss_model = CLG_RANDOM;
709 if (tb[TCA_NETEM_LOSS])
710 ret = get_loss_clg(sch, tb[TCA_NETEM_LOSS]);
711
712 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Stephen Hemminger300ce172005-10-30 13:47:34 -0800715/*
716 * Special case version of FIFO queue for use by netem.
717 * It queues in order based on timestamps in skb's
718 */
719struct fifo_sched_data {
720 u32 limit;
Stephen Hemminger075aa572007-03-22 12:17:05 -0700721 psched_time_t oldest;
Stephen Hemminger300ce172005-10-30 13:47:34 -0800722};
723
724static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
725{
726 struct fifo_sched_data *q = qdisc_priv(sch);
727 struct sk_buff_head *list = &sch->q;
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700728 psched_time_t tnext = netem_skb_cb(nskb)->time_to_send;
Stephen Hemminger300ce172005-10-30 13:47:34 -0800729 struct sk_buff *skb;
730
731 if (likely(skb_queue_len(list) < q->limit)) {
Stephen Hemminger075aa572007-03-22 12:17:05 -0700732 /* Optimize for add at tail */
Patrick McHardy104e0872007-03-23 11:28:07 -0700733 if (likely(skb_queue_empty(list) || tnext >= q->oldest)) {
Stephen Hemminger075aa572007-03-22 12:17:05 -0700734 q->oldest = tnext;
735 return qdisc_enqueue_tail(nskb, sch);
736 }
737
Stephen Hemminger300ce172005-10-30 13:47:34 -0800738 skb_queue_reverse_walk(list, skb) {
Jussi Kivilinna5f861732008-07-20 00:08:04 -0700739 const struct netem_skb_cb *cb = netem_skb_cb(skb);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800740
Patrick McHardy104e0872007-03-23 11:28:07 -0700741 if (tnext >= cb->time_to_send)
Stephen Hemminger300ce172005-10-30 13:47:34 -0800742 break;
743 }
744
745 __skb_queue_after(list, skb, nskb);
746
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700747 sch->qstats.backlog += qdisc_pkt_len(nskb);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800748
749 return NET_XMIT_SUCCESS;
750 }
751
Stephen Hemminger075aa572007-03-22 12:17:05 -0700752 return qdisc_reshape_fail(nskb, sch);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800753}
754
Patrick McHardy1e904742008-01-22 22:11:17 -0800755static int tfifo_init(struct Qdisc *sch, struct nlattr *opt)
Stephen Hemminger300ce172005-10-30 13:47:34 -0800756{
757 struct fifo_sched_data *q = qdisc_priv(sch);
758
759 if (opt) {
Patrick McHardy1e904742008-01-22 22:11:17 -0800760 struct tc_fifo_qopt *ctl = nla_data(opt);
761 if (nla_len(opt) < sizeof(*ctl))
Stephen Hemminger300ce172005-10-30 13:47:34 -0800762 return -EINVAL;
763
764 q->limit = ctl->limit;
765 } else
David S. Miller5ce2d482008-07-08 17:06:30 -0700766 q->limit = max_t(u32, qdisc_dev(sch)->tx_queue_len, 1);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800767
Patrick McHardya0849802007-03-23 11:28:30 -0700768 q->oldest = PSCHED_PASTPERFECT;
Stephen Hemminger300ce172005-10-30 13:47:34 -0800769 return 0;
770}
771
772static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
773{
774 struct fifo_sched_data *q = qdisc_priv(sch);
775 struct tc_fifo_qopt opt = { .limit = q->limit };
776
Patrick McHardy1e904742008-01-22 22:11:17 -0800777 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Stephen Hemminger300ce172005-10-30 13:47:34 -0800778 return skb->len;
779
Patrick McHardy1e904742008-01-22 22:11:17 -0800780nla_put_failure:
Stephen Hemminger300ce172005-10-30 13:47:34 -0800781 return -1;
782}
783
Eric Dumazet20fea082007-11-14 01:44:41 -0800784static struct Qdisc_ops tfifo_qdisc_ops __read_mostly = {
Stephen Hemminger300ce172005-10-30 13:47:34 -0800785 .id = "tfifo",
786 .priv_size = sizeof(struct fifo_sched_data),
787 .enqueue = tfifo_enqueue,
788 .dequeue = qdisc_dequeue_head,
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700789 .peek = qdisc_peek_head,
Stephen Hemminger300ce172005-10-30 13:47:34 -0800790 .drop = qdisc_queue_drop,
791 .init = tfifo_init,
792 .reset = qdisc_reset_queue,
793 .change = tfifo_init,
794 .dump = tfifo_dump,
795};
796
Patrick McHardy1e904742008-01-22 22:11:17 -0800797static int netem_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct netem_sched_data *q = qdisc_priv(sch);
800 int ret;
801
802 if (!opt)
803 return -EINVAL;
804
Patrick McHardy59cb5c62007-03-16 01:20:31 -0700805 qdisc_watchdog_init(&q->watchdog, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
stephen hemminger661b7972011-02-23 13:04:21 +0000807 q->loss_model = CLG_RANDOM;
Changli Gao3511c912010-10-16 13:04:08 +0000808 q->qdisc = qdisc_create_dflt(sch->dev_queue, &tfifo_qdisc_ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800809 TC_H_MAKE(sch->handle, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!q->qdisc) {
stephen hemminger250a65f2011-02-23 13:04:22 +0000811 pr_notice("netem: qdisc create tfifo qdisc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -ENOMEM;
813 }
814
815 ret = netem_change(sch, opt);
816 if (ret) {
stephen hemminger250a65f2011-02-23 13:04:22 +0000817 pr_info("netem: change failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 qdisc_destroy(q->qdisc);
819 }
820 return ret;
821}
822
823static void netem_destroy(struct Qdisc *sch)
824{
825 struct netem_sched_data *q = qdisc_priv(sch);
826
Patrick McHardy59cb5c62007-03-16 01:20:31 -0700827 qdisc_watchdog_cancel(&q->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 qdisc_destroy(q->qdisc);
stephen hemminger6373a9a2011-02-23 13:04:18 +0000829 dist_free(q->delay_dist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
stephen hemminger661b7972011-02-23 13:04:21 +0000832static int dump_loss_model(const struct netem_sched_data *q,
833 struct sk_buff *skb)
834{
835 struct nlattr *nest;
836
837 nest = nla_nest_start(skb, TCA_NETEM_LOSS);
838 if (nest == NULL)
839 goto nla_put_failure;
840
841 switch (q->loss_model) {
842 case CLG_RANDOM:
843 /* legacy loss model */
844 nla_nest_cancel(skb, nest);
845 return 0; /* no data */
846
847 case CLG_4_STATES: {
848 struct tc_netem_gimodel gi = {
849 .p13 = q->clg.a1,
850 .p31 = q->clg.a2,
851 .p32 = q->clg.a3,
852 .p14 = q->clg.a4,
853 .p23 = q->clg.a5,
854 };
855
856 NLA_PUT(skb, NETEM_LOSS_GI, sizeof(gi), &gi);
857 break;
858 }
859 case CLG_GILB_ELL: {
860 struct tc_netem_gemodel ge = {
861 .p = q->clg.a1,
862 .r = q->clg.a2,
863 .h = q->clg.a3,
864 .k1 = q->clg.a4,
865 };
866
867 NLA_PUT(skb, NETEM_LOSS_GE, sizeof(ge), &ge);
868 break;
869 }
870 }
871
872 nla_nest_end(skb, nest);
873 return 0;
874
875nla_put_failure:
876 nla_nest_cancel(skb, nest);
877 return -1;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
881{
882 const struct netem_sched_data *q = qdisc_priv(sch);
stephen hemminger861d7f72011-02-23 13:04:17 +0000883 struct nlattr *nla = (struct nlattr *) skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct tc_netem_qopt qopt;
885 struct tc_netem_corr cor;
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700886 struct tc_netem_reorder reorder;
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800887 struct tc_netem_corrupt corrupt;
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000888 struct tc_netem_rate rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 qopt.latency = q->latency;
891 qopt.jitter = q->jitter;
892 qopt.limit = q->limit;
893 qopt.loss = q->loss;
894 qopt.gap = q->gap;
895 qopt.duplicate = q->duplicate;
Patrick McHardy1e904742008-01-22 22:11:17 -0800896 NLA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 cor.delay_corr = q->delay_cor.rho;
899 cor.loss_corr = q->loss_cor.rho;
900 cor.dup_corr = q->dup_cor.rho;
Patrick McHardy1e904742008-01-22 22:11:17 -0800901 NLA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700902
903 reorder.probability = q->reorder;
904 reorder.correlation = q->reorder_cor.rho;
Patrick McHardy1e904742008-01-22 22:11:17 -0800905 NLA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
Stephen Hemminger0dca51d2005-05-26 12:55:48 -0700906
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800907 corrupt.probability = q->corrupt;
908 corrupt.correlation = q->corrupt_cor.rho;
Patrick McHardy1e904742008-01-22 22:11:17 -0800909 NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
Stephen Hemmingerc865e5d2005-12-21 19:03:44 -0800910
Hagen Paul Pfeifer7bc0f282011-11-30 12:20:26 +0000911 rate.rate = q->rate;
912 NLA_PUT(skb, TCA_NETEM_RATE, sizeof(rate), &rate);
913
stephen hemminger661b7972011-02-23 13:04:21 +0000914 if (dump_loss_model(q, skb) != 0)
915 goto nla_put_failure;
916
stephen hemminger861d7f72011-02-23 13:04:17 +0000917 return nla_nest_end(skb, nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Patrick McHardy1e904742008-01-22 22:11:17 -0800919nla_put_failure:
stephen hemminger861d7f72011-02-23 13:04:17 +0000920 nlmsg_trim(skb, nla);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -1;
922}
923
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000924static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
925 struct sk_buff *skb, struct tcmsg *tcm)
926{
927 struct netem_sched_data *q = qdisc_priv(sch);
928
929 if (cl != 1) /* only one class */
930 return -ENOENT;
931
932 tcm->tcm_handle |= TC_H_MIN(1);
933 tcm->tcm_info = q->qdisc->handle;
934
935 return 0;
936}
937
938static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
939 struct Qdisc **old)
940{
941 struct netem_sched_data *q = qdisc_priv(sch);
942
943 if (new == NULL)
944 new = &noop_qdisc;
945
946 sch_tree_lock(sch);
947 *old = q->qdisc;
948 q->qdisc = new;
949 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
950 qdisc_reset(*old);
951 sch_tree_unlock(sch);
952
953 return 0;
954}
955
956static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
957{
958 struct netem_sched_data *q = qdisc_priv(sch);
959 return q->qdisc;
960}
961
962static unsigned long netem_get(struct Qdisc *sch, u32 classid)
963{
964 return 1;
965}
966
967static void netem_put(struct Qdisc *sch, unsigned long arg)
968{
969}
970
971static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
972{
973 if (!walker->stop) {
974 if (walker->count >= walker->skip)
975 if (walker->fn(sch, 1, walker) < 0) {
976 walker->stop = 1;
977 return;
978 }
979 walker->count++;
980 }
981}
982
983static const struct Qdisc_class_ops netem_class_ops = {
984 .graft = netem_graft,
985 .leaf = netem_leaf,
986 .get = netem_get,
987 .put = netem_put,
988 .walk = netem_walk,
989 .dump = netem_dump_class,
990};
991
Eric Dumazet20fea082007-11-14 01:44:41 -0800992static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 .id = "netem",
stephen hemminger10f6dfc2011-02-23 13:04:20 +0000994 .cl_ops = &netem_class_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 .priv_size = sizeof(struct netem_sched_data),
996 .enqueue = netem_enqueue,
997 .dequeue = netem_dequeue,
Jarek Poplawski77be1552008-10-31 00:47:01 -0700998 .peek = qdisc_peek_dequeued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 .drop = netem_drop,
1000 .init = netem_init,
1001 .reset = netem_reset,
1002 .destroy = netem_destroy,
1003 .change = netem_change,
1004 .dump = netem_dump,
1005 .owner = THIS_MODULE,
1006};
1007
1008
1009static int __init netem_module_init(void)
1010{
Stephen Hemmingereb229c42005-11-03 13:49:01 -08001011 pr_info("netem: version " VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return register_qdisc(&netem_qdisc_ops);
1013}
1014static void __exit netem_module_exit(void)
1015{
1016 unregister_qdisc(&netem_qdisc_ops);
1017}
1018module_init(netem_module_init)
1019module_exit(netem_module_exit)
1020MODULE_LICENSE("GPL");