sch_hfsc.c 41 KB

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