sch_hfsc.c 42 KB

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