PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/net/sched/sch_hfsc.c

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