sch_hfsc.c 40 KB

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