sch_hfsc.c 41 KB

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