sch_netem.c 26 KB

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