sch_netem.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. /* netem can't properly corrupt a megapacket (like we get from GSO), so instead
  334. * when we statistically choose to corrupt one, we instead segment it, returning
  335. * the first packet to be corrupted, and re-enqueue the remaining frames
  336. */
  337. static struct sk_buff *netem_segment(struct sk_buff *skb, struct Qdisc *sch)
  338. {
  339. struct sk_buff *segs;
  340. netdev_features_t features = netif_skb_features(skb);
  341. segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
  342. if (IS_ERR_OR_NULL(segs)) {
  343. qdisc_reshape_fail(skb, sch);
  344. return NULL;
  345. }
  346. consume_skb(skb);
  347. return segs;
  348. }
  349. /*
  350. * Insert one skb into qdisc.
  351. * Note: parent depends on return value to account for queue length.
  352. * NET_XMIT_DROP: queue length didn't change.
  353. * NET_XMIT_SUCCESS: one skb was queued.
  354. */
  355. static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  356. {
  357. struct netem_sched_data *q = qdisc_priv(sch);
  358. /* We don't fill cb now as skb_unshare() may invalidate it */
  359. struct netem_skb_cb *cb;
  360. struct sk_buff *skb2;
  361. struct sk_buff *segs = NULL;
  362. unsigned int len = 0, last_len, prev_len = qdisc_pkt_len(skb);
  363. int nb = 0;
  364. int count = 1;
  365. int rc = NET_XMIT_SUCCESS;
  366. /* Do not fool qdisc_drop_all() */
  367. skb->prev = NULL;
  368. /* Random duplication */
  369. if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
  370. ++count;
  371. /* Drop packet? */
  372. if (loss_event(q)) {
  373. if (q->ecn && INET_ECN_set_ce(skb))
  374. qdisc_qstats_drop(sch); /* mark packet */
  375. else
  376. --count;
  377. }
  378. if (count == 0) {
  379. qdisc_qstats_drop(sch);
  380. kfree_skb(skb);
  381. return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  382. }
  383. /* If a delay is expected, orphan the skb. (orphaning usually takes
  384. * place at TX completion time, so _before_ the link transit delay)
  385. */
  386. if (q->latency || q->jitter)
  387. skb_orphan_partial(skb);
  388. /*
  389. * If we need to duplicate packet, then re-insert at top of the
  390. * qdisc tree, since parent queuer expects that only one
  391. * skb will be queued.
  392. */
  393. if (count > 1 && (skb2 = skb_clone(skb, GFP_ATOMIC)) != NULL) {
  394. struct Qdisc *rootq = qdisc_root(sch);
  395. u32 dupsave = q->duplicate; /* prevent duplicating a dup... */
  396. q->duplicate = 0;
  397. rootq->enqueue(skb2, rootq);
  398. q->duplicate = dupsave;
  399. }
  400. /*
  401. * Randomized packet corruption.
  402. * Make copy if needed since we are modifying
  403. * If packet is going to be hardware checksummed, then
  404. * do it now in software before we mangle it.
  405. */
  406. if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
  407. if (skb_is_gso(skb)) {
  408. segs = netem_segment(skb, sch);
  409. if (!segs)
  410. return NET_XMIT_DROP;
  411. } else {
  412. segs = skb;
  413. }
  414. skb = segs;
  415. segs = segs->next;
  416. if (!(skb = skb_unshare(skb, GFP_ATOMIC)) ||
  417. (skb->ip_summed == CHECKSUM_PARTIAL &&
  418. skb_checksum_help(skb))) {
  419. rc = qdisc_drop(skb, sch);
  420. goto finish_segs;
  421. }
  422. skb->data[prandom_u32() % skb_headlen(skb)] ^=
  423. 1<<(prandom_u32() % 8);
  424. }
  425. if (unlikely(skb_queue_len(&sch->q) >= sch->limit))
  426. return qdisc_reshape_fail(skb, sch);
  427. qdisc_qstats_backlog_inc(sch, skb);
  428. cb = netem_skb_cb(skb);
  429. if (q->gap == 0 || /* not doing reordering */
  430. q->counter < q->gap - 1 || /* inside last reordering gap */
  431. q->reorder < get_crandom(&q->reorder_cor)) {
  432. psched_time_t now;
  433. psched_tdiff_t delay;
  434. delay = tabledist(q->latency, q->jitter,
  435. &q->delay_cor, q->delay_dist);
  436. now = psched_get_time();
  437. if (q->rate) {
  438. struct sk_buff *last;
  439. if (!skb_queue_empty(&sch->q))
  440. last = skb_peek_tail(&sch->q);
  441. else
  442. last = netem_rb_to_skb(rb_last(&q->t_root));
  443. if (last) {
  444. /*
  445. * Last packet in queue is reference point (now),
  446. * calculate this time bonus and subtract
  447. * from delay.
  448. */
  449. delay -= netem_skb_cb(last)->time_to_send - now;
  450. delay = max_t(psched_tdiff_t, 0, delay);
  451. now = netem_skb_cb(last)->time_to_send;
  452. }
  453. delay += packet_len_2_sched_time(qdisc_pkt_len(skb), q);
  454. }
  455. cb->time_to_send = now + delay;
  456. cb->tstamp_save = skb->tstamp;
  457. ++q->counter;
  458. tfifo_enqueue(skb, sch);
  459. } else {
  460. /*
  461. * Do re-ordering by putting one out of N packets at the front
  462. * of the queue.
  463. */
  464. cb->time_to_send = psched_get_time();
  465. q->counter = 0;
  466. __skb_queue_head(&sch->q, skb);
  467. sch->qstats.requeues++;
  468. }
  469. finish_segs:
  470. if (segs) {
  471. while (segs) {
  472. skb2 = segs->next;
  473. segs->next = NULL;
  474. qdisc_skb_cb(segs)->pkt_len = segs->len;
  475. last_len = segs->len;
  476. rc = qdisc_enqueue(segs, sch);
  477. if (rc != NET_XMIT_SUCCESS) {
  478. if (net_xmit_drop_count(rc))
  479. qdisc_qstats_drop(sch);
  480. } else {
  481. nb++;
  482. len += last_len;
  483. }
  484. segs = skb2;
  485. }
  486. sch->q.qlen += nb;
  487. if (nb > 1)
  488. qdisc_tree_reduce_backlog(sch, 1 - nb, prev_len - len);
  489. }
  490. return NET_XMIT_SUCCESS;
  491. }
  492. static unsigned int netem_drop(struct Qdisc *sch)
  493. {
  494. struct netem_sched_data *q = qdisc_priv(sch);
  495. unsigned int len;
  496. len = qdisc_queue_drop(sch);
  497. if (!len) {
  498. struct rb_node *p = rb_first(&q->t_root);
  499. if (p) {
  500. struct sk_buff *skb = netem_rb_to_skb(p);
  501. rb_erase(p, &q->t_root);
  502. sch->q.qlen--;
  503. skb->next = NULL;
  504. skb->prev = NULL;
  505. qdisc_qstats_backlog_dec(sch, skb);
  506. kfree_skb(skb);
  507. }
  508. }
  509. if (!len && q->qdisc && q->qdisc->ops->drop)
  510. len = q->qdisc->ops->drop(q->qdisc);
  511. if (len)
  512. qdisc_qstats_drop(sch);
  513. return len;
  514. }
  515. static struct sk_buff *netem_dequeue(struct Qdisc *sch)
  516. {
  517. struct netem_sched_data *q = qdisc_priv(sch);
  518. struct sk_buff *skb;
  519. struct rb_node *p;
  520. if (qdisc_is_throttled(sch))
  521. return NULL;
  522. tfifo_dequeue:
  523. skb = __skb_dequeue(&sch->q);
  524. if (skb) {
  525. qdisc_qstats_backlog_dec(sch, skb);
  526. deliver:
  527. qdisc_unthrottled(sch);
  528. qdisc_bstats_update(sch, skb);
  529. return skb;
  530. }
  531. p = rb_first(&q->t_root);
  532. if (p) {
  533. psched_time_t time_to_send;
  534. skb = netem_rb_to_skb(p);
  535. /* if more time remaining? */
  536. time_to_send = netem_skb_cb(skb)->time_to_send;
  537. if (time_to_send <= psched_get_time()) {
  538. rb_erase(p, &q->t_root);
  539. sch->q.qlen--;
  540. qdisc_qstats_backlog_dec(sch, skb);
  541. skb->next = NULL;
  542. skb->prev = NULL;
  543. skb->tstamp = netem_skb_cb(skb)->tstamp_save;
  544. #ifdef CONFIG_NET_CLS_ACT
  545. /*
  546. * If it's at ingress let's pretend the delay is
  547. * from the network (tstamp will be updated).
  548. */
  549. if (G_TC_FROM(skb->tc_verd) & AT_INGRESS)
  550. skb->tstamp.tv64 = 0;
  551. #endif
  552. if (q->qdisc) {
  553. unsigned int pkt_len = qdisc_pkt_len(skb);
  554. int err = qdisc_enqueue(skb, q->qdisc);
  555. if (err != NET_XMIT_SUCCESS &&
  556. net_xmit_drop_count(err)) {
  557. qdisc_qstats_drop(sch);
  558. qdisc_tree_reduce_backlog(sch, 1,
  559. pkt_len);
  560. }
  561. goto tfifo_dequeue;
  562. }
  563. goto deliver;
  564. }
  565. if (q->qdisc) {
  566. skb = q->qdisc->ops->dequeue(q->qdisc);
  567. if (skb)
  568. goto deliver;
  569. }
  570. qdisc_watchdog_schedule(&q->watchdog, time_to_send);
  571. }
  572. if (q->qdisc) {
  573. skb = q->qdisc->ops->dequeue(q->qdisc);
  574. if (skb)
  575. goto deliver;
  576. }
  577. return NULL;
  578. }
  579. static void netem_reset(struct Qdisc *sch)
  580. {
  581. struct netem_sched_data *q = qdisc_priv(sch);
  582. qdisc_reset_queue(sch);
  583. tfifo_reset(sch);
  584. if (q->qdisc)
  585. qdisc_reset(q->qdisc);
  586. qdisc_watchdog_cancel(&q->watchdog);
  587. }
  588. static void dist_free(struct disttable *d)
  589. {
  590. kvfree(d);
  591. }
  592. /*
  593. * Distribution data is a variable size payload containing
  594. * signed 16 bit values.
  595. */
  596. static int get_dist_table(struct Qdisc *sch, const struct nlattr *attr)
  597. {
  598. struct netem_sched_data *q = qdisc_priv(sch);
  599. size_t n = nla_len(attr)/sizeof(__s16);
  600. const __s16 *data = nla_data(attr);
  601. spinlock_t *root_lock;
  602. struct disttable *d;
  603. int i;
  604. size_t s;
  605. if (n > NETEM_DIST_MAX)
  606. return -EINVAL;
  607. s = sizeof(struct disttable) + n * sizeof(s16);
  608. d = kmalloc(s, GFP_KERNEL | __GFP_NOWARN);
  609. if (!d)
  610. d = vmalloc(s);
  611. if (!d)
  612. return -ENOMEM;
  613. d->size = n;
  614. for (i = 0; i < n; i++)
  615. d->table[i] = data[i];
  616. root_lock = qdisc_root_sleeping_lock(sch);
  617. spin_lock_bh(root_lock);
  618. swap(q->delay_dist, d);
  619. spin_unlock_bh(root_lock);
  620. dist_free(d);
  621. return 0;
  622. }
  623. static void get_correlation(struct netem_sched_data *q, const struct nlattr *attr)
  624. {
  625. const struct tc_netem_corr *c = nla_data(attr);
  626. init_crandom(&q->delay_cor, c->delay_corr);
  627. init_crandom(&q->loss_cor, c->loss_corr);
  628. init_crandom(&q->dup_cor, c->dup_corr);
  629. }
  630. static void get_reorder(struct netem_sched_data *q, const struct nlattr *attr)
  631. {
  632. const struct tc_netem_reorder *r = nla_data(attr);
  633. q->reorder = r->probability;
  634. init_crandom(&q->reorder_cor, r->correlation);
  635. }
  636. static void get_corrupt(struct netem_sched_data *q, const struct nlattr *attr)
  637. {
  638. const struct tc_netem_corrupt *r = nla_data(attr);
  639. q->corrupt = r->probability;
  640. init_crandom(&q->corrupt_cor, r->correlation);
  641. }
  642. static void get_rate(struct netem_sched_data *q, const struct nlattr *attr)
  643. {
  644. const struct tc_netem_rate *r = nla_data(attr);
  645. q->rate = r->rate;
  646. q->packet_overhead = r->packet_overhead;
  647. q->cell_size = r->cell_size;
  648. q->cell_overhead = r->cell_overhead;
  649. if (q->cell_size)
  650. q->cell_size_reciprocal = reciprocal_value(q->cell_size);
  651. else
  652. q->cell_size_reciprocal = (struct reciprocal_value) { 0 };
  653. }
  654. static int get_loss_clg(struct netem_sched_data *q, const struct nlattr *attr)
  655. {
  656. const struct nlattr *la;
  657. int rem;
  658. nla_for_each_nested(la, attr, rem) {
  659. u16 type = nla_type(la);
  660. switch (type) {
  661. case NETEM_LOSS_GI: {
  662. const struct tc_netem_gimodel *gi = nla_data(la);
  663. if (nla_len(la) < sizeof(struct tc_netem_gimodel)) {
  664. pr_info("netem: incorrect gi model size\n");
  665. return -EINVAL;
  666. }
  667. q->loss_model = CLG_4_STATES;
  668. q->clg.state = TX_IN_GAP_PERIOD;
  669. q->clg.a1 = gi->p13;
  670. q->clg.a2 = gi->p31;
  671. q->clg.a3 = gi->p32;
  672. q->clg.a4 = gi->p14;
  673. q->clg.a5 = gi->p23;
  674. break;
  675. }
  676. case NETEM_LOSS_GE: {
  677. const struct tc_netem_gemodel *ge = nla_data(la);
  678. if (nla_len(la) < sizeof(struct tc_netem_gemodel)) {
  679. pr_info("netem: incorrect ge model size\n");
  680. return -EINVAL;
  681. }
  682. q->loss_model = CLG_GILB_ELL;
  683. q->clg.state = GOOD_STATE;
  684. q->clg.a1 = ge->p;
  685. q->clg.a2 = ge->r;
  686. q->clg.a3 = ge->h;
  687. q->clg.a4 = ge->k1;
  688. break;
  689. }
  690. default:
  691. pr_info("netem: unknown loss type %u\n", type);
  692. return -EINVAL;
  693. }
  694. }
  695. return 0;
  696. }
  697. static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
  698. [TCA_NETEM_CORR] = { .len = sizeof(struct tc_netem_corr) },
  699. [TCA_NETEM_REORDER] = { .len = sizeof(struct tc_netem_reorder) },
  700. [TCA_NETEM_CORRUPT] = { .len = sizeof(struct tc_netem_corrupt) },
  701. [TCA_NETEM_RATE] = { .len = sizeof(struct tc_netem_rate) },
  702. [TCA_NETEM_LOSS] = { .type = NLA_NESTED },
  703. [TCA_NETEM_ECN] = { .type = NLA_U32 },
  704. [TCA_NETEM_RATE64] = { .type = NLA_U64 },
  705. };
  706. static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
  707. const struct nla_policy *policy, int len)
  708. {
  709. int nested_len = nla_len(nla) - NLA_ALIGN(len);
  710. if (nested_len < 0) {
  711. pr_info("netem: invalid attributes len %d\n", nested_len);
  712. return -EINVAL;
  713. }
  714. if (nested_len >= nla_attr_size(0))
  715. return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
  716. nested_len, policy);
  717. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  718. return 0;
  719. }
  720. /* Parse netlink message to set options */
  721. static int netem_change(struct Qdisc *sch, struct nlattr *opt)
  722. {
  723. struct netem_sched_data *q = qdisc_priv(sch);
  724. struct nlattr *tb[TCA_NETEM_MAX + 1];
  725. struct tc_netem_qopt *qopt;
  726. struct clgstate old_clg;
  727. int old_loss_model = CLG_RANDOM;
  728. int ret;
  729. if (opt == NULL)
  730. return -EINVAL;
  731. qopt = nla_data(opt);
  732. ret = parse_attr(tb, TCA_NETEM_MAX, opt, netem_policy, sizeof(*qopt));
  733. if (ret < 0)
  734. return ret;
  735. /* backup q->clg and q->loss_model */
  736. old_clg = q->clg;
  737. old_loss_model = q->loss_model;
  738. if (tb[TCA_NETEM_LOSS]) {
  739. ret = get_loss_clg(q, tb[TCA_NETEM_LOSS]);
  740. if (ret) {
  741. q->loss_model = old_loss_model;
  742. return ret;
  743. }
  744. } else {
  745. q->loss_model = CLG_RANDOM;
  746. }
  747. if (tb[TCA_NETEM_DELAY_DIST]) {
  748. ret = get_dist_table(sch, tb[TCA_NETEM_DELAY_DIST]);
  749. if (ret) {
  750. /* recover clg and loss_model, in case of
  751. * q->clg and q->loss_model were modified
  752. * in get_loss_clg()
  753. */
  754. q->clg = old_clg;
  755. q->loss_model = old_loss_model;
  756. return ret;
  757. }
  758. }
  759. sch->limit = qopt->limit;
  760. q->latency = qopt->latency;
  761. q->jitter = qopt->jitter;
  762. q->limit = qopt->limit;
  763. q->gap = qopt->gap;
  764. q->counter = 0;
  765. q->loss = qopt->loss;
  766. q->duplicate = qopt->duplicate;
  767. /* for compatibility with earlier versions.
  768. * if gap is set, need to assume 100% probability
  769. */
  770. if (q->gap)
  771. q->reorder = ~0;
  772. if (tb[TCA_NETEM_CORR])
  773. get_correlation(q, tb[TCA_NETEM_CORR]);
  774. if (tb[TCA_NETEM_REORDER])
  775. get_reorder(q, tb[TCA_NETEM_REORDER]);
  776. if (tb[TCA_NETEM_CORRUPT])
  777. get_corrupt(q, tb[TCA_NETEM_CORRUPT]);
  778. if (tb[TCA_NETEM_RATE])
  779. get_rate(q, tb[TCA_NETEM_RATE]);
  780. if (tb[TCA_NETEM_RATE64])
  781. q->rate = max_t(u64, q->rate,
  782. nla_get_u64(tb[TCA_NETEM_RATE64]));
  783. if (tb[TCA_NETEM_ECN])
  784. q->ecn = nla_get_u32(tb[TCA_NETEM_ECN]);
  785. return ret;
  786. }
  787. static int netem_init(struct Qdisc *sch, struct nlattr *opt)
  788. {
  789. struct netem_sched_data *q = qdisc_priv(sch);
  790. int ret;
  791. qdisc_watchdog_init(&q->watchdog, sch);
  792. if (!opt)
  793. return -EINVAL;
  794. q->loss_model = CLG_RANDOM;
  795. ret = netem_change(sch, opt);
  796. if (ret)
  797. pr_info("netem: change failed\n");
  798. return ret;
  799. }
  800. static void netem_destroy(struct Qdisc *sch)
  801. {
  802. struct netem_sched_data *q = qdisc_priv(sch);
  803. qdisc_watchdog_cancel(&q->watchdog);
  804. if (q->qdisc)
  805. qdisc_destroy(q->qdisc);
  806. dist_free(q->delay_dist);
  807. }
  808. static int dump_loss_model(const struct netem_sched_data *q,
  809. struct sk_buff *skb)
  810. {
  811. struct nlattr *nest;
  812. nest = nla_nest_start(skb, TCA_NETEM_LOSS);
  813. if (nest == NULL)
  814. goto nla_put_failure;
  815. switch (q->loss_model) {
  816. case CLG_RANDOM:
  817. /* legacy loss model */
  818. nla_nest_cancel(skb, nest);
  819. return 0; /* no data */
  820. case CLG_4_STATES: {
  821. struct tc_netem_gimodel gi = {
  822. .p13 = q->clg.a1,
  823. .p31 = q->clg.a2,
  824. .p32 = q->clg.a3,
  825. .p14 = q->clg.a4,
  826. .p23 = q->clg.a5,
  827. };
  828. if (nla_put(skb, NETEM_LOSS_GI, sizeof(gi), &gi))
  829. goto nla_put_failure;
  830. break;
  831. }
  832. case CLG_GILB_ELL: {
  833. struct tc_netem_gemodel ge = {
  834. .p = q->clg.a1,
  835. .r = q->clg.a2,
  836. .h = q->clg.a3,
  837. .k1 = q->clg.a4,
  838. };
  839. if (nla_put(skb, NETEM_LOSS_GE, sizeof(ge), &ge))
  840. goto nla_put_failure;
  841. break;
  842. }
  843. }
  844. nla_nest_end(skb, nest);
  845. return 0;
  846. nla_put_failure:
  847. nla_nest_cancel(skb, nest);
  848. return -1;
  849. }
  850. static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
  851. {
  852. const struct netem_sched_data *q = qdisc_priv(sch);
  853. struct nlattr *nla = (struct nlattr *) skb_tail_pointer(skb);
  854. struct tc_netem_qopt qopt;
  855. struct tc_netem_corr cor;
  856. struct tc_netem_reorder reorder;
  857. struct tc_netem_corrupt corrupt;
  858. struct tc_netem_rate rate;
  859. qopt.latency = q->latency;
  860. qopt.jitter = q->jitter;
  861. qopt.limit = q->limit;
  862. qopt.loss = q->loss;
  863. qopt.gap = q->gap;
  864. qopt.duplicate = q->duplicate;
  865. if (nla_put(skb, TCA_OPTIONS, sizeof(qopt), &qopt))
  866. goto nla_put_failure;
  867. cor.delay_corr = q->delay_cor.rho;
  868. cor.loss_corr = q->loss_cor.rho;
  869. cor.dup_corr = q->dup_cor.rho;
  870. if (nla_put(skb, TCA_NETEM_CORR, sizeof(cor), &cor))
  871. goto nla_put_failure;
  872. reorder.probability = q->reorder;
  873. reorder.correlation = q->reorder_cor.rho;
  874. if (nla_put(skb, TCA_NETEM_REORDER, sizeof(reorder), &reorder))
  875. goto nla_put_failure;
  876. corrupt.probability = q->corrupt;
  877. corrupt.correlation = q->corrupt_cor.rho;
  878. if (nla_put(skb, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt))
  879. goto nla_put_failure;
  880. if (q->rate >= (1ULL << 32)) {
  881. if (nla_put_u64(skb, TCA_NETEM_RATE64, q->rate))
  882. goto nla_put_failure;
  883. rate.rate = ~0U;
  884. } else {
  885. rate.rate = q->rate;
  886. }
  887. rate.packet_overhead = q->packet_overhead;
  888. rate.cell_size = q->cell_size;
  889. rate.cell_overhead = q->cell_overhead;
  890. if (nla_put(skb, TCA_NETEM_RATE, sizeof(rate), &rate))
  891. goto nla_put_failure;
  892. if (q->ecn && nla_put_u32(skb, TCA_NETEM_ECN, q->ecn))
  893. goto nla_put_failure;
  894. if (dump_loss_model(q, skb) != 0)
  895. goto nla_put_failure;
  896. return nla_nest_end(skb, nla);
  897. nla_put_failure:
  898. nlmsg_trim(skb, nla);
  899. return -1;
  900. }
  901. static int netem_dump_class(struct Qdisc *sch, unsigned long cl,
  902. struct sk_buff *skb, struct tcmsg *tcm)
  903. {
  904. struct netem_sched_data *q = qdisc_priv(sch);
  905. if (cl != 1 || !q->qdisc) /* only one class */
  906. return -ENOENT;
  907. tcm->tcm_handle |= TC_H_MIN(1);
  908. tcm->tcm_info = q->qdisc->handle;
  909. return 0;
  910. }
  911. static int netem_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  912. struct Qdisc **old)
  913. {
  914. struct netem_sched_data *q = qdisc_priv(sch);
  915. *old = qdisc_replace(sch, new, &q->qdisc);
  916. return 0;
  917. }
  918. static struct Qdisc *netem_leaf(struct Qdisc *sch, unsigned long arg)
  919. {
  920. struct netem_sched_data *q = qdisc_priv(sch);
  921. return q->qdisc;
  922. }
  923. static unsigned long netem_get(struct Qdisc *sch, u32 classid)
  924. {
  925. return 1;
  926. }
  927. static void netem_put(struct Qdisc *sch, unsigned long arg)
  928. {
  929. }
  930. static void netem_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  931. {
  932. if (!walker->stop) {
  933. if (walker->count >= walker->skip)
  934. if (walker->fn(sch, 1, walker) < 0) {
  935. walker->stop = 1;
  936. return;
  937. }
  938. walker->count++;
  939. }
  940. }
  941. static const struct Qdisc_class_ops netem_class_ops = {
  942. .graft = netem_graft,
  943. .leaf = netem_leaf,
  944. .get = netem_get,
  945. .put = netem_put,
  946. .walk = netem_walk,
  947. .dump = netem_dump_class,
  948. };
  949. static struct Qdisc_ops netem_qdisc_ops __read_mostly = {
  950. .id = "netem",
  951. .cl_ops = &netem_class_ops,
  952. .priv_size = sizeof(struct netem_sched_data),
  953. .enqueue = netem_enqueue,
  954. .dequeue = netem_dequeue,
  955. .peek = qdisc_peek_dequeued,
  956. .drop = netem_drop,
  957. .init = netem_init,
  958. .reset = netem_reset,
  959. .destroy = netem_destroy,
  960. .change = netem_change,
  961. .dump = netem_dump,
  962. .owner = THIS_MODULE,
  963. };
  964. static int __init netem_module_init(void)
  965. {
  966. pr_info("netem: version " VERSION "\n");
  967. return register_qdisc(&netem_qdisc_ops);
  968. }
  969. static void __exit netem_module_exit(void)
  970. {
  971. unregister_qdisc(&netem_qdisc_ops);
  972. }
  973. module_init(netem_module_init)
  974. module_exit(netem_module_exit)
  975. MODULE_LICENSE("GPL");