sch_hfsc.c 41 KB

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