sch_netem.c 26 KB

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