sch_hfsc.c 41 KB

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