sch_hfsc.c 40 KB

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