sch_netem.c 23 KB

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