sch_hfsc.c 42 KB

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