sch_hfsc.c 41 KB

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