sch_hfsc.c 40 KB

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