sch_hfsc.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  1. /*
  2. * Copyright (c) 2003 Patrick McHardy, <kaber@trash.net>
  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 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * 2003-10-17 - Ported from altq
  10. */
  11. /*
  12. * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
  13. *
  14. * Permission to use, copy, modify, and distribute this software and
  15. * its documentation is hereby granted (including for commercial or
  16. * for-profit use), provided that both the copyright notice and this
  17. * permission notice appear in all copies of the software, derivative
  18. * works, or modified versions, and any portions thereof.
  19. *
  20. * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
  21. * WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS
  22. * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  32. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  33. * DAMAGE.
  34. *
  35. * Carnegie Mellon encourages (but does not require) users of this
  36. * software to return any improvements or extensions that they make,
  37. * and to grant Carnegie Mellon the rights to redistribute these
  38. * changes without encumbrance.
  39. */
  40. /*
  41. * H-FSC is described in Proceedings of SIGCOMM'97,
  42. * "A Hierarchical Fair Service Curve Algorithm for Link-Sharing,
  43. * Real-Time and Priority Service"
  44. * by Ion Stoica, Hui Zhang, and T. S. Eugene Ng.
  45. *
  46. * Oleg Cherevko <olwi@aq.ml.com.ua> added the upperlimit for link-sharing.
  47. * when a class has an upperlimit, the fit-time is computed from the
  48. * upperlimit service curve. the link-sharing scheduler does not schedule
  49. * a class whose fit-time exceeds the current time.
  50. */
  51. #include <linux/kernel.h>
  52. #include <linux/module.h>
  53. #include <linux/types.h>
  54. #include <linux/errno.h>
  55. #include <linux/jiffies.h>
  56. #include <linux/compiler.h>
  57. #include <linux/spinlock.h>
  58. #include <linux/skbuff.h>
  59. #include <linux/string.h>
  60. #include <linux/slab.h>
  61. #include <linux/list.h>
  62. #include <linux/rbtree.h>
  63. #include <linux/init.h>
  64. #include <linux/netdevice.h>
  65. #include <linux/rtnetlink.h>
  66. #include <linux/pkt_sched.h>
  67. #include <net/netlink.h>
  68. #include <net/pkt_sched.h>
  69. #include <net/pkt_cls.h>
  70. #include <asm/system.h>
  71. #include <asm/div64.h>
  72. /*
  73. * kernel internal service curve representation:
  74. * coordinates are given by 64 bit unsigned integers.
  75. * x-axis: unit is clock count.
  76. * y-axis: unit is byte.
  77. *
  78. * The service curve parameters are converted to the internal
  79. * representation. The slope values are scaled to avoid overflow.
  80. * the inverse slope values as well as the y-projection of the 1st
  81. * segment are kept in order to to avoid 64-bit divide operations
  82. * that are expensive on 32-bit architectures.
  83. */
  84. struct internal_sc
  85. {
  86. u64 sm1; /* scaled slope of the 1st segment */
  87. u64 ism1; /* scaled inverse-slope of the 1st segment */
  88. u64 dx; /* the x-projection of the 1st segment */
  89. u64 dy; /* the y-projection of the 1st segment */
  90. u64 sm2; /* scaled slope of the 2nd segment */
  91. u64 ism2; /* scaled inverse-slope of the 2nd segment */
  92. };
  93. /* runtime service curve */
  94. struct runtime_sc
  95. {
  96. u64 x; /* current starting position on x-axis */
  97. u64 y; /* current starting position on y-axis */
  98. u64 sm1; /* scaled slope of the 1st segment */
  99. u64 ism1; /* scaled inverse-slope of the 1st segment */
  100. u64 dx; /* the x-projection of the 1st segment */
  101. u64 dy; /* the y-projection of the 1st segment */
  102. u64 sm2; /* scaled slope of the 2nd segment */
  103. u64 ism2; /* scaled inverse-slope of the 2nd segment */
  104. };
  105. enum hfsc_class_flags
  106. {
  107. HFSC_RSC = 0x1,
  108. HFSC_FSC = 0x2,
  109. HFSC_USC = 0x4
  110. };
  111. struct hfsc_class
  112. {
  113. u32 classid; /* class id */
  114. unsigned int refcnt; /* usage count */
  115. struct gnet_stats_basic bstats;
  116. struct gnet_stats_queue qstats;
  117. struct gnet_stats_rate_est rate_est;
  118. spinlock_t *stats_lock;
  119. unsigned int level; /* class level in hierarchy */
  120. struct tcf_proto *filter_list; /* filter list */
  121. unsigned int filter_cnt; /* filter count */
  122. struct hfsc_sched *sched; /* scheduler data */
  123. struct hfsc_class *cl_parent; /* parent class */
  124. struct list_head siblings; /* sibling classes */
  125. struct list_head children; /* child classes */
  126. struct Qdisc *qdisc; /* leaf qdisc */
  127. struct rb_node el_node; /* qdisc's eligible tree member */
  128. struct rb_root vt_tree; /* active children sorted by cl_vt */
  129. struct rb_node vt_node; /* parent's vt_tree member */
  130. struct rb_root cf_tree; /* active children sorted by cl_f */
  131. struct rb_node cf_node; /* parent's cf_heap member */
  132. struct list_head hlist; /* hash list member */
  133. struct list_head dlist; /* drop list member */
  134. u64 cl_total; /* total work in bytes */
  135. u64 cl_cumul; /* cumulative work in bytes done by
  136. real-time criteria */
  137. u64 cl_d; /* deadline*/
  138. u64 cl_e; /* eligible time */
  139. u64 cl_vt; /* virtual time */
  140. u64 cl_f; /* time when this class will fit for
  141. link-sharing, max(myf, cfmin) */
  142. u64 cl_myf; /* my fit-time (calculated from this
  143. class's own upperlimit curve) */
  144. u64 cl_myfadj; /* my fit-time adjustment (to cancel
  145. history dependence) */
  146. u64 cl_cfmin; /* earliest children's fit-time (used
  147. with cl_myf to obtain cl_f) */
  148. u64 cl_cvtmin; /* minimal virtual time among the
  149. children fit for link-sharing
  150. (monotonic within a period) */
  151. u64 cl_vtadj; /* intra-period cumulative vt
  152. adjustment */
  153. u64 cl_vtoff; /* inter-period cumulative vt offset */
  154. u64 cl_cvtmax; /* max child's vt in the last period */
  155. u64 cl_cvtoff; /* cumulative cvtmax of all periods */
  156. u64 cl_pcvtoff; /* parent's cvtoff at initalization
  157. time */
  158. struct internal_sc cl_rsc; /* internal real-time service curve */
  159. struct internal_sc cl_fsc; /* internal fair service curve */
  160. struct internal_sc cl_usc; /* internal upperlimit service curve */
  161. struct runtime_sc cl_deadline; /* deadline curve */
  162. struct runtime_sc cl_eligible; /* eligible curve */
  163. struct runtime_sc cl_virtual; /* virtual curve */
  164. struct runtime_sc cl_ulimit; /* upperlimit curve */
  165. unsigned long cl_flags; /* which curves are valid */
  166. unsigned long cl_vtperiod; /* vt period sequence number */
  167. unsigned long cl_parentperiod;/* parent's vt period sequence number*/
  168. unsigned long cl_nactive; /* number of active children */
  169. };
  170. #define HFSC_HSIZE 16
  171. struct hfsc_sched
  172. {
  173. u16 defcls; /* default class id */
  174. struct hfsc_class root; /* root class */
  175. struct list_head clhash[HFSC_HSIZE]; /* class hash */
  176. struct rb_root eligible; /* eligible tree */
  177. struct list_head droplist; /* active leaf class list (for
  178. dropping) */
  179. struct sk_buff_head requeue; /* requeued packet */
  180. struct qdisc_watchdog watchdog; /* watchdog timer */
  181. };
  182. #define HT_INFINITY 0xffffffffffffffffULL /* infinite time value */
  183. /*
  184. * eligible tree holds backlogged classes being sorted by their eligible times.
  185. * there is one eligible tree per hfsc instance.
  186. */
  187. static void
  188. eltree_insert(struct hfsc_class *cl)
  189. {
  190. struct rb_node **p = &cl->sched->eligible.rb_node;
  191. struct rb_node *parent = NULL;
  192. struct hfsc_class *cl1;
  193. while (*p != NULL) {
  194. parent = *p;
  195. cl1 = rb_entry(parent, struct hfsc_class, el_node);
  196. if (cl->cl_e >= cl1->cl_e)
  197. p = &parent->rb_right;
  198. else
  199. p = &parent->rb_left;
  200. }
  201. rb_link_node(&cl->el_node, parent, p);
  202. rb_insert_color(&cl->el_node, &cl->sched->eligible);
  203. }
  204. static inline void
  205. eltree_remove(struct hfsc_class *cl)
  206. {
  207. rb_erase(&cl->el_node, &cl->sched->eligible);
  208. }
  209. static inline void
  210. eltree_update(struct hfsc_class *cl)
  211. {
  212. eltree_remove(cl);
  213. eltree_insert(cl);
  214. }
  215. /* find the class with the minimum deadline among the eligible classes */
  216. static inline struct hfsc_class *
  217. eltree_get_mindl(struct hfsc_sched *q, u64 cur_time)
  218. {
  219. struct hfsc_class *p, *cl = NULL;
  220. struct rb_node *n;
  221. for (n = rb_first(&q->eligible); n != NULL; n = rb_next(n)) {
  222. p = rb_entry(n, struct hfsc_class, el_node);
  223. if (p->cl_e > cur_time)
  224. break;
  225. if (cl == NULL || p->cl_d < cl->cl_d)
  226. cl = p;
  227. }
  228. return cl;
  229. }
  230. /* find the class with minimum eligible time among the eligible classes */
  231. static inline struct hfsc_class *
  232. eltree_get_minel(struct hfsc_sched *q)
  233. {
  234. struct rb_node *n;
  235. n = rb_first(&q->eligible);
  236. if (n == NULL)
  237. return NULL;
  238. return rb_entry(n, struct hfsc_class, el_node);
  239. }
  240. /*
  241. * vttree holds holds backlogged child classes being sorted by their virtual
  242. * time. each intermediate class has one vttree.
  243. */
  244. static void
  245. vttree_insert(struct hfsc_class *cl)
  246. {
  247. struct rb_node **p = &cl->cl_parent->vt_tree.rb_node;
  248. struct rb_node *parent = NULL;
  249. struct hfsc_class *cl1;
  250. while (*p != NULL) {
  251. parent = *p;
  252. cl1 = rb_entry(parent, struct hfsc_class, vt_node);
  253. if (cl->cl_vt >= cl1->cl_vt)
  254. p = &parent->rb_right;
  255. else
  256. p = &parent->rb_left;
  257. }
  258. rb_link_node(&cl->vt_node, parent, p);
  259. rb_insert_color(&cl->vt_node, &cl->cl_parent->vt_tree);
  260. }
  261. static inline void
  262. vttree_remove(struct hfsc_class *cl)
  263. {
  264. rb_erase(&cl->vt_node, &cl->cl_parent->vt_tree);
  265. }
  266. static inline void
  267. vttree_update(struct hfsc_class *cl)
  268. {
  269. vttree_remove(cl);
  270. vttree_insert(cl);
  271. }
  272. static inline struct hfsc_class *
  273. vttree_firstfit(struct hfsc_class *cl, u64 cur_time)
  274. {
  275. struct hfsc_class *p;
  276. struct rb_node *n;
  277. for (n = rb_first(&cl->vt_tree); n != NULL; n = rb_next(n)) {
  278. p = rb_entry(n, struct hfsc_class, vt_node);
  279. if (p->cl_f <= cur_time)
  280. return p;
  281. }
  282. return NULL;
  283. }
  284. /*
  285. * get the leaf class with the minimum vt in the hierarchy
  286. */
  287. static struct hfsc_class *
  288. vttree_get_minvt(struct hfsc_class *cl, u64 cur_time)
  289. {
  290. /* if root-class's cfmin is bigger than cur_time nothing to do */
  291. if (cl->cl_cfmin > cur_time)
  292. return NULL;
  293. while (cl->level > 0) {
  294. cl = vttree_firstfit(cl, cur_time);
  295. if (cl == NULL)
  296. return NULL;
  297. /*
  298. * update parent's cl_cvtmin.
  299. */
  300. if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
  301. cl->cl_parent->cl_cvtmin = cl->cl_vt;
  302. }
  303. return cl;
  304. }
  305. static void
  306. cftree_insert(struct hfsc_class *cl)
  307. {
  308. struct rb_node **p = &cl->cl_parent->cf_tree.rb_node;
  309. struct rb_node *parent = NULL;
  310. struct hfsc_class *cl1;
  311. while (*p != NULL) {
  312. parent = *p;
  313. cl1 = rb_entry(parent, struct hfsc_class, cf_node);
  314. if (cl->cl_f >= cl1->cl_f)
  315. p = &parent->rb_right;
  316. else
  317. p = &parent->rb_left;
  318. }
  319. rb_link_node(&cl->cf_node, parent, p);
  320. rb_insert_color(&cl->cf_node, &cl->cl_parent->cf_tree);
  321. }
  322. static inline void
  323. cftree_remove(struct hfsc_class *cl)
  324. {
  325. rb_erase(&cl->cf_node, &cl->cl_parent->cf_tree);
  326. }
  327. static inline void
  328. cftree_update(struct hfsc_class *cl)
  329. {
  330. cftree_remove(cl);
  331. cftree_insert(cl);
  332. }
  333. /*
  334. * service curve support functions
  335. *
  336. * external service curve parameters
  337. * m: bps
  338. * d: us
  339. * internal service curve parameters
  340. * sm: (bytes/psched_us) << SM_SHIFT
  341. * ism: (psched_us/byte) << ISM_SHIFT
  342. * dx: psched_us
  343. *
  344. * The clock source resolution with ktime is 1.024us.
  345. *
  346. * sm and ism are scaled in order to keep effective digits.
  347. * SM_SHIFT and ISM_SHIFT are selected to keep at least 4 effective
  348. * digits in decimal using the following table.
  349. *
  350. * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps
  351. * ------------+-------------------------------------------------------
  352. * bytes/1.024us 12.8e-3 128e-3 1280e-3 12800e-3 128000e-3
  353. *
  354. * 1.024us/byte 78.125 7.8125 0.78125 0.078125 0.0078125
  355. */
  356. #define SM_SHIFT 20
  357. #define ISM_SHIFT 18
  358. #define SM_MASK ((1ULL << SM_SHIFT) - 1)
  359. #define ISM_MASK ((1ULL << ISM_SHIFT) - 1)
  360. static inline u64
  361. seg_x2y(u64 x, u64 sm)
  362. {
  363. u64 y;
  364. /*
  365. * compute
  366. * y = x * sm >> SM_SHIFT
  367. * but divide it for the upper and lower bits to avoid overflow
  368. */
  369. y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
  370. return y;
  371. }
  372. static inline u64
  373. seg_y2x(u64 y, u64 ism)
  374. {
  375. u64 x;
  376. if (y == 0)
  377. x = 0;
  378. else if (ism == HT_INFINITY)
  379. x = HT_INFINITY;
  380. else {
  381. x = (y >> ISM_SHIFT) * ism
  382. + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
  383. }
  384. return x;
  385. }
  386. /* Convert m (bps) into sm (bytes/psched us) */
  387. static u64
  388. m2sm(u32 m)
  389. {
  390. u64 sm;
  391. sm = ((u64)m << SM_SHIFT);
  392. sm += PSCHED_TICKS_PER_SEC - 1;
  393. do_div(sm, PSCHED_TICKS_PER_SEC);
  394. return sm;
  395. }
  396. /* convert m (bps) into ism (psched us/byte) */
  397. static u64
  398. m2ism(u32 m)
  399. {
  400. u64 ism;
  401. if (m == 0)
  402. ism = HT_INFINITY;
  403. else {
  404. ism = ((u64)PSCHED_TICKS_PER_SEC << ISM_SHIFT);
  405. ism += m - 1;
  406. do_div(ism, m);
  407. }
  408. return ism;
  409. }
  410. /* convert d (us) into dx (psched us) */
  411. static u64
  412. d2dx(u32 d)
  413. {
  414. u64 dx;
  415. dx = ((u64)d * PSCHED_TICKS_PER_SEC);
  416. dx += USEC_PER_SEC - 1;
  417. do_div(dx, USEC_PER_SEC);
  418. return dx;
  419. }
  420. /* convert sm (bytes/psched us) into m (bps) */
  421. static u32
  422. sm2m(u64 sm)
  423. {
  424. u64 m;
  425. m = (sm * PSCHED_TICKS_PER_SEC) >> SM_SHIFT;
  426. return (u32)m;
  427. }
  428. /* convert dx (psched us) into d (us) */
  429. static u32
  430. dx2d(u64 dx)
  431. {
  432. u64 d;
  433. d = dx * USEC_PER_SEC;
  434. do_div(d, PSCHED_TICKS_PER_SEC);
  435. return (u32)d;
  436. }
  437. static void
  438. sc2isc(struct tc_service_curve *sc, struct internal_sc *isc)
  439. {
  440. isc->sm1 = m2sm(sc->m1);
  441. isc->ism1 = m2ism(sc->m1);
  442. isc->dx = d2dx(sc->d);
  443. isc->dy = seg_x2y(isc->dx, isc->sm1);
  444. isc->sm2 = m2sm(sc->m2);
  445. isc->ism2 = m2ism(sc->m2);
  446. }
  447. /*
  448. * initialize the runtime service curve with the given internal
  449. * service curve starting at (x, y).
  450. */
  451. static void
  452. rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
  453. {
  454. rtsc->x = x;
  455. rtsc->y = y;
  456. rtsc->sm1 = isc->sm1;
  457. rtsc->ism1 = isc->ism1;
  458. rtsc->dx = isc->dx;
  459. rtsc->dy = isc->dy;
  460. rtsc->sm2 = isc->sm2;
  461. rtsc->ism2 = isc->ism2;
  462. }
  463. /*
  464. * calculate the y-projection of the runtime service curve by the
  465. * given x-projection value
  466. */
  467. static u64
  468. rtsc_y2x(struct runtime_sc *rtsc, u64 y)
  469. {
  470. u64 x;
  471. if (y < rtsc->y)
  472. x = rtsc->x;
  473. else if (y <= rtsc->y + rtsc->dy) {
  474. /* x belongs to the 1st segment */
  475. if (rtsc->dy == 0)
  476. x = rtsc->x + rtsc->dx;
  477. else
  478. x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
  479. } else {
  480. /* x belongs to the 2nd segment */
  481. x = rtsc->x + rtsc->dx
  482. + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
  483. }
  484. return x;
  485. }
  486. static u64
  487. rtsc_x2y(struct runtime_sc *rtsc, u64 x)
  488. {
  489. u64 y;
  490. if (x <= rtsc->x)
  491. y = rtsc->y;
  492. else if (x <= rtsc->x + rtsc->dx)
  493. /* y belongs to the 1st segment */
  494. y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
  495. else
  496. /* y belongs to the 2nd segment */
  497. y = rtsc->y + rtsc->dy
  498. + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
  499. return y;
  500. }
  501. /*
  502. * update the runtime service curve by taking the minimum of the current
  503. * runtime service curve and the service curve starting at (x, y).
  504. */
  505. static void
  506. rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y)
  507. {
  508. u64 y1, y2, dx, dy;
  509. u32 dsm;
  510. if (isc->sm1 <= isc->sm2) {
  511. /* service curve is convex */
  512. y1 = rtsc_x2y(rtsc, x);
  513. if (y1 < y)
  514. /* the current rtsc is smaller */
  515. return;
  516. rtsc->x = x;
  517. rtsc->y = y;
  518. return;
  519. }
  520. /*
  521. * service curve is concave
  522. * compute the two y values of the current rtsc
  523. * y1: at x
  524. * y2: at (x + dx)
  525. */
  526. y1 = rtsc_x2y(rtsc, x);
  527. if (y1 <= y) {
  528. /* rtsc is below isc, no change to rtsc */
  529. return;
  530. }
  531. y2 = rtsc_x2y(rtsc, x + isc->dx);
  532. if (y2 >= y + isc->dy) {
  533. /* rtsc is above isc, replace rtsc by isc */
  534. rtsc->x = x;
  535. rtsc->y = y;
  536. rtsc->dx = isc->dx;
  537. rtsc->dy = isc->dy;
  538. return;
  539. }
  540. /*
  541. * the two curves intersect
  542. * compute the offsets (dx, dy) using the reverse
  543. * function of seg_x2y()
  544. * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
  545. */
  546. dx = (y1 - y) << SM_SHIFT;
  547. dsm = isc->sm1 - isc->sm2;
  548. do_div(dx, dsm);
  549. /*
  550. * check if (x, y1) belongs to the 1st segment of rtsc.
  551. * if so, add the offset.
  552. */
  553. if (rtsc->x + rtsc->dx > x)
  554. dx += rtsc->x + rtsc->dx - x;
  555. dy = seg_x2y(dx, isc->sm1);
  556. rtsc->x = x;
  557. rtsc->y = y;
  558. rtsc->dx = dx;
  559. rtsc->dy = dy;
  560. return;
  561. }
  562. static void
  563. init_ed(struct hfsc_class *cl, unsigned int next_len)
  564. {
  565. u64 cur_time = psched_get_time();
  566. /* update the deadline curve */
  567. rtsc_min(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
  568. /*
  569. * update the eligible curve.
  570. * for concave, it is equal to the deadline curve.
  571. * for convex, it is a linear curve with slope m2.
  572. */
  573. cl->cl_eligible = cl->cl_deadline;
  574. if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
  575. cl->cl_eligible.dx = 0;
  576. cl->cl_eligible.dy = 0;
  577. }
  578. /* compute e and d */
  579. cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
  580. cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
  581. eltree_insert(cl);
  582. }
  583. static void
  584. update_ed(struct hfsc_class *cl, unsigned int next_len)
  585. {
  586. cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
  587. cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
  588. eltree_update(cl);
  589. }
  590. static inline void
  591. update_d(struct hfsc_class *cl, unsigned int next_len)
  592. {
  593. cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
  594. }
  595. static inline void
  596. update_cfmin(struct hfsc_class *cl)
  597. {
  598. struct rb_node *n = rb_first(&cl->cf_tree);
  599. struct hfsc_class *p;
  600. if (n == NULL) {
  601. cl->cl_cfmin = 0;
  602. return;
  603. }
  604. p = rb_entry(n, struct hfsc_class, cf_node);
  605. cl->cl_cfmin = p->cl_f;
  606. }
  607. static void
  608. init_vf(struct hfsc_class *cl, unsigned int len)
  609. {
  610. struct hfsc_class *max_cl;
  611. struct rb_node *n;
  612. u64 vt, f, cur_time;
  613. int go_active;
  614. cur_time = 0;
  615. go_active = 1;
  616. for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
  617. if (go_active && cl->cl_nactive++ == 0)
  618. go_active = 1;
  619. else
  620. go_active = 0;
  621. if (go_active) {
  622. n = rb_last(&cl->cl_parent->vt_tree);
  623. if (n != NULL) {
  624. max_cl = rb_entry(n, struct hfsc_class,vt_node);
  625. /*
  626. * set vt to the average of the min and max
  627. * classes. if the parent's period didn't
  628. * change, don't decrease vt of the class.
  629. */
  630. vt = max_cl->cl_vt;
  631. if (cl->cl_parent->cl_cvtmin != 0)
  632. vt = (cl->cl_parent->cl_cvtmin + vt)/2;
  633. if (cl->cl_parent->cl_vtperiod !=
  634. cl->cl_parentperiod || vt > cl->cl_vt)
  635. cl->cl_vt = vt;
  636. } else {
  637. /*
  638. * first child for a new parent backlog period.
  639. * add parent's cvtmax to cvtoff to make a new
  640. * vt (vtoff + vt) larger than the vt in the
  641. * last period for all children.
  642. */
  643. vt = cl->cl_parent->cl_cvtmax;
  644. cl->cl_parent->cl_cvtoff += vt;
  645. cl->cl_parent->cl_cvtmax = 0;
  646. cl->cl_parent->cl_cvtmin = 0;
  647. cl->cl_vt = 0;
  648. }
  649. cl->cl_vtoff = cl->cl_parent->cl_cvtoff -
  650. cl->cl_pcvtoff;
  651. /* update the virtual curve */
  652. vt = cl->cl_vt + cl->cl_vtoff;
  653. rtsc_min(&cl->cl_virtual, &cl->cl_fsc, vt,
  654. cl->cl_total);
  655. if (cl->cl_virtual.x == vt) {
  656. cl->cl_virtual.x -= cl->cl_vtoff;
  657. cl->cl_vtoff = 0;
  658. }
  659. cl->cl_vtadj = 0;
  660. cl->cl_vtperiod++; /* increment vt period */
  661. cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
  662. if (cl->cl_parent->cl_nactive == 0)
  663. cl->cl_parentperiod++;
  664. cl->cl_f = 0;
  665. vttree_insert(cl);
  666. cftree_insert(cl);
  667. if (cl->cl_flags & HFSC_USC) {
  668. /* class has upper limit curve */
  669. if (cur_time == 0)
  670. cur_time = psched_get_time();
  671. /* update the ulimit curve */
  672. rtsc_min(&cl->cl_ulimit, &cl->cl_usc, cur_time,
  673. cl->cl_total);
  674. /* compute myf */
  675. cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
  676. cl->cl_total);
  677. cl->cl_myfadj = 0;
  678. }
  679. }
  680. f = max(cl->cl_myf, cl->cl_cfmin);
  681. if (f != cl->cl_f) {
  682. cl->cl_f = f;
  683. cftree_update(cl);
  684. update_cfmin(cl->cl_parent);
  685. }
  686. }
  687. }
  688. static void
  689. update_vf(struct hfsc_class *cl, unsigned int len, u64 cur_time)
  690. {
  691. u64 f; /* , myf_bound, delta; */
  692. int go_passive = 0;
  693. if (cl->qdisc->q.qlen == 0 && cl->cl_flags & HFSC_FSC)
  694. go_passive = 1;
  695. for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
  696. cl->cl_total += len;
  697. if (!(cl->cl_flags & HFSC_FSC) || cl->cl_nactive == 0)
  698. continue;
  699. if (go_passive && --cl->cl_nactive == 0)
  700. go_passive = 1;
  701. else
  702. go_passive = 0;
  703. if (go_passive) {
  704. /* no more active child, going passive */
  705. /* update cvtmax of the parent class */
  706. if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
  707. cl->cl_parent->cl_cvtmax = cl->cl_vt;
  708. /* remove this class from the vt tree */
  709. vttree_remove(cl);
  710. cftree_remove(cl);
  711. update_cfmin(cl->cl_parent);
  712. continue;
  713. }
  714. /*
  715. * update vt and f
  716. */
  717. cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
  718. - cl->cl_vtoff + cl->cl_vtadj;
  719. /*
  720. * if vt of the class is smaller than cvtmin,
  721. * the class was skipped in the past due to non-fit.
  722. * if so, we need to adjust vtadj.
  723. */
  724. if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
  725. cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
  726. cl->cl_vt = cl->cl_parent->cl_cvtmin;
  727. }
  728. /* update the vt tree */
  729. vttree_update(cl);
  730. if (cl->cl_flags & HFSC_USC) {
  731. cl->cl_myf = cl->cl_myfadj + rtsc_y2x(&cl->cl_ulimit,
  732. cl->cl_total);
  733. #if 0
  734. /*
  735. * This code causes classes to stay way under their
  736. * limit when multiple classes are used at gigabit
  737. * speed. needs investigation. -kaber
  738. */
  739. /*
  740. * if myf lags behind by more than one clock tick
  741. * from the current time, adjust myfadj to prevent
  742. * a rate-limited class from going greedy.
  743. * in a steady state under rate-limiting, myf
  744. * fluctuates within one clock tick.
  745. */
  746. myf_bound = cur_time - PSCHED_JIFFIE2US(1);
  747. if (cl->cl_myf < myf_bound) {
  748. delta = cur_time - cl->cl_myf;
  749. cl->cl_myfadj += delta;
  750. cl->cl_myf += delta;
  751. }
  752. #endif
  753. }
  754. f = max(cl->cl_myf, cl->cl_cfmin);
  755. if (f != cl->cl_f) {
  756. cl->cl_f = f;
  757. cftree_update(cl);
  758. update_cfmin(cl->cl_parent);
  759. }
  760. }
  761. }
  762. static void
  763. set_active(struct hfsc_class *cl, unsigned int len)
  764. {
  765. if (cl->cl_flags & HFSC_RSC)
  766. init_ed(cl, len);
  767. if (cl->cl_flags & HFSC_FSC)
  768. init_vf(cl, len);
  769. list_add_tail(&cl->dlist, &cl->sched->droplist);
  770. }
  771. static void
  772. set_passive(struct hfsc_class *cl)
  773. {
  774. if (cl->cl_flags & HFSC_RSC)
  775. eltree_remove(cl);
  776. list_del(&cl->dlist);
  777. /*
  778. * vttree is now handled in update_vf() so that update_vf(cl, 0, 0)
  779. * needs to be called explicitly to remove a class from vttree.
  780. */
  781. }
  782. /*
  783. * hack to get length of first packet in queue.
  784. */
  785. static unsigned int
  786. qdisc_peek_len(struct Qdisc *sch)
  787. {
  788. struct sk_buff *skb;
  789. unsigned int len;
  790. skb = sch->dequeue(sch);
  791. if (skb == NULL) {
  792. if (net_ratelimit())
  793. printk("qdisc_peek_len: non work-conserving qdisc ?\n");
  794. return 0;
  795. }
  796. len = skb->len;
  797. if (unlikely(sch->ops->requeue(skb, sch) != NET_XMIT_SUCCESS)) {
  798. if (net_ratelimit())
  799. printk("qdisc_peek_len: failed to requeue\n");
  800. qdisc_tree_decrease_qlen(sch, 1);
  801. return 0;
  802. }
  803. return len;
  804. }
  805. static void
  806. hfsc_purge_queue(struct Qdisc *sch, struct hfsc_class *cl)
  807. {
  808. unsigned int len = cl->qdisc->q.qlen;
  809. qdisc_reset(cl->qdisc);
  810. qdisc_tree_decrease_qlen(cl->qdisc, len);
  811. }
  812. static void
  813. hfsc_adjust_levels(struct hfsc_class *cl)
  814. {
  815. struct hfsc_class *p;
  816. unsigned int level;
  817. do {
  818. level = 0;
  819. list_for_each_entry(p, &cl->children, siblings) {
  820. if (p->level >= level)
  821. level = p->level + 1;
  822. }
  823. cl->level = level;
  824. } while ((cl = cl->cl_parent) != NULL);
  825. }
  826. static inline unsigned int
  827. hfsc_hash(u32 h)
  828. {
  829. h ^= h >> 8;
  830. h ^= h >> 4;
  831. return h & (HFSC_HSIZE - 1);
  832. }
  833. static inline struct hfsc_class *
  834. hfsc_find_class(u32 classid, struct Qdisc *sch)
  835. {
  836. struct hfsc_sched *q = qdisc_priv(sch);
  837. struct hfsc_class *cl;
  838. list_for_each_entry(cl, &q->clhash[hfsc_hash(classid)], hlist) {
  839. if (cl->classid == classid)
  840. return cl;
  841. }
  842. return NULL;
  843. }
  844. static void
  845. hfsc_change_rsc(struct hfsc_class *cl, struct tc_service_curve *rsc,
  846. u64 cur_time)
  847. {
  848. sc2isc(rsc, &cl->cl_rsc);
  849. rtsc_init(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul);
  850. cl->cl_eligible = cl->cl_deadline;
  851. if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) {
  852. cl->cl_eligible.dx = 0;
  853. cl->cl_eligible.dy = 0;
  854. }
  855. cl->cl_flags |= HFSC_RSC;
  856. }
  857. static void
  858. hfsc_change_fsc(struct hfsc_class *cl, struct tc_service_curve *fsc)
  859. {
  860. sc2isc(fsc, &cl->cl_fsc);
  861. rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total);
  862. cl->cl_flags |= HFSC_FSC;
  863. }
  864. static void
  865. hfsc_change_usc(struct hfsc_class *cl, struct tc_service_curve *usc,
  866. u64 cur_time)
  867. {
  868. sc2isc(usc, &cl->cl_usc);
  869. rtsc_init(&cl->cl_ulimit, &cl->cl_usc, cur_time, cl->cl_total);
  870. cl->cl_flags |= HFSC_USC;
  871. }
  872. static int
  873. hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
  874. struct rtattr **tca, unsigned long *arg)
  875. {
  876. struct hfsc_sched *q = qdisc_priv(sch);
  877. struct hfsc_class *cl = (struct hfsc_class *)*arg;
  878. struct hfsc_class *parent = NULL;
  879. struct rtattr *opt = tca[TCA_OPTIONS-1];
  880. struct rtattr *tb[TCA_HFSC_MAX];
  881. struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL;
  882. u64 cur_time;
  883. if (opt == NULL || rtattr_parse_nested(tb, TCA_HFSC_MAX, opt))
  884. return -EINVAL;
  885. if (tb[TCA_HFSC_RSC-1]) {
  886. if (RTA_PAYLOAD(tb[TCA_HFSC_RSC-1]) < sizeof(*rsc))
  887. return -EINVAL;
  888. rsc = RTA_DATA(tb[TCA_HFSC_RSC-1]);
  889. if (rsc->m1 == 0 && rsc->m2 == 0)
  890. rsc = NULL;
  891. }
  892. if (tb[TCA_HFSC_FSC-1]) {
  893. if (RTA_PAYLOAD(tb[TCA_HFSC_FSC-1]) < sizeof(*fsc))
  894. return -EINVAL;
  895. fsc = RTA_DATA(tb[TCA_HFSC_FSC-1]);
  896. if (fsc->m1 == 0 && fsc->m2 == 0)
  897. fsc = NULL;
  898. }
  899. if (tb[TCA_HFSC_USC-1]) {
  900. if (RTA_PAYLOAD(tb[TCA_HFSC_USC-1]) < sizeof(*usc))
  901. return -EINVAL;
  902. usc = RTA_DATA(tb[TCA_HFSC_USC-1]);
  903. if (usc->m1 == 0 && usc->m2 == 0)
  904. usc = NULL;
  905. }
  906. if (cl != NULL) {
  907. if (parentid) {
  908. if (cl->cl_parent && cl->cl_parent->classid != parentid)
  909. return -EINVAL;
  910. if (cl->cl_parent == NULL && parentid != TC_H_ROOT)
  911. return -EINVAL;
  912. }
  913. cur_time = psched_get_time();
  914. sch_tree_lock(sch);
  915. if (rsc != NULL)
  916. hfsc_change_rsc(cl, rsc, cur_time);
  917. if (fsc != NULL)
  918. hfsc_change_fsc(cl, fsc);
  919. if (usc != NULL)
  920. hfsc_change_usc(cl, usc, cur_time);
  921. if (cl->qdisc->q.qlen != 0) {
  922. if (cl->cl_flags & HFSC_RSC)
  923. update_ed(cl, qdisc_peek_len(cl->qdisc));
  924. if (cl->cl_flags & HFSC_FSC)
  925. update_vf(cl, 0, cur_time);
  926. }
  927. sch_tree_unlock(sch);
  928. #ifdef CONFIG_NET_ESTIMATOR
  929. if (tca[TCA_RATE-1])
  930. gen_replace_estimator(&cl->bstats, &cl->rate_est,
  931. cl->stats_lock, tca[TCA_RATE-1]);
  932. #endif
  933. return 0;
  934. }
  935. if (parentid == TC_H_ROOT)
  936. return -EEXIST;
  937. parent = &q->root;
  938. if (parentid) {
  939. parent = hfsc_find_class(parentid, sch);
  940. if (parent == NULL)
  941. return -ENOENT;
  942. }
  943. if (classid == 0 || TC_H_MAJ(classid ^ sch->handle) != 0)
  944. return -EINVAL;
  945. if (hfsc_find_class(classid, sch))
  946. return -EEXIST;
  947. if (rsc == NULL && fsc == NULL)
  948. return -EINVAL;
  949. cl = kzalloc(sizeof(struct hfsc_class), GFP_KERNEL);
  950. if (cl == NULL)
  951. return -ENOBUFS;
  952. if (rsc != NULL)
  953. hfsc_change_rsc(cl, rsc, 0);
  954. if (fsc != NULL)
  955. hfsc_change_fsc(cl, fsc);
  956. if (usc != NULL)
  957. hfsc_change_usc(cl, usc, 0);
  958. cl->refcnt = 1;
  959. cl->classid = classid;
  960. cl->sched = q;
  961. cl->cl_parent = parent;
  962. cl->qdisc = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, classid);
  963. if (cl->qdisc == NULL)
  964. cl->qdisc = &noop_qdisc;
  965. cl->stats_lock = &sch->dev->queue_lock;
  966. INIT_LIST_HEAD(&cl->children);
  967. cl->vt_tree = RB_ROOT;
  968. cl->cf_tree = RB_ROOT;
  969. sch_tree_lock(sch);
  970. list_add_tail(&cl->hlist, &q->clhash[hfsc_hash(classid)]);
  971. list_add_tail(&cl->siblings, &parent->children);
  972. if (parent->level == 0)
  973. hfsc_purge_queue(sch, parent);
  974. hfsc_adjust_levels(parent);
  975. cl->cl_pcvtoff = parent->cl_cvtoff;
  976. sch_tree_unlock(sch);
  977. #ifdef CONFIG_NET_ESTIMATOR
  978. if (tca[TCA_RATE-1])
  979. gen_new_estimator(&cl->bstats, &cl->rate_est,
  980. cl->stats_lock, tca[TCA_RATE-1]);
  981. #endif
  982. *arg = (unsigned long)cl;
  983. return 0;
  984. }
  985. static void
  986. hfsc_destroy_filters(struct tcf_proto **fl)
  987. {
  988. struct tcf_proto *tp;
  989. while ((tp = *fl) != NULL) {
  990. *fl = tp->next;
  991. tcf_destroy(tp);
  992. }
  993. }
  994. static void
  995. hfsc_destroy_class(struct Qdisc *sch, struct hfsc_class *cl)
  996. {
  997. struct hfsc_sched *q = qdisc_priv(sch);
  998. hfsc_destroy_filters(&cl->filter_list);
  999. qdisc_destroy(cl->qdisc);
  1000. #ifdef CONFIG_NET_ESTIMATOR
  1001. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  1002. #endif
  1003. if (cl != &q->root)
  1004. kfree(cl);
  1005. }
  1006. static int
  1007. hfsc_delete_class(struct Qdisc *sch, unsigned long arg)
  1008. {
  1009. struct hfsc_sched *q = qdisc_priv(sch);
  1010. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1011. if (cl->level > 0 || cl->filter_cnt > 0 || cl == &q->root)
  1012. return -EBUSY;
  1013. sch_tree_lock(sch);
  1014. list_del(&cl->siblings);
  1015. hfsc_adjust_levels(cl->cl_parent);
  1016. hfsc_purge_queue(sch, cl);
  1017. list_del(&cl->hlist);
  1018. if (--cl->refcnt == 0)
  1019. hfsc_destroy_class(sch, cl);
  1020. sch_tree_unlock(sch);
  1021. return 0;
  1022. }
  1023. static struct hfsc_class *
  1024. hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  1025. {
  1026. struct hfsc_sched *q = qdisc_priv(sch);
  1027. struct hfsc_class *cl;
  1028. struct tcf_result res;
  1029. struct tcf_proto *tcf;
  1030. int result;
  1031. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0 &&
  1032. (cl = hfsc_find_class(skb->priority, sch)) != NULL)
  1033. if (cl->level == 0)
  1034. return cl;
  1035. *qerr = NET_XMIT_BYPASS;
  1036. tcf = q->root.filter_list;
  1037. while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
  1038. #ifdef CONFIG_NET_CLS_ACT
  1039. switch (result) {
  1040. case TC_ACT_QUEUED:
  1041. case TC_ACT_STOLEN:
  1042. *qerr = NET_XMIT_SUCCESS;
  1043. case TC_ACT_SHOT:
  1044. return NULL;
  1045. }
  1046. #elif defined(CONFIG_NET_CLS_POLICE)
  1047. if (result == TC_POLICE_SHOT)
  1048. return NULL;
  1049. #endif
  1050. if ((cl = (struct hfsc_class *)res.class) == NULL) {
  1051. if ((cl = hfsc_find_class(res.classid, sch)) == NULL)
  1052. break; /* filter selected invalid classid */
  1053. }
  1054. if (cl->level == 0)
  1055. return cl; /* hit leaf class */
  1056. /* apply inner filter chain */
  1057. tcf = cl->filter_list;
  1058. }
  1059. /* classification failed, try default class */
  1060. cl = hfsc_find_class(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
  1061. if (cl == NULL || cl->level > 0)
  1062. return NULL;
  1063. return cl;
  1064. }
  1065. static int
  1066. hfsc_graft_class(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  1067. struct Qdisc **old)
  1068. {
  1069. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1070. if (cl == NULL)
  1071. return -ENOENT;
  1072. if (cl->level > 0)
  1073. return -EINVAL;
  1074. if (new == NULL) {
  1075. new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  1076. cl->classid);
  1077. if (new == NULL)
  1078. new = &noop_qdisc;
  1079. }
  1080. sch_tree_lock(sch);
  1081. hfsc_purge_queue(sch, cl);
  1082. *old = xchg(&cl->qdisc, new);
  1083. sch_tree_unlock(sch);
  1084. return 0;
  1085. }
  1086. static struct Qdisc *
  1087. hfsc_class_leaf(struct Qdisc *sch, unsigned long arg)
  1088. {
  1089. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1090. if (cl != NULL && cl->level == 0)
  1091. return cl->qdisc;
  1092. return NULL;
  1093. }
  1094. static void
  1095. hfsc_qlen_notify(struct Qdisc *sch, unsigned long arg)
  1096. {
  1097. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1098. if (cl->qdisc->q.qlen == 0) {
  1099. update_vf(cl, 0, 0);
  1100. set_passive(cl);
  1101. }
  1102. }
  1103. static unsigned long
  1104. hfsc_get_class(struct Qdisc *sch, u32 classid)
  1105. {
  1106. struct hfsc_class *cl = hfsc_find_class(classid, sch);
  1107. if (cl != NULL)
  1108. cl->refcnt++;
  1109. return (unsigned long)cl;
  1110. }
  1111. static void
  1112. hfsc_put_class(struct Qdisc *sch, unsigned long arg)
  1113. {
  1114. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1115. if (--cl->refcnt == 0)
  1116. hfsc_destroy_class(sch, cl);
  1117. }
  1118. static unsigned long
  1119. hfsc_bind_tcf(struct Qdisc *sch, unsigned long parent, u32 classid)
  1120. {
  1121. struct hfsc_class *p = (struct hfsc_class *)parent;
  1122. struct hfsc_class *cl = hfsc_find_class(classid, sch);
  1123. if (cl != NULL) {
  1124. if (p != NULL && p->level <= cl->level)
  1125. return 0;
  1126. cl->filter_cnt++;
  1127. }
  1128. return (unsigned long)cl;
  1129. }
  1130. static void
  1131. hfsc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  1132. {
  1133. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1134. cl->filter_cnt--;
  1135. }
  1136. static struct tcf_proto **
  1137. hfsc_tcf_chain(struct Qdisc *sch, unsigned long arg)
  1138. {
  1139. struct hfsc_sched *q = qdisc_priv(sch);
  1140. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1141. if (cl == NULL)
  1142. cl = &q->root;
  1143. return &cl->filter_list;
  1144. }
  1145. static int
  1146. hfsc_dump_sc(struct sk_buff *skb, int attr, struct internal_sc *sc)
  1147. {
  1148. struct tc_service_curve tsc;
  1149. tsc.m1 = sm2m(sc->sm1);
  1150. tsc.d = dx2d(sc->dx);
  1151. tsc.m2 = sm2m(sc->sm2);
  1152. RTA_PUT(skb, attr, sizeof(tsc), &tsc);
  1153. return skb->len;
  1154. rtattr_failure:
  1155. return -1;
  1156. }
  1157. static inline int
  1158. hfsc_dump_curves(struct sk_buff *skb, struct hfsc_class *cl)
  1159. {
  1160. if ((cl->cl_flags & HFSC_RSC) &&
  1161. (hfsc_dump_sc(skb, TCA_HFSC_RSC, &cl->cl_rsc) < 0))
  1162. goto rtattr_failure;
  1163. if ((cl->cl_flags & HFSC_FSC) &&
  1164. (hfsc_dump_sc(skb, TCA_HFSC_FSC, &cl->cl_fsc) < 0))
  1165. goto rtattr_failure;
  1166. if ((cl->cl_flags & HFSC_USC) &&
  1167. (hfsc_dump_sc(skb, TCA_HFSC_USC, &cl->cl_usc) < 0))
  1168. goto rtattr_failure;
  1169. return skb->len;
  1170. rtattr_failure:
  1171. return -1;
  1172. }
  1173. static int
  1174. hfsc_dump_class(struct Qdisc *sch, unsigned long arg, struct sk_buff *skb,
  1175. struct tcmsg *tcm)
  1176. {
  1177. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1178. unsigned char *b = skb_tail_pointer(skb);
  1179. struct rtattr *rta = (struct rtattr *)b;
  1180. tcm->tcm_parent = cl->cl_parent ? cl->cl_parent->classid : TC_H_ROOT;
  1181. tcm->tcm_handle = cl->classid;
  1182. if (cl->level == 0)
  1183. tcm->tcm_info = cl->qdisc->handle;
  1184. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  1185. if (hfsc_dump_curves(skb, cl) < 0)
  1186. goto rtattr_failure;
  1187. rta->rta_len = skb_tail_pointer(skb) - b;
  1188. return skb->len;
  1189. rtattr_failure:
  1190. nlmsg_trim(skb, b);
  1191. return -1;
  1192. }
  1193. static int
  1194. hfsc_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  1195. struct gnet_dump *d)
  1196. {
  1197. struct hfsc_class *cl = (struct hfsc_class *)arg;
  1198. struct tc_hfsc_stats xstats;
  1199. cl->qstats.qlen = cl->qdisc->q.qlen;
  1200. xstats.level = cl->level;
  1201. xstats.period = cl->cl_vtperiod;
  1202. xstats.work = cl->cl_total;
  1203. xstats.rtwork = cl->cl_cumul;
  1204. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  1205. #ifdef CONFIG_NET_ESTIMATOR
  1206. gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
  1207. #endif
  1208. gnet_stats_copy_queue(d, &cl->qstats) < 0)
  1209. return -1;
  1210. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  1211. }
  1212. static void
  1213. hfsc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  1214. {
  1215. struct hfsc_sched *q = qdisc_priv(sch);
  1216. struct hfsc_class *cl;
  1217. unsigned int i;
  1218. if (arg->stop)
  1219. return;
  1220. for (i = 0; i < HFSC_HSIZE; i++) {
  1221. list_for_each_entry(cl, &q->clhash[i], hlist) {
  1222. if (arg->count < arg->skip) {
  1223. arg->count++;
  1224. continue;
  1225. }
  1226. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  1227. arg->stop = 1;
  1228. return;
  1229. }
  1230. arg->count++;
  1231. }
  1232. }
  1233. }
  1234. static void
  1235. hfsc_schedule_watchdog(struct Qdisc *sch)
  1236. {
  1237. struct hfsc_sched *q = qdisc_priv(sch);
  1238. struct hfsc_class *cl;
  1239. u64 next_time = 0;
  1240. if ((cl = eltree_get_minel(q)) != NULL)
  1241. next_time = cl->cl_e;
  1242. if (q->root.cl_cfmin != 0) {
  1243. if (next_time == 0 || next_time > q->root.cl_cfmin)
  1244. next_time = q->root.cl_cfmin;
  1245. }
  1246. WARN_ON(next_time == 0);
  1247. qdisc_watchdog_schedule(&q->watchdog, next_time);
  1248. }
  1249. static int
  1250. hfsc_init_qdisc(struct Qdisc *sch, struct rtattr *opt)
  1251. {
  1252. struct hfsc_sched *q = qdisc_priv(sch);
  1253. struct tc_hfsc_qopt *qopt;
  1254. unsigned int i;
  1255. if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
  1256. return -EINVAL;
  1257. qopt = RTA_DATA(opt);
  1258. sch->stats_lock = &sch->dev->queue_lock;
  1259. q->defcls = qopt->defcls;
  1260. for (i = 0; i < HFSC_HSIZE; i++)
  1261. INIT_LIST_HEAD(&q->clhash[i]);
  1262. q->eligible = RB_ROOT;
  1263. INIT_LIST_HEAD(&q->droplist);
  1264. skb_queue_head_init(&q->requeue);
  1265. q->root.refcnt = 1;
  1266. q->root.classid = sch->handle;
  1267. q->root.sched = q;
  1268. q->root.qdisc = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  1269. sch->handle);
  1270. if (q->root.qdisc == NULL)
  1271. q->root.qdisc = &noop_qdisc;
  1272. q->root.stats_lock = &sch->dev->queue_lock;
  1273. INIT_LIST_HEAD(&q->root.children);
  1274. q->root.vt_tree = RB_ROOT;
  1275. q->root.cf_tree = RB_ROOT;
  1276. list_add(&q->root.hlist, &q->clhash[hfsc_hash(q->root.classid)]);
  1277. qdisc_watchdog_init(&q->watchdog, sch);
  1278. return 0;
  1279. }
  1280. static int
  1281. hfsc_change_qdisc(struct Qdisc *sch, struct rtattr *opt)
  1282. {
  1283. struct hfsc_sched *q = qdisc_priv(sch);
  1284. struct tc_hfsc_qopt *qopt;
  1285. if (opt == NULL || RTA_PAYLOAD(opt) < sizeof(*qopt))
  1286. return -EINVAL;
  1287. qopt = RTA_DATA(opt);
  1288. sch_tree_lock(sch);
  1289. q->defcls = qopt->defcls;
  1290. sch_tree_unlock(sch);
  1291. return 0;
  1292. }
  1293. static void
  1294. hfsc_reset_class(struct hfsc_class *cl)
  1295. {
  1296. cl->cl_total = 0;
  1297. cl->cl_cumul = 0;
  1298. cl->cl_d = 0;
  1299. cl->cl_e = 0;
  1300. cl->cl_vt = 0;
  1301. cl->cl_vtadj = 0;
  1302. cl->cl_vtoff = 0;
  1303. cl->cl_cvtmin = 0;
  1304. cl->cl_cvtmax = 0;
  1305. cl->cl_cvtoff = 0;
  1306. cl->cl_pcvtoff = 0;
  1307. cl->cl_vtperiod = 0;
  1308. cl->cl_parentperiod = 0;
  1309. cl->cl_f = 0;
  1310. cl->cl_myf = 0;
  1311. cl->cl_myfadj = 0;
  1312. cl->cl_cfmin = 0;
  1313. cl->cl_nactive = 0;
  1314. cl->vt_tree = RB_ROOT;
  1315. cl->cf_tree = RB_ROOT;
  1316. qdisc_reset(cl->qdisc);
  1317. if (cl->cl_flags & HFSC_RSC)
  1318. rtsc_init(&cl->cl_deadline, &cl->cl_rsc, 0, 0);
  1319. if (cl->cl_flags & HFSC_FSC)
  1320. rtsc_init(&cl->cl_virtual, &cl->cl_fsc, 0, 0);
  1321. if (cl->cl_flags & HFSC_USC)
  1322. rtsc_init(&cl->cl_ulimit, &cl->cl_usc, 0, 0);
  1323. }
  1324. static void
  1325. hfsc_reset_qdisc(struct Qdisc *sch)
  1326. {
  1327. struct hfsc_sched *q = qdisc_priv(sch);
  1328. struct hfsc_class *cl;
  1329. unsigned int i;
  1330. for (i = 0; i < HFSC_HSIZE; i++) {
  1331. list_for_each_entry(cl, &q->clhash[i], hlist)
  1332. hfsc_reset_class(cl);
  1333. }
  1334. __skb_queue_purge(&q->requeue);
  1335. q->eligible = RB_ROOT;
  1336. INIT_LIST_HEAD(&q->droplist);
  1337. qdisc_watchdog_cancel(&q->watchdog);
  1338. sch->q.qlen = 0;
  1339. }
  1340. static void
  1341. hfsc_destroy_qdisc(struct Qdisc *sch)
  1342. {
  1343. struct hfsc_sched *q = qdisc_priv(sch);
  1344. struct hfsc_class *cl, *next;
  1345. unsigned int i;
  1346. for (i = 0; i < HFSC_HSIZE; i++) {
  1347. list_for_each_entry_safe(cl, next, &q->clhash[i], hlist)
  1348. hfsc_destroy_class(sch, cl);
  1349. }
  1350. __skb_queue_purge(&q->requeue);
  1351. qdisc_watchdog_cancel(&q->watchdog);
  1352. }
  1353. static int
  1354. hfsc_dump_qdisc(struct Qdisc *sch, struct sk_buff *skb)
  1355. {
  1356. struct hfsc_sched *q = qdisc_priv(sch);
  1357. unsigned char *b = skb_tail_pointer(skb);
  1358. struct tc_hfsc_qopt qopt;
  1359. qopt.defcls = q->defcls;
  1360. RTA_PUT(skb, TCA_OPTIONS, sizeof(qopt), &qopt);
  1361. return skb->len;
  1362. rtattr_failure:
  1363. nlmsg_trim(skb, b);
  1364. return -1;
  1365. }
  1366. static int
  1367. hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  1368. {
  1369. struct hfsc_class *cl;
  1370. unsigned int len;
  1371. int err;
  1372. cl = hfsc_classify(skb, sch, &err);
  1373. if (cl == NULL) {
  1374. if (err == NET_XMIT_BYPASS)
  1375. sch->qstats.drops++;
  1376. kfree_skb(skb);
  1377. return err;
  1378. }
  1379. len = skb->len;
  1380. err = cl->qdisc->enqueue(skb, cl->qdisc);
  1381. if (unlikely(err != NET_XMIT_SUCCESS)) {
  1382. cl->qstats.drops++;
  1383. sch->qstats.drops++;
  1384. return err;
  1385. }
  1386. if (cl->qdisc->q.qlen == 1)
  1387. set_active(cl, len);
  1388. cl->bstats.packets++;
  1389. cl->bstats.bytes += len;
  1390. sch->bstats.packets++;
  1391. sch->bstats.bytes += len;
  1392. sch->q.qlen++;
  1393. return NET_XMIT_SUCCESS;
  1394. }
  1395. static struct sk_buff *
  1396. hfsc_dequeue(struct Qdisc *sch)
  1397. {
  1398. struct hfsc_sched *q = qdisc_priv(sch);
  1399. struct hfsc_class *cl;
  1400. struct sk_buff *skb;
  1401. u64 cur_time;
  1402. unsigned int next_len;
  1403. int realtime = 0;
  1404. if (sch->q.qlen == 0)
  1405. return NULL;
  1406. if ((skb = __skb_dequeue(&q->requeue)))
  1407. goto out;
  1408. cur_time = psched_get_time();
  1409. /*
  1410. * if there are eligible classes, use real-time criteria.
  1411. * find the class with the minimum deadline among
  1412. * the eligible classes.
  1413. */
  1414. if ((cl = eltree_get_mindl(q, cur_time)) != NULL) {
  1415. realtime = 1;
  1416. } else {
  1417. /*
  1418. * use link-sharing criteria
  1419. * get the class with the minimum vt in the hierarchy
  1420. */
  1421. cl = vttree_get_minvt(&q->root, cur_time);
  1422. if (cl == NULL) {
  1423. sch->qstats.overlimits++;
  1424. hfsc_schedule_watchdog(sch);
  1425. return NULL;
  1426. }
  1427. }
  1428. skb = cl->qdisc->dequeue(cl->qdisc);
  1429. if (skb == NULL) {
  1430. if (net_ratelimit())
  1431. printk("HFSC: Non-work-conserving qdisc ?\n");
  1432. return NULL;
  1433. }
  1434. update_vf(cl, skb->len, cur_time);
  1435. if (realtime)
  1436. cl->cl_cumul += skb->len;
  1437. if (cl->qdisc->q.qlen != 0) {
  1438. if (cl->cl_flags & HFSC_RSC) {
  1439. /* update ed */
  1440. next_len = qdisc_peek_len(cl->qdisc);
  1441. if (realtime)
  1442. update_ed(cl, next_len);
  1443. else
  1444. update_d(cl, next_len);
  1445. }
  1446. } else {
  1447. /* the class becomes passive */
  1448. set_passive(cl);
  1449. }
  1450. out:
  1451. sch->flags &= ~TCQ_F_THROTTLED;
  1452. sch->q.qlen--;
  1453. return skb;
  1454. }
  1455. static int
  1456. hfsc_requeue(struct sk_buff *skb, struct Qdisc *sch)
  1457. {
  1458. struct hfsc_sched *q = qdisc_priv(sch);
  1459. __skb_queue_head(&q->requeue, skb);
  1460. sch->q.qlen++;
  1461. sch->qstats.requeues++;
  1462. return NET_XMIT_SUCCESS;
  1463. }
  1464. static unsigned int
  1465. hfsc_drop(struct Qdisc *sch)
  1466. {
  1467. struct hfsc_sched *q = qdisc_priv(sch);
  1468. struct hfsc_class *cl;
  1469. unsigned int len;
  1470. list_for_each_entry(cl, &q->droplist, dlist) {
  1471. if (cl->qdisc->ops->drop != NULL &&
  1472. (len = cl->qdisc->ops->drop(cl->qdisc)) > 0) {
  1473. if (cl->qdisc->q.qlen == 0) {
  1474. update_vf(cl, 0, 0);
  1475. set_passive(cl);
  1476. } else {
  1477. list_move_tail(&cl->dlist, &q->droplist);
  1478. }
  1479. cl->qstats.drops++;
  1480. sch->qstats.drops++;
  1481. sch->q.qlen--;
  1482. return len;
  1483. }
  1484. }
  1485. return 0;
  1486. }
  1487. static struct Qdisc_class_ops hfsc_class_ops = {
  1488. .change = hfsc_change_class,
  1489. .delete = hfsc_delete_class,
  1490. .graft = hfsc_graft_class,
  1491. .leaf = hfsc_class_leaf,
  1492. .qlen_notify = hfsc_qlen_notify,
  1493. .get = hfsc_get_class,
  1494. .put = hfsc_put_class,
  1495. .bind_tcf = hfsc_bind_tcf,
  1496. .unbind_tcf = hfsc_unbind_tcf,
  1497. .tcf_chain = hfsc_tcf_chain,
  1498. .dump = hfsc_dump_class,
  1499. .dump_stats = hfsc_dump_class_stats,
  1500. .walk = hfsc_walk
  1501. };
  1502. static struct Qdisc_ops hfsc_qdisc_ops = {
  1503. .id = "hfsc",
  1504. .init = hfsc_init_qdisc,
  1505. .change = hfsc_change_qdisc,
  1506. .reset = hfsc_reset_qdisc,
  1507. .destroy = hfsc_destroy_qdisc,
  1508. .dump = hfsc_dump_qdisc,
  1509. .enqueue = hfsc_enqueue,
  1510. .dequeue = hfsc_dequeue,
  1511. .requeue = hfsc_requeue,
  1512. .drop = hfsc_drop,
  1513. .cl_ops = &hfsc_class_ops,
  1514. .priv_size = sizeof(struct hfsc_sched),
  1515. .owner = THIS_MODULE
  1516. };
  1517. static int __init
  1518. hfsc_init(void)
  1519. {
  1520. return register_qdisc(&hfsc_qdisc_ops);
  1521. }
  1522. static void __exit
  1523. hfsc_cleanup(void)
  1524. {
  1525. unregister_qdisc(&hfsc_qdisc_ops);
  1526. }
  1527. MODULE_LICENSE("GPL");
  1528. module_init(hfsc_init);
  1529. module_exit(hfsc_cleanup);