sch_hfsc.c 40 KB

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