sch_netem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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.
  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. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/rtnetlink.h>
  23. #include <net/netlink.h>
  24. #include <net/pkt_sched.h>
  25. #define VERSION "1.3"
  26. /* Network Emulation Queuing algorithm.
  27. ====================================
  28. Sources: [1] Mark Carson, Darrin Santay, "NIST Net - A Linux-based
  29. Network Emulation Tool
  30. [2] Luigi Rizzo, DummyNet for FreeBSD
  31. ----------------------------------------------------------------
  32. This started out as a simple way to delay outgoing packets to
  33. test TCP but has grown to include most of the functionality
  34. of a full blown network emulator like NISTnet. It can delay
  35. packets and add random jitter (and correlation). The random
  36. distribution can be loaded from a table as well to provide
  37. normal, Pareto, or experimental curves. Packet loss,
  38. duplication, and reordering can also be emulated.
  39. This qdisc does not do classification that can be handled in
  40. layering other disciplines. It does not need to do bandwidth
  41. control either since that can be handled by using token
  42. bucket or other rate control.
  43. Correlated Loss Generator models
  44. Added generation of correlated loss according to the
  45. "Gilbert-Elliot" model, a 4-state markov model.
  46. References:
  47. [1] NetemCLG Home http://netgroup.uniroma2.it/NetemCLG
  48. [2] S. Salsano, F. Ludovici, A. Ordine, "Definition of a general
  49. and intuitive loss model for packet networks and its implementation
  50. in the Netem module in the Linux kernel", available in [1]
  51. Authors: Stefano Salsano <stefano.salsano at uniroma2.it
  52. Fabio Ludovici <fabio.ludovici at yahoo.it>
  53. */
  54. struct netem_sched_data {
  55. struct Qdisc *qdisc;
  56. struct qdisc_watchdog watchdog;
  57. psched_tdiff_t latency;
  58. psched_tdiff_t jitter;
  59. u32 loss;
  60. u32 limit;
  61. u32 counter;
  62. u32 gap;
  63. u32 duplicate;
  64. u32 reorder;
  65. u32 corrupt;
  66. struct crndstate {
  67. u32 last;
  68. u32 rho;
  69. } delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor;
  70. struct disttable {
  71. u32 size;
  72. s16 table[0];
  73. } *delay_dist;
  74. enum {
  75. CLG_RANDOM,
  76. CLG_4_STATES,
  77. CLG_GILB_ELL,
  78. } loss_model;
  79. /* Correlated Loss Generation models */
  80. struct clgstate {
  81. /* state of the Markov chain */
  82. u8 state;
  83. /* 4-states and Gilbert-Elliot models */
  84. u32 a1; /* p13 for 4-states or p for GE */
  85. u32 a2; /* p31 for 4-states or r for GE */
  86. u32 a3; /* p32 for 4-states or h for GE */
  87. u32 a4; /* p14 for 4-states or 1-k for GE */
  88. u32 a5; /* p23 used only in 4-states */
  89. } clg;
  90. };
  91. /* Time stamp put into socket buffer control block */
  92. struct netem_skb_cb {
  93. psched_time_t time_to_send;
  94. };
  95. static inline struct netem_skb_cb *netem_skb_cb(struct sk_buff *skb)
  96. {
  97. qdisc_cb_private_validate(skb, sizeof(struct netem_skb_cb));
  98. return (struct netem_skb_cb *)qdisc_skb_cb(skb)->data;
  99. }
  100. /* init_crandom - initialize correlated random number generator
  101. * Use entropy source for initial seed.
  102. */
  103. static void init_crandom(struct crndstate *state, unsigned long rho)
  104. {
  105. state->rho = rho;
  106. state->last = net_random();
  107. }
  108. /* get_crandom - correlated random number generator
  109. * Next number depends on last value.
  110. * rho is scaled to avoid floating point.
  111. */
  112. static u32 get_crandom(struct crndstate *state)
  113. {
  114. u64 value, rho;
  115. unsigned long answer;
  116. if (state->rho == 0) /* no correlation */
  117. return net_random();
  118. value = net_random();
  119. rho = (u64)state->rho + 1;
  120. answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
  121. state->last = answer;
  122. return answer;
  123. }
  124. /* loss_4state - 4-state model loss generator
  125. * Generates losses according to the 4-state Markov chain adopted in
  126. * the GI (General and Intuitive) loss model.
  127. */
  128. static bool loss_4state(struct netem_sched_data *q)
  129. {
  130. struct clgstate *clg = &q->clg;
  131. u32 rnd = net_random();
  132. /*
  133. * Makes a comparison between rnd and the transition
  134. * probabilities outgoing from the current state, then decides the
  135. * next state and if the next packet has to be transmitted or lost.
  136. * The four states correspond to:
  137. * 1 => successfully transmitted packets within a gap period
  138. * 4 => isolated losses within a gap period
  139. * 3 => lost packets within a burst period
  140. * 2 => successfully transmitted packets within a burst period
  141. */
  142. switch (clg->state) {
  143. case 1:
  144. if (rnd < clg->a4) {
  145. clg->state = 4;
  146. return true;
  147. } else if (clg->a4 < rnd && rnd < clg->a1) {
  148. clg->state = 3;
  149. return true;
  150. } else if (clg->a1 < rnd)
  151. clg->state = 1;
  152. break;
  153. case 2:
  154. if (rnd < clg->a5) {
  155. clg->state = 3;
  156. return true;
  157. } else
  158. clg->state = 2;
  159. break;
  160. case 3:
  161. if (rnd < clg->a3)
  162. clg->state = 2;
  163. else if (clg->a3 < rnd && rnd < clg->a2 + clg->a3) {
  164. clg->state = 1;
  165. return true;
  166. } else if (clg->a2 + clg->a3 < rnd) {
  167. clg->state = 3;
  168. return true;
  169. }
  170. break;
  171. case 4:
  172. clg->state = 1;
  173. break;
  174. }
  175. return false;
  176. }
  177. /* loss_gilb_ell - Gilbert-Elliot model loss generator
  178. * Generates losses according to the Gilbert-Elliot loss model or
  179. * its special cases (Gilbert or Simple Gilbert)
  180. *
  181. * Makes a comparison between random number and the transition
  182. * probabilities outgoing from the current state, then decides the
  183. * next state. A second random number is extracted and the comparison
  184. * with the loss probability of the current state decides if the next
  185. * packet will be transmitted or lost.
  186. */
  187. static bool loss_gilb_ell(struct netem_sched_data *q)
  188. {
  189. struct clgstate *clg = &q->clg;
  190. switch (clg->state) {
  191. case 1:
  192. if (net_random() < clg->a1)
  193. clg->state = 2;
  194. if (net_random() < clg->a4)
  195. return true;
  196. case 2:
  197. if (net_random() < clg->a2)
  198. clg->state = 1;
  199. if (clg->a3 > net_random())
  200. return true;
  201. }
  202. return false;
  203. }
  204. static bool loss_event(struct netem_sched_data *q)
  205. {
  206. switch (q->loss_model) {
  207. case CLG_RANDOM:
  208. /* Random packet drop 0 => none, ~0 => all */
  209. return q->loss && q->loss >= get_crandom(&q->loss_cor);
  210. case CLG_4_STATES:
  211. /* 4state loss model algorithm (used also for GI model)
  212. * Extracts a value from the markov 4 state loss generator,
  213. * if it is 1 drops a packet and if needed writes the event in
  214. * the kernel logs
  215. */
  216. return loss_4state(q);
  217. case CLG_GILB_ELL:
  218. /* Gilbert-Elliot loss model algorithm
  219. * Extracts a value from the Gilbert-Elliot loss generator,
  220. * if it is 1 drops a packet and if needed writes the event in
  221. * the kernel logs
  222. */
  223. return loss_gilb_ell(q);
  224. }
  225. return false; /* not reached */
  226. }
  227. /* tabledist - return a pseudo-randomly distributed value with mean mu and
  228. * std deviation sigma. Uses table lookup to approximate the desired
  229. * distribution, and a uniformly-distributed pseudo-random source.
  230. */
  231. static psched_tdiff_t tabledist(psched_tdiff_t mu, psched_tdiff_t sigma,
  232. struct crndstate *state,
  233. const struct disttable *dist)
  234. {
  235. psched_tdiff_t x;
  236. long t;
  237. u32 rnd;
  238. if (sigma == 0)
  239. return mu;
  240. rnd = get_crandom(state);
  241. /* default uniform distribution */
  242. if (dist == NULL)
  243. return (rnd % (2*sigma)) - sigma + mu;
  244. t = dist->table[rnd % dist->size];
  245. x = (sigma % NETEM_DIST_SCALE) * t;
  246. if (x >= 0)
  247. x += NETEM_DIST_SCALE/2;
  248. else
  249. x -= NETEM_DIST_SCALE/2;
  250. return x / NETEM_DIST_SCALE + (sigma / NETEM_DIST_SCALE) * t + mu;
  251. }
  252. /*
  253. * Insert one skb into qdisc.
  254. * Note: parent depends on return value to account for queue length.
  255. * NET_XMIT_DROP: queue length didn't change.
  256. * NET_XMIT_SUCCESS: one skb was queued.
  257. */
  258. static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  259. {
  260. struct netem_sched_data *q = qdisc_priv(sch);
  261. /* We don't fill cb now as skb_unshare() may invalidate it */
  262. struct netem_skb_cb *cb;
  263. struct sk_buff *skb2;
  264. int ret;
  265. int count = 1;
  266. /* Random duplication */
  267. if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
  268. ++count;
  269. /* Drop packet? */
  270. if (loss_event(q))
  271. --count;
  272. if (count == 0) {
  273. sch->qstats.drops++;
  274. kfree_skb(skb);
  275. return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  276. }
  277. skb_orphan(skb);
  278. /*
  279. * If we need to duplicate packet, then re-insert at top of the
  280. * qdisc tree, since parent queuer expects that only one
  281. * skb will be queued.
  282. */
  283. if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
  284. struct Qdisc *rootq = qdisc_root(sch);
  285. u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
  286. q->duplicate = 0;
  287. qdisc_enqueue_root(skb2, rootq);
  288. q->duplicate = dupsave;
  289. }
  290. /*
  291. * Randomized packet corruption.
  292. * Make copy if needed since we are modifying
  293. * If packet is going to be hardware checksummed, then
  294. * do it now in software before we mangle it.
  295. */
  296. if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
  297. if (!(skb = skb_unshare(skb, GFP_ATOMIC)) ||
  298. (skb->ip_summed == CHECKSUM_PARTIAL &&
  299. skb_checksum_help(skb))) {
  300. sch->qstats.drops++;
  301. return NET_XMIT_DROP;
  302. }
  303. skb->data[net_random() % skb_headlen(skb)] ^= 1<<(net_random() % 8);
  304. }
  305. cb = netem_skb_cb(skb);
  306. if (q->gap == 0 || /* not doing reordering */
  307. q->counter < q->gap || /* inside last reordering gap */
  308. q->reorder < get_crandom(&q->reorder_cor)) {
  309. psched_time_t now;
  310. psched_tdiff_t delay;
  311. delay = tabledist(q->latency, q->jitter,
  312. &q->delay_cor, q->delay_dist);
  313. now = psched_get_time();
  314. cb->time_to_send = now + delay;
  315. ++q->counter;
  316. ret = qdisc_enqueue(skb, q->qdisc);
  317. } else {
  318. /*
  319. * Do re-ordering by putting one out of N packets at the front
  320. * of the queue.
  321. */
  322. cb->time_to_send = psched_get_time();
  323. q->counter = 0;
  324. __skb_queue_head(&q->qdisc->q, skb);
  325. sch->qstats.backlog += qdisc_pkt_len(skb);
  326. sch->qstats.requeues++;
  327. ret = NET_XMIT_SUCCESS;
  328. }
  329. if (ret != NET_XMIT_SUCCESS) {
  330. if (net_xmit_drop_count(ret)) {
  331. sch->qstats.drops++;
  332. return ret;
  333. }
  334. }
  335. sch->q.qlen++;
  336. return NET_XMIT_SUCCESS;
  337. }
  338. static unsigned int netem_drop(struct Qdisc *sch)
  339. {
  340. struct netem_sched_data *q = qdisc_priv(sch);
  341. unsigned int len = 0;
  342. if (q->qdisc->ops->drop && (len = q->qdisc->ops->drop(q->qdisc)) != 0) {
  343. sch->q.qlen--;
  344. sch->qstats.drops++;
  345. }
  346. return len;
  347. }
  348. static struct sk_buff *netem_dequeue(struct Qdisc *sch)
  349. {
  350. struct netem_sched_data *q = qdisc_priv(sch);
  351. struct sk_buff *skb;
  352. if (qdisc_is_throttled(sch))
  353. return NULL;
  354. skb = q->qdisc->ops->peek(q->qdisc);
  355. if (skb) {
  356. const struct netem_skb_cb *cb = netem_skb_cb(skb);
  357. psched_time_t now = psched_get_time();
  358. /* if more time remaining? */
  359. if (cb->time_to_send <= now) {
  360. skb = qdisc_dequeue_peeked(q->qdisc);
  361. if (unlikely(!skb))
  362. return NULL;
  363. #ifdef CONFIG_NET_CLS_ACT
  364. /*
  365. * If it's at ingress let's pretend the delay is
  366. * from the network (tstamp will be updated).
  367. */
  368. if (G_TC_FROM(skb->tc_verd) & AT_INGRESS)
  369. skb->tstamp.tv64 = 0;
  370. #endif
  371. sch->q.qlen--;
  372. qdisc_unthrottled(sch);
  373. qdisc_bstats_update(sch, skb);
  374. return skb;
  375. }
  376. qdisc_watchdog_schedule(&q->watchdog, cb->time_to_send);
  377. }
  378. return NULL;
  379. }
  380. static void netem_reset(struct Qdisc *sch)
  381. {
  382. struct netem_sched_data *q = qdisc_priv(sch);
  383. qdisc_reset(q->qdisc);
  384. sch->q.qlen = 0;
  385. qdisc_watchdog_cancel(&q->watchdog);
  386. }
  387. static void dist_free(struct disttable *d)
  388. {
  389. if (d) {
  390. if (is_vmalloc_addr(d))
  391. vfree(d);
  392. else
  393. kfree(d);
  394. }
  395. }
  396. /*
  397. * Distribution data is a variable size payload containing
  398. * signed 16 bit values.
  399. */
  400. static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
  401. {
  402. struct netem_sched_data *q = qdisc_priv(sch);
  403. size_t n = nla_len(attr)/sizeof(__s16);
  404. const __s16 *data = nla_data(attr);
  405. spinlock_t *root_lock;
  406. struct disttable *d;
  407. int i;
  408. size_t s;
  409. if (n > NETEM_DIST_MAX)
  410. return -EINVAL;
  411. s = sizeof(struct disttable) + n * sizeof(s16);
  412. d = kmalloc(s, GFP_KERNEL);
  413. if (!d)
  414. d = vmalloc(s);
  415. if (!d)
  416. return -ENOMEM;
  417. d->size = n;
  418. for (i = 0; i < n; i++)
  419. d->table[i] = data[i];
  420. root_lock = qdisc_root_sleeping_lock(sch);
  421. spin_lock_bh(root_lock);
  422. dist_free(q->delay_dist);
  423. q->delay_dist = d;
  424. spin_unlock_bh(root_lock);
  425. return 0;
  426. }
  427. static void get_correlation(struct Qdisc *sch, const struct nlattr *attr)
  428. {
  429. struct netem_sched_data *q = qdisc_priv(sch);
  430. const struct tc_netem_corr *c = nla_data(attr);
  431. init_crandom(&q->delay_cor, c->delay_corr);
  432. init_crandom(&q->loss_cor, c->loss_corr);
  433. init_crandom(&q->dup_cor, c->dup_corr);
  434. }
  435. static void get_reorder(struct Qdisc *sch, const struct nlattr *attr)
  436. {
  437. struct netem_sched_data *q = qdisc_priv(sch);
  438. const struct tc_netem_reorder *r = nla_data(attr);
  439. q->reorder = r->probability;
  440. init_crandom(&q->reorder_cor, r->correlation);
  441. }
  442. static void get_corrupt(struct Qdisc *sch, const struct nlattr *attr)
  443. {
  444. struct netem_sched_data *q = qdisc_priv(sch);
  445. const struct tc_netem_corrupt *r = nla_data(attr);
  446. q->corrupt = r->probability;
  447. init_crandom(&q->corrupt_cor, r->correlation);
  448. }
  449. static int get_loss_clg(struct Qdisc *sch, const struct nlattr *attr)
  450. {
  451. struct netem_sched_data *q = qdisc_priv(sch);
  452. const struct nlattr *la;
  453. int rem;
  454. nla_for_each_nested(la, attr, rem) {
  455. u16 type = nla_type(la);
  456. switch(type) {
  457. case NETEM_LOSS_GI: {
  458. const struct tc_netem_gimodel *gi = nla_data(la);
  459. if (nla_len(la) != sizeof(struct tc_netem_gimodel)) {
  460. pr_info("netem: incorrect gi model size\n");
  461. return -EINVAL;
  462. }
  463. q->loss_model = CLG_4_STATES;
  464. q->clg.state = 1;
  465. q->clg.a1 = gi->p13;
  466. q->clg.a2 = gi->p31;
  467. q->clg.a3 = gi->p32;
  468. q->clg.a4 = gi->p14;
  469. q->clg.a5 = gi->p23;
  470. break;
  471. }
  472. case NETEM_LOSS_GE: {
  473. const struct tc_netem_gemodel *ge = nla_data(la);
  474. if (nla_len(la) != sizeof(struct tc_netem_gemodel)) {
  475. pr_info("netem: incorrect gi model size\n");
  476. return -EINVAL;
  477. }
  478. q->loss_model = CLG_GILB_ELL;
  479. q->clg.state = 1;
  480. q->clg.a1 = ge->p;
  481. q->clg.a2 = ge->r;
  482. q->clg.a3 = ge->h;
  483. q->clg.a4 = ge->k1;
  484. break;
  485. }
  486. default:
  487. pr_info("netem: unknown loss type %u\n", type);
  488. return -EINVAL;
  489. }
  490. }
  491. return 0;
  492. }
  493. static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
  494. [TCA_NETEM_CORR] = { .len = sizeof(struct tc_netem_corr) },
  495. [TCA_NETEM_REORDER] = { .len = sizeof(struct tc_netem_reorder) },
  496. [TCA_NETEM_CORRUPT] = { .len = sizeof(struct tc_netem_corrupt) },
  497. [TCA_NETEM_LOSS] = { .type = NLA_NESTED },
  498. };
  499. static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
  500. const struct nla_policy *policy, int len)
  501. {
  502. int nested_len = nla_len(nla) - NLA_ALIGN(len);
  503. if (nested_len < 0) {
  504. pr_info("netem: invalid attributes len %d\n", nested_len);
  505. return -EINVAL;
  506. }
  507. if (nested_len >= nla_attr_size(0))
  508. return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
  509. nested_len, policy);
  510. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  511. return 0;
  512. }
  513. /* Parse netlink message to set options */
  514. static int netem_change(struct Qdisc *sch, struct nlattr *opt)
  515. {
  516. struct netem_sched_data *q = qdisc_priv(sch);
  517. struct nlattr *tb[TCA_NETEM_MAX + 1];
  518. struct tc_netem_qopt *qopt;
  519. int ret;
  520. if (opt == NULL)
  521. return -EINVAL;
  522. qopt = nla_data(opt);
  523. ret = parse_attr(tb, TCA_NETEM_MAX, opt, netem_policy, sizeof(*qopt));
  524. if (ret < 0)
  525. return ret;
  526. ret = fifo_set_limit(q->qdisc, qopt->limit);
  527. if (ret) {
  528. pr_info("netem: can't set fifo limit\n");
  529. return ret;
  530. }
  531. q->latency = qopt->latency;
  532. q->jitter = qopt->jitter;
  533. q->limit = qopt->limit;
  534. q->gap = qopt->gap;
  535. q->counter = 0;
  536. q->loss = qopt->loss;
  537. q->duplicate = qopt->duplicate;
  538. /* for compatibility with earlier versions.
  539. * if gap is set, need to assume 100% probability
  540. */
  541. if (q->gap)
  542. q->reorder = ~0;
  543. if (tb[TCA_NETEM_CORR])
  544. get_correlation(sch, tb[TCA_NETEM_CORR]);
  545. if (tb[TCA_NETEM_DELAY_DIST]) {
  546. ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
  547. if (ret)
  548. return ret;
  549. }
  550. if (tb[TCA_NETEM_REORDER])
  551. get_reorder(sch, tb[TCA_NETEM_REORDER]);
  552. if (tb[TCA_NETEM_CORRUPT])
  553. get_corrupt(sch, tb[TCA_NETEM_CORRUPT]);
  554. q->loss_model = CLG_RANDOM;
  555. if (tb[TCA_NETEM_LOSS])
  556. ret = get_loss_clg(sch, tb[TCA_NETEM_LOSS]);
  557. return ret;
  558. }
  559. /*
  560. * Special case version of FIFO queue for use by netem.
  561. * It queues in order based on timestamps in skb's
  562. */
  563. struct fifo_sched_data {
  564. u32 limit;
  565. psched_time_t oldest;
  566. };
  567. static int tfifo_enqueue(struct sk_buff *nskb, struct Qdisc *sch)
  568. {
  569. struct fifo_sched_data *q = qdisc_priv(sch);
  570. struct sk_buff_head *list = &sch->q;
  571. psched_time_t tnext = netem_skb_cb(nskb)->time_to_send;
  572. struct sk_buff *skb;
  573. if (likely(skb_queue_len(list) < q->limit)) {
  574. /* Optimize for add at tail */
  575. if (likely(skb_queue_empty(list) || tnext >= q->oldest)) {
  576. q->oldest = tnext;
  577. return qdisc_enqueue_tail(nskb, sch);
  578. }
  579. skb_queue_reverse_walk(list, skb) {
  580. const struct netem_skb_cb *cb = netem_skb_cb(skb);
  581. if (tnext >= cb->time_to_send)
  582. break;
  583. }
  584. __skb_queue_after(list, skb, nskb);
  585. sch->qstats.backlog += qdisc_pkt_len(nskb);
  586. return NET_XMIT_SUCCESS;
  587. }
  588. return qdisc_reshape_fail(nskb, sch);
  589. }
  590. static int tfifo_init(struct Qdisc *sch, struct nlattr *opt)
  591. {
  592. struct fifo_sched_data *q = qdisc_priv(sch);
  593. if (opt) {
  594. struct tc_fifo_qopt *ctl = nla_data(opt);
  595. if (nla_len(opt) < sizeof(*ctl))
  596. return -EINVAL;
  597. q->limit = ctl->limit;
  598. } else
  599. q->limit = max_t(u32, qdisc_dev(sch)->tx_queue_len, 1);
  600. q->oldest = PSCHED_PASTPERFECT;
  601. return 0;
  602. }
  603. static int tfifo_dump(struct Qdisc *sch, struct sk_buff *skb)
  604. {
  605. struct fifo_sched_data *q = qdisc_priv(sch);
  606. struct tc_fifo_qopt opt = { .limit = q->limit };
  607. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  608. return skb->len;
  609. nla_put_failure:
  610. return -1;
  611. }
  612. static struct Qdisc_ops tfifo_qdisc_ops __read_mostly = {
  613. .id = "tfifo",
  614. .priv_size = sizeof(struct fifo_sched_data),
  615. .enqueue = tfifo_enqueue,
  616. .dequeue = qdisc_dequeue_head,
  617. .peek = qdisc_peek_head,
  618. .drop = qdisc_queue_drop,
  619. .init = tfifo_init,
  620. .reset = qdisc_reset_queue,
  621. .change = tfifo_init,
  622. .dump = tfifo_dump,
  623. };
  624. static int netem_init(struct Qdisc *sch, struct nlattr *opt)
  625. {
  626. struct netem_sched_data *q = qdisc_priv(sch);
  627. int ret;
  628. if (!opt)
  629. return -EINVAL;
  630. qdisc_watchdog_init(&q->watchdog, sch);
  631. q->loss_model = CLG_RANDOM;
  632. q->qdisc = qdisc_create_dflt(sch->dev_queue, &tfifo_qdisc_ops,
  633. TC_H_MAKE(sch->handle, 1));
  634. if (!q->qdisc) {
  635. pr_notice("netem: qdisc create tfifo qdisc failed\n");
  636. return -ENOMEM;
  637. }
  638. ret = netem_change(sch, opt);
  639. if (ret) {
  640. pr_info("netem: change failed\n");
  641. qdisc_destroy(q->qdisc);
  642. }
  643. return ret;
  644. }
  645. static void netem_destroy(struct Qdisc *sch)
  646. {
  647. struct netem_sched_data *q = qdisc_priv(sch);
  648. qdisc_watchdog_cancel(&q->watchdog);
  649. qdisc_destroy(q->qdisc);
  650. dist_free(q->delay_dist);
  651. }
  652. static int dump_loss_model(const struct netem_sched_data *q,
  653. struct sk_buff *skb)
  654. {
  655. struct nlattr *nest;
  656. nest = nla_nest_start(skb, TCA_NETEM_LOSS);
  657. if (nest == NULL)
  658. goto nla_put_failure;
  659. switch (q->loss_model) {
  660. case CLG_RANDOM:
  661. /* legacy loss model */
  662. nla_nest_cancel(skb, nest);
  663. return 0; /* no data */
  664. case CLG_4_STATES: {
  665. struct tc_netem_gimodel gi = {
  666. .p13 = q->clg.a1,
  667. .p31 = q->clg.a2,
  668. .p32 = q->clg.a3,
  669. .p14 = q->clg.a4,
  670. .p23 = q->clg.a5,
  671. };
  672. NLA_PUT(skb, NETEM_LOSS_GI, sizeof(gi), &gi);
  673. break;
  674. }
  675. case CLG_GILB_ELL: {
  676. struct tc_netem_gemodel ge = {
  677. .p = q->clg.a1,
  678. .r = q->clg.a2,
  679. .h = q->clg.a3,
  680. .k1 = q->clg.a4,
  681. };
  682. NLA_PUT(skb, NETEM_LOSS_GE, sizeof(ge), &ge);
  683. break;
  684. }
  685. }
  686. nla_nest_end(skb, nest);
  687. return 0;
  688. nla_put_failure:
  689. nla_nest_cancel(skb, nest);
  690. return -1;
  691. }
  692. static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
  693. {
  694. const struct netem_sched_data *q = qdisc_priv(sch);
  695. struct nlattr *nla = (struct nlattr *) skb_tail_pointer(skb);
  696. struct tc_netem_qopt qopt;
  697. struct tc_netem_corr cor;
  698. struct tc_netem_reorder reorder;
  699. struct tc_netem_corrupt corrupt;
  700. qopt.latency = q->latency;
  701. qopt.jitter = q->jitter;
  702. qopt.limit = q->limit;
  703. qopt.loss = q->loss;
  704. qopt.gap = q->gap;
  705. qopt.duplicate = q->duplicate;
  706. NLA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
  707. cor.delay_corr = q->delay_cor.rho;
  708. cor.loss_corr = q->loss_cor.rho;
  709. cor.dup_corr = q->dup_cor.rho;
  710. NLA_PUT(skb, TCA_NETEM_CORR, sizeof(cor), &cor);
  711. reorder.probability = q->reorder;
  712. reorder.correlation = q->reorder_cor.rho;
  713. NLA_PUT(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
  714. corrupt.probability = q->corrupt;
  715. corrupt.correlation = q->corrupt_cor.rho;
  716. NLA_PUT(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
  717. if (dump_loss_model(q, skb) != 0)
  718. goto nla_put_failure;
  719. return nla_nest_end(skb, nla);
  720. nla_put_failure:
  721. nlmsg_trim(skb, nla);
  722. return -1;
  723. }
  724. static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
  725. struct sk_buff *skb, struct tcmsg *tcm)
  726. {
  727. struct netem_sched_data *q = qdisc_priv(sch);
  728. if (cl != 1) /* only one class */
  729. return -ENOENT;
  730. tcm->tcm_handle |= TC_H_MIN(1);
  731. tcm->tcm_info = q->qdisc->handle;
  732. return 0;
  733. }
  734. static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  735. struct Qdisc **old)
  736. {
  737. struct netem_sched_data *q = qdisc_priv(sch);
  738. if (new == NULL)
  739. new = &noop_qdisc;
  740. sch_tree_lock(sch);
  741. *old = q->qdisc;
  742. q->qdisc = new;
  743. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  744. qdisc_reset(*old);
  745. sch_tree_unlock(sch);
  746. return 0;
  747. }
  748. static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
  749. {
  750. struct netem_sched_data *q = qdisc_priv(sch);
  751. return q->qdisc;
  752. }
  753. static unsigned long netem_get(struct Qdisc *sch, u32 classid)
  754. {
  755. return 1;
  756. }
  757. static void netem_put(struct Qdisc *sch, unsigned long arg)
  758. {
  759. }
  760. static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  761. {
  762. if (!walker->stop) {
  763. if (walker->count >= walker->skip)
  764. if (walker->fn(sch, 1, walker) < 0) {
  765. walker->stop = 1;
  766. return;
  767. }
  768. walker->count++;
  769. }
  770. }
  771. static const struct Qdisc_class_ops netem_class_ops = {
  772. .graft = netem_graft,
  773. .leaf = netem_leaf,
  774. .get = netem_get,
  775. .put = netem_put,
  776. .walk = netem_walk,
  777. .dump = netem_dump_class,
  778. };
  779. static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
  780. .id = "netem",
  781. .cl_ops = &netem_class_ops,
  782. .priv_size = sizeof(struct netem_sched_data),
  783. .enqueue = netem_enqueue,
  784. .dequeue = netem_dequeue,
  785. .peek = qdisc_peek_dequeued,
  786. .drop = netem_drop,
  787. .init = netem_init,
  788. .reset = netem_reset,
  789. .destroy = netem_destroy,
  790. .change = netem_change,
  791. .dump = netem_dump,
  792. .owner = THIS_MODULE,
  793. };
  794. static int __init netem_module_init(void)
  795. {
  796. pr_info("netem: version " VERSION "\n");
  797. return register_qdisc(&netem_qdisc_ops);
  798. }
  799. static void __exit netem_module_exit(void)
  800. {
  801. unregister_qdisc(&netem_qdisc_ops);
  802. }
  803. module_init(netem_module_init)
  804. module_exit(netem_module_exit)
  805. MODULE_LICENSE("GPL");