sch_htb.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. /*
  2. * net/sched/sch_htb.c Hierarchical token bucket, feed tree version
  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
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Martin Devera, <devik@cdi.cz>
  10. *
  11. * Credits (in time order) for older HTB versions:
  12. * Stef Coene <stef.coene@docum.org>
  13. * HTB support at LARTC mailing list
  14. * Ondrej Kraus, <krauso@barr.cz>
  15. * found missing INIT_QDISC(htb)
  16. * Vladimir Smelhaus, Aamer Akhter, Bert Hubert
  17. * helped a lot to locate nasty class stall bug
  18. * Andi Kleen, Jamal Hadi, Bert Hubert
  19. * code review and helpful comments on shaping
  20. * Tomasz Wrona, <tw@eter.tym.pl>
  21. * created test case so that I was able to fix nasty bug
  22. * Wilfried Weissmann
  23. * spotted bug in dequeue code and helped with fix
  24. * Jiri Fojtasek
  25. * fixed requeue routine
  26. * and many others. thanks.
  27. *
  28. * $Id: sch_htb.c,v 1.25 2003/12/07 11:08:25 devik Exp devik $
  29. */
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/kernel.h>
  33. #include <linux/string.h>
  34. #include <linux/errno.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/list.h>
  37. #include <linux/compiler.h>
  38. #include <linux/rbtree.h>
  39. #include <net/netlink.h>
  40. #include <net/pkt_sched.h>
  41. /* HTB algorithm.
  42. Author: devik@cdi.cz
  43. ========================================================================
  44. HTB is like TBF with multiple classes. It is also similar to CBQ because
  45. it allows to assign priority to each class in hierarchy.
  46. In fact it is another implementation of Floyd's formal sharing.
  47. Levels:
  48. Each class is assigned level. Leaf has ALWAYS level 0 and root
  49. classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
  50. one less than their parent.
  51. */
  52. #define HTB_HSIZE 16 /* classid hash size */
  53. #define HTB_HYSTERESIS 1 /* whether to use mode hysteresis for speedup */
  54. #define HTB_VER 0x30011 /* major must be matched with number suplied by TC as version */
  55. #if HTB_VER >> 16 != TC_HTB_PROTOVER
  56. #error "Mismatched sch_htb.c and pkt_sch.h"
  57. #endif
  58. /* used internaly to keep status of single class */
  59. enum htb_cmode {
  60. HTB_CANT_SEND, /* class can't send and can't borrow */
  61. HTB_MAY_BORROW, /* class can't send but may borrow */
  62. HTB_CAN_SEND /* class can send */
  63. };
  64. /* interior & leaf nodes; props specific to leaves are marked L: */
  65. struct htb_class {
  66. /* general class parameters */
  67. u32 classid;
  68. struct gnet_stats_basic bstats;
  69. struct gnet_stats_queue qstats;
  70. struct gnet_stats_rate_est rate_est;
  71. struct tc_htb_xstats xstats; /* our special stats */
  72. int refcnt; /* usage count of this class */
  73. /* topology */
  74. int level; /* our level (see above) */
  75. struct htb_class *parent; /* parent class */
  76. struct hlist_node hlist; /* classid hash list item */
  77. struct list_head sibling; /* sibling list item */
  78. struct list_head children; /* children list */
  79. union {
  80. struct htb_class_leaf {
  81. struct Qdisc *q;
  82. int prio;
  83. int aprio;
  84. int quantum;
  85. int deficit[TC_HTB_MAXDEPTH];
  86. struct list_head drop_list;
  87. } leaf;
  88. struct htb_class_inner {
  89. struct rb_root feed[TC_HTB_NUMPRIO]; /* feed trees */
  90. struct rb_node *ptr[TC_HTB_NUMPRIO]; /* current class ptr */
  91. /* When class changes from state 1->2 and disconnects from
  92. parent's feed then we lost ptr value and start from the
  93. first child again. Here we store classid of the
  94. last valid ptr (used when ptr is NULL). */
  95. u32 last_ptr_id[TC_HTB_NUMPRIO];
  96. } inner;
  97. } un;
  98. struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
  99. struct rb_node pq_node; /* node for event queue */
  100. psched_time_t pq_key;
  101. int prio_activity; /* for which prios are we active */
  102. enum htb_cmode cmode; /* current mode of the class */
  103. /* class attached filters */
  104. struct tcf_proto *filter_list;
  105. int filter_cnt;
  106. int warned; /* only one warning about non work conserving .. */
  107. /* token bucket parameters */
  108. struct qdisc_rate_table *rate; /* rate table of the class itself */
  109. struct qdisc_rate_table *ceil; /* ceiling rate (limits borrows too) */
  110. long buffer, cbuffer; /* token bucket depth/rate */
  111. psched_tdiff_t mbuffer; /* max wait time */
  112. long tokens, ctokens; /* current number of tokens */
  113. psched_time_t t_c; /* checkpoint time */
  114. int prio; /* For parent to leaf return possible here */
  115. int quantum; /* we do backup. Finally full replacement */
  116. /* of un.leaf originals should be done. */
  117. };
  118. static inline long L2T(struct htb_class *cl, struct qdisc_rate_table *rate,
  119. int size)
  120. {
  121. long result = qdisc_l2t(rate, size);
  122. return result;
  123. }
  124. struct htb_sched {
  125. struct list_head root; /* root classes list */
  126. struct hlist_head hash[HTB_HSIZE]; /* hashed by classid */
  127. struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */
  128. /* self list - roots of self generating tree */
  129. struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
  130. int row_mask[TC_HTB_MAXDEPTH];
  131. struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
  132. u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
  133. /* self wait list - roots of wait PQs per row */
  134. struct rb_root wait_pq[TC_HTB_MAXDEPTH];
  135. /* time of nearest event per level (row) */
  136. psched_time_t near_ev_cache[TC_HTB_MAXDEPTH];
  137. /* whether we hit non-work conserving class during this dequeue; we use */
  138. int nwc_hit; /* this to disable mindelay complaint in dequeue */
  139. int defcls; /* class where unclassified flows go to */
  140. /* filters for qdisc itself */
  141. struct tcf_proto *filter_list;
  142. int filter_cnt;
  143. int rate2quantum; /* quant = rate / rate2quantum */
  144. psched_time_t now; /* cached dequeue time */
  145. struct qdisc_watchdog watchdog;
  146. /* non shaped skbs; let them go directly thru */
  147. struct sk_buff_head direct_queue;
  148. int direct_qlen; /* max qlen of above */
  149. long direct_pkts;
  150. };
  151. /* compute hash of size HTB_HSIZE for given handle */
  152. static inline int htb_hash(u32 h)
  153. {
  154. #if HTB_HSIZE != 16
  155. #error "Declare new hash for your HTB_HSIZE"
  156. #endif
  157. h ^= h >> 8; /* stolen from cbq_hash */
  158. h ^= h >> 4;
  159. return h & 0xf;
  160. }
  161. /* find class in global hash table using given handle */
  162. static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
  163. {
  164. struct htb_sched *q = qdisc_priv(sch);
  165. struct hlist_node *p;
  166. struct htb_class *cl;
  167. if (TC_H_MAJ(handle) != sch->handle)
  168. return NULL;
  169. hlist_for_each_entry(cl, p, q->hash + htb_hash(handle), hlist) {
  170. if (cl->classid == handle)
  171. return cl;
  172. }
  173. return NULL;
  174. }
  175. /**
  176. * htb_classify - classify a packet into class
  177. *
  178. * It returns NULL if the packet should be dropped or -1 if the packet
  179. * should be passed directly thru. In all other cases leaf class is returned.
  180. * We allow direct class selection by classid in priority. The we examine
  181. * filters in qdisc and in inner nodes (if higher filter points to the inner
  182. * node). If we end up with classid MAJOR:0 we enqueue the skb into special
  183. * internal fifo (direct). These packets then go directly thru. If we still
  184. * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessfull
  185. * then finish and return direct queue.
  186. */
  187. #define HTB_DIRECT (struct htb_class*)-1
  188. static inline u32 htb_classid(struct htb_class *cl)
  189. {
  190. return (cl && cl != HTB_DIRECT) ? cl->classid : TC_H_UNSPEC;
  191. }
  192. static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
  193. int *qerr)
  194. {
  195. struct htb_sched *q = qdisc_priv(sch);
  196. struct htb_class *cl;
  197. struct tcf_result res;
  198. struct tcf_proto *tcf;
  199. int result;
  200. /* allow to select class by setting skb->priority to valid classid;
  201. note that nfmark can be used too by attaching filter fw with no
  202. rules in it */
  203. if (skb->priority == sch->handle)
  204. return HTB_DIRECT; /* X:0 (direct flow) selected */
  205. if ((cl = htb_find(skb->priority, sch)) != NULL && cl->level == 0)
  206. return cl;
  207. *qerr = NET_XMIT_BYPASS;
  208. tcf = q->filter_list;
  209. while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
  210. #ifdef CONFIG_NET_CLS_ACT
  211. switch (result) {
  212. case TC_ACT_QUEUED:
  213. case TC_ACT_STOLEN:
  214. *qerr = NET_XMIT_SUCCESS;
  215. case TC_ACT_SHOT:
  216. return NULL;
  217. }
  218. #endif
  219. if ((cl = (void *)res.class) == NULL) {
  220. if (res.classid == sch->handle)
  221. return HTB_DIRECT; /* X:0 (direct flow) */
  222. if ((cl = htb_find(res.classid, sch)) == NULL)
  223. break; /* filter selected invalid classid */
  224. }
  225. if (!cl->level)
  226. return cl; /* we hit leaf; return it */
  227. /* we have got inner class; apply inner filter chain */
  228. tcf = cl->filter_list;
  229. }
  230. /* classification failed; try to use default class */
  231. cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
  232. if (!cl || cl->level)
  233. return HTB_DIRECT; /* bad default .. this is safe bet */
  234. return cl;
  235. }
  236. /**
  237. * htb_add_to_id_tree - adds class to the round robin list
  238. *
  239. * Routine adds class to the list (actually tree) sorted by classid.
  240. * Make sure that class is not already on such list for given prio.
  241. */
  242. static void htb_add_to_id_tree(struct rb_root *root,
  243. struct htb_class *cl, int prio)
  244. {
  245. struct rb_node **p = &root->rb_node, *parent = NULL;
  246. while (*p) {
  247. struct htb_class *c;
  248. parent = *p;
  249. c = rb_entry(parent, struct htb_class, node[prio]);
  250. if (cl->classid > c->classid)
  251. p = &parent->rb_right;
  252. else
  253. p = &parent->rb_left;
  254. }
  255. rb_link_node(&cl->node[prio], parent, p);
  256. rb_insert_color(&cl->node[prio], root);
  257. }
  258. /**
  259. * htb_add_to_wait_tree - adds class to the event queue with delay
  260. *
  261. * The class is added to priority event queue to indicate that class will
  262. * change its mode in cl->pq_key microseconds. Make sure that class is not
  263. * already in the queue.
  264. */
  265. static void htb_add_to_wait_tree(struct htb_sched *q,
  266. struct htb_class *cl, long delay)
  267. {
  268. struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
  269. cl->pq_key = q->now + delay;
  270. if (cl->pq_key == q->now)
  271. cl->pq_key++;
  272. /* update the nearest event cache */
  273. if (q->near_ev_cache[cl->level] > cl->pq_key)
  274. q->near_ev_cache[cl->level] = cl->pq_key;
  275. while (*p) {
  276. struct htb_class *c;
  277. parent = *p;
  278. c = rb_entry(parent, struct htb_class, pq_node);
  279. if (cl->pq_key >= c->pq_key)
  280. p = &parent->rb_right;
  281. else
  282. p = &parent->rb_left;
  283. }
  284. rb_link_node(&cl->pq_node, parent, p);
  285. rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
  286. }
  287. /**
  288. * htb_next_rb_node - finds next node in binary tree
  289. *
  290. * When we are past last key we return NULL.
  291. * Average complexity is 2 steps per call.
  292. */
  293. static inline void htb_next_rb_node(struct rb_node **n)
  294. {
  295. *n = rb_next(*n);
  296. }
  297. /**
  298. * htb_add_class_to_row - add class to its row
  299. *
  300. * The class is added to row at priorities marked in mask.
  301. * It does nothing if mask == 0.
  302. */
  303. static inline void htb_add_class_to_row(struct htb_sched *q,
  304. struct htb_class *cl, int mask)
  305. {
  306. q->row_mask[cl->level] |= mask;
  307. while (mask) {
  308. int prio = ffz(~mask);
  309. mask &= ~(1 << prio);
  310. htb_add_to_id_tree(q->row[cl->level] + prio, cl, prio);
  311. }
  312. }
  313. /* If this triggers, it is a bug in this code, but it need not be fatal */
  314. static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
  315. {
  316. if (RB_EMPTY_NODE(rb)) {
  317. WARN_ON(1);
  318. } else {
  319. rb_erase(rb, root);
  320. RB_CLEAR_NODE(rb);
  321. }
  322. }
  323. /**
  324. * htb_remove_class_from_row - removes class from its row
  325. *
  326. * The class is removed from row at priorities marked in mask.
  327. * It does nothing if mask == 0.
  328. */
  329. static inline void htb_remove_class_from_row(struct htb_sched *q,
  330. struct htb_class *cl, int mask)
  331. {
  332. int m = 0;
  333. while (mask) {
  334. int prio = ffz(~mask);
  335. mask &= ~(1 << prio);
  336. if (q->ptr[cl->level][prio] == cl->node + prio)
  337. htb_next_rb_node(q->ptr[cl->level] + prio);
  338. htb_safe_rb_erase(cl->node + prio, q->row[cl->level] + prio);
  339. if (!q->row[cl->level][prio].rb_node)
  340. m |= 1 << prio;
  341. }
  342. q->row_mask[cl->level] &= ~m;
  343. }
  344. /**
  345. * htb_activate_prios - creates active classe's feed chain
  346. *
  347. * The class is connected to ancestors and/or appropriate rows
  348. * for priorities it is participating on. cl->cmode must be new
  349. * (activated) mode. It does nothing if cl->prio_activity == 0.
  350. */
  351. static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
  352. {
  353. struct htb_class *p = cl->parent;
  354. long m, mask = cl->prio_activity;
  355. while (cl->cmode == HTB_MAY_BORROW && p && mask) {
  356. m = mask;
  357. while (m) {
  358. int prio = ffz(~m);
  359. m &= ~(1 << prio);
  360. if (p->un.inner.feed[prio].rb_node)
  361. /* parent already has its feed in use so that
  362. reset bit in mask as parent is already ok */
  363. mask &= ~(1 << prio);
  364. htb_add_to_id_tree(p->un.inner.feed + prio, cl, prio);
  365. }
  366. p->prio_activity |= mask;
  367. cl = p;
  368. p = cl->parent;
  369. }
  370. if (cl->cmode == HTB_CAN_SEND && mask)
  371. htb_add_class_to_row(q, cl, mask);
  372. }
  373. /**
  374. * htb_deactivate_prios - remove class from feed chain
  375. *
  376. * cl->cmode must represent old mode (before deactivation). It does
  377. * nothing if cl->prio_activity == 0. Class is removed from all feed
  378. * chains and rows.
  379. */
  380. static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
  381. {
  382. struct htb_class *p = cl->parent;
  383. long m, mask = cl->prio_activity;
  384. while (cl->cmode == HTB_MAY_BORROW && p && mask) {
  385. m = mask;
  386. mask = 0;
  387. while (m) {
  388. int prio = ffz(~m);
  389. m &= ~(1 << prio);
  390. if (p->un.inner.ptr[prio] == cl->node + prio) {
  391. /* we are removing child which is pointed to from
  392. parent feed - forget the pointer but remember
  393. classid */
  394. p->un.inner.last_ptr_id[prio] = cl->classid;
  395. p->un.inner.ptr[prio] = NULL;
  396. }
  397. htb_safe_rb_erase(cl->node + prio, p->un.inner.feed + prio);
  398. if (!p->un.inner.feed[prio].rb_node)
  399. mask |= 1 << prio;
  400. }
  401. p->prio_activity &= ~mask;
  402. cl = p;
  403. p = cl->parent;
  404. }
  405. if (cl->cmode == HTB_CAN_SEND && mask)
  406. htb_remove_class_from_row(q, cl, mask);
  407. }
  408. #if HTB_HYSTERESIS
  409. static inline long htb_lowater(const struct htb_class *cl)
  410. {
  411. return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
  412. }
  413. static inline long htb_hiwater(const struct htb_class *cl)
  414. {
  415. return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
  416. }
  417. #else
  418. #define htb_lowater(cl) (0)
  419. #define htb_hiwater(cl) (0)
  420. #endif
  421. /**
  422. * htb_class_mode - computes and returns current class mode
  423. *
  424. * It computes cl's mode at time cl->t_c+diff and returns it. If mode
  425. * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
  426. * from now to time when cl will change its state.
  427. * Also it is worth to note that class mode doesn't change simply
  428. * at cl->{c,}tokens == 0 but there can rather be hysteresis of
  429. * 0 .. -cl->{c,}buffer range. It is meant to limit number of
  430. * mode transitions per time unit. The speed gain is about 1/6.
  431. */
  432. static inline enum htb_cmode
  433. htb_class_mode(struct htb_class *cl, long *diff)
  434. {
  435. long toks;
  436. if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
  437. *diff = -toks;
  438. return HTB_CANT_SEND;
  439. }
  440. if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
  441. return HTB_CAN_SEND;
  442. *diff = -toks;
  443. return HTB_MAY_BORROW;
  444. }
  445. /**
  446. * htb_change_class_mode - changes classe's mode
  447. *
  448. * This should be the only way how to change classe's mode under normal
  449. * cirsumstances. Routine will update feed lists linkage, change mode
  450. * and add class to the wait event queue if appropriate. New mode should
  451. * be different from old one and cl->pq_key has to be valid if changing
  452. * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
  453. */
  454. static void
  455. htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff)
  456. {
  457. enum htb_cmode new_mode = htb_class_mode(cl, diff);
  458. if (new_mode == cl->cmode)
  459. return;
  460. if (cl->prio_activity) { /* not necessary: speed optimization */
  461. if (cl->cmode != HTB_CANT_SEND)
  462. htb_deactivate_prios(q, cl);
  463. cl->cmode = new_mode;
  464. if (new_mode != HTB_CANT_SEND)
  465. htb_activate_prios(q, cl);
  466. } else
  467. cl->cmode = new_mode;
  468. }
  469. /**
  470. * htb_activate - inserts leaf cl into appropriate active feeds
  471. *
  472. * Routine learns (new) priority of leaf and activates feed chain
  473. * for the prio. It can be called on already active leaf safely.
  474. * It also adds leaf into droplist.
  475. */
  476. static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
  477. {
  478. BUG_TRAP(!cl->level && cl->un.leaf.q && cl->un.leaf.q->q.qlen);
  479. if (!cl->prio_activity) {
  480. cl->prio_activity = 1 << (cl->un.leaf.aprio = cl->un.leaf.prio);
  481. htb_activate_prios(q, cl);
  482. list_add_tail(&cl->un.leaf.drop_list,
  483. q->drops + cl->un.leaf.aprio);
  484. }
  485. }
  486. /**
  487. * htb_deactivate - remove leaf cl from active feeds
  488. *
  489. * Make sure that leaf is active. In the other words it can't be called
  490. * with non-active leaf. It also removes class from the drop list.
  491. */
  492. static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
  493. {
  494. BUG_TRAP(cl->prio_activity);
  495. htb_deactivate_prios(q, cl);
  496. cl->prio_activity = 0;
  497. list_del_init(&cl->un.leaf.drop_list);
  498. }
  499. static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  500. {
  501. int ret;
  502. struct htb_sched *q = qdisc_priv(sch);
  503. struct htb_class *cl = htb_classify(skb, sch, &ret);
  504. if (cl == HTB_DIRECT) {
  505. /* enqueue to helper queue */
  506. if (q->direct_queue.qlen < q->direct_qlen) {
  507. __skb_queue_tail(&q->direct_queue, skb);
  508. q->direct_pkts++;
  509. } else {
  510. kfree_skb(skb);
  511. sch->qstats.drops++;
  512. return NET_XMIT_DROP;
  513. }
  514. #ifdef CONFIG_NET_CLS_ACT
  515. } else if (!cl) {
  516. if (ret == NET_XMIT_BYPASS)
  517. sch->qstats.drops++;
  518. kfree_skb(skb);
  519. return ret;
  520. #endif
  521. } else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) !=
  522. NET_XMIT_SUCCESS) {
  523. sch->qstats.drops++;
  524. cl->qstats.drops++;
  525. return NET_XMIT_DROP;
  526. } else {
  527. cl->bstats.packets +=
  528. skb_is_gso(skb)?skb_shinfo(skb)->gso_segs:1;
  529. cl->bstats.bytes += skb->len;
  530. htb_activate(q, cl);
  531. }
  532. sch->q.qlen++;
  533. sch->bstats.packets += skb_is_gso(skb)?skb_shinfo(skb)->gso_segs:1;
  534. sch->bstats.bytes += skb->len;
  535. return NET_XMIT_SUCCESS;
  536. }
  537. /* TODO: requeuing packet charges it to policers again !! */
  538. static int htb_requeue(struct sk_buff *skb, struct Qdisc *sch)
  539. {
  540. struct htb_sched *q = qdisc_priv(sch);
  541. int ret = NET_XMIT_SUCCESS;
  542. struct htb_class *cl = htb_classify(skb, sch, &ret);
  543. struct sk_buff *tskb;
  544. if (cl == HTB_DIRECT || !cl) {
  545. /* enqueue to helper queue */
  546. if (q->direct_queue.qlen < q->direct_qlen && cl) {
  547. __skb_queue_head(&q->direct_queue, skb);
  548. } else {
  549. __skb_queue_head(&q->direct_queue, skb);
  550. tskb = __skb_dequeue_tail(&q->direct_queue);
  551. kfree_skb(tskb);
  552. sch->qstats.drops++;
  553. return NET_XMIT_CN;
  554. }
  555. } else if (cl->un.leaf.q->ops->requeue(skb, cl->un.leaf.q) !=
  556. NET_XMIT_SUCCESS) {
  557. sch->qstats.drops++;
  558. cl->qstats.drops++;
  559. return NET_XMIT_DROP;
  560. } else
  561. htb_activate(q, cl);
  562. sch->q.qlen++;
  563. sch->qstats.requeues++;
  564. return NET_XMIT_SUCCESS;
  565. }
  566. /**
  567. * htb_charge_class - charges amount "bytes" to leaf and ancestors
  568. *
  569. * Routine assumes that packet "bytes" long was dequeued from leaf cl
  570. * borrowing from "level". It accounts bytes to ceil leaky bucket for
  571. * leaf and all ancestors and to rate bucket for ancestors at levels
  572. * "level" and higher. It also handles possible change of mode resulting
  573. * from the update. Note that mode can also increase here (MAY_BORROW to
  574. * CAN_SEND) because we can use more precise clock that event queue here.
  575. * In such case we remove class from event queue first.
  576. */
  577. static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
  578. int level, struct sk_buff *skb)
  579. {
  580. int bytes = skb->len;
  581. long toks, diff;
  582. enum htb_cmode old_mode;
  583. #define HTB_ACCNT(T,B,R) toks = diff + cl->T; \
  584. if (toks > cl->B) toks = cl->B; \
  585. toks -= L2T(cl, cl->R, bytes); \
  586. if (toks <= -cl->mbuffer) toks = 1-cl->mbuffer; \
  587. cl->T = toks
  588. while (cl) {
  589. diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
  590. if (cl->level >= level) {
  591. if (cl->level == level)
  592. cl->xstats.lends++;
  593. HTB_ACCNT(tokens, buffer, rate);
  594. } else {
  595. cl->xstats.borrows++;
  596. cl->tokens += diff; /* we moved t_c; update tokens */
  597. }
  598. HTB_ACCNT(ctokens, cbuffer, ceil);
  599. cl->t_c = q->now;
  600. old_mode = cl->cmode;
  601. diff = 0;
  602. htb_change_class_mode(q, cl, &diff);
  603. if (old_mode != cl->cmode) {
  604. if (old_mode != HTB_CAN_SEND)
  605. htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
  606. if (cl->cmode != HTB_CAN_SEND)
  607. htb_add_to_wait_tree(q, cl, diff);
  608. }
  609. /* update byte stats except for leaves which are already updated */
  610. if (cl->level) {
  611. cl->bstats.bytes += bytes;
  612. cl->bstats.packets += skb_is_gso(skb)?
  613. skb_shinfo(skb)->gso_segs:1;
  614. }
  615. cl = cl->parent;
  616. }
  617. }
  618. /**
  619. * htb_do_events - make mode changes to classes at the level
  620. *
  621. * Scans event queue for pending events and applies them. Returns time of
  622. * next pending event (0 for no event in pq).
  623. * Note: Applied are events whose have cl->pq_key <= q->now.
  624. */
  625. static psched_time_t htb_do_events(struct htb_sched *q, int level)
  626. {
  627. int i;
  628. for (i = 0; i < 500; i++) {
  629. struct htb_class *cl;
  630. long diff;
  631. struct rb_node *p = rb_first(&q->wait_pq[level]);
  632. if (!p)
  633. return 0;
  634. cl = rb_entry(p, struct htb_class, pq_node);
  635. if (cl->pq_key > q->now)
  636. return cl->pq_key;
  637. htb_safe_rb_erase(p, q->wait_pq + level);
  638. diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
  639. htb_change_class_mode(q, cl, &diff);
  640. if (cl->cmode != HTB_CAN_SEND)
  641. htb_add_to_wait_tree(q, cl, diff);
  642. }
  643. if (net_ratelimit())
  644. printk(KERN_WARNING "htb: too many events !\n");
  645. return q->now + PSCHED_TICKS_PER_SEC / 10;
  646. }
  647. /* Returns class->node+prio from id-tree where classe's id is >= id. NULL
  648. is no such one exists. */
  649. static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
  650. u32 id)
  651. {
  652. struct rb_node *r = NULL;
  653. while (n) {
  654. struct htb_class *cl =
  655. rb_entry(n, struct htb_class, node[prio]);
  656. if (id == cl->classid)
  657. return n;
  658. if (id > cl->classid) {
  659. n = n->rb_right;
  660. } else {
  661. r = n;
  662. n = n->rb_left;
  663. }
  664. }
  665. return r;
  666. }
  667. /**
  668. * htb_lookup_leaf - returns next leaf class in DRR order
  669. *
  670. * Find leaf where current feed pointers points to.
  671. */
  672. static struct htb_class *htb_lookup_leaf(struct rb_root *tree, int prio,
  673. struct rb_node **pptr, u32 * pid)
  674. {
  675. int i;
  676. struct {
  677. struct rb_node *root;
  678. struct rb_node **pptr;
  679. u32 *pid;
  680. } stk[TC_HTB_MAXDEPTH], *sp = stk;
  681. BUG_TRAP(tree->rb_node);
  682. sp->root = tree->rb_node;
  683. sp->pptr = pptr;
  684. sp->pid = pid;
  685. for (i = 0; i < 65535; i++) {
  686. if (!*sp->pptr && *sp->pid) {
  687. /* ptr was invalidated but id is valid - try to recover
  688. the original or next ptr */
  689. *sp->pptr =
  690. htb_id_find_next_upper(prio, sp->root, *sp->pid);
  691. }
  692. *sp->pid = 0; /* ptr is valid now so that remove this hint as it
  693. can become out of date quickly */
  694. if (!*sp->pptr) { /* we are at right end; rewind & go up */
  695. *sp->pptr = sp->root;
  696. while ((*sp->pptr)->rb_left)
  697. *sp->pptr = (*sp->pptr)->rb_left;
  698. if (sp > stk) {
  699. sp--;
  700. BUG_TRAP(*sp->pptr);
  701. if (!*sp->pptr)
  702. return NULL;
  703. htb_next_rb_node(sp->pptr);
  704. }
  705. } else {
  706. struct htb_class *cl;
  707. cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
  708. if (!cl->level)
  709. return cl;
  710. (++sp)->root = cl->un.inner.feed[prio].rb_node;
  711. sp->pptr = cl->un.inner.ptr + prio;
  712. sp->pid = cl->un.inner.last_ptr_id + prio;
  713. }
  714. }
  715. BUG_TRAP(0);
  716. return NULL;
  717. }
  718. /* dequeues packet at given priority and level; call only if
  719. you are sure that there is active class at prio/level */
  720. static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, int prio,
  721. int level)
  722. {
  723. struct sk_buff *skb = NULL;
  724. struct htb_class *cl, *start;
  725. /* look initial class up in the row */
  726. start = cl = htb_lookup_leaf(q->row[level] + prio, prio,
  727. q->ptr[level] + prio,
  728. q->last_ptr_id[level] + prio);
  729. do {
  730. next:
  731. BUG_TRAP(cl);
  732. if (!cl)
  733. return NULL;
  734. /* class can be empty - it is unlikely but can be true if leaf
  735. qdisc drops packets in enqueue routine or if someone used
  736. graft operation on the leaf since last dequeue;
  737. simply deactivate and skip such class */
  738. if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
  739. struct htb_class *next;
  740. htb_deactivate(q, cl);
  741. /* row/level might become empty */
  742. if ((q->row_mask[level] & (1 << prio)) == 0)
  743. return NULL;
  744. next = htb_lookup_leaf(q->row[level] + prio,
  745. prio, q->ptr[level] + prio,
  746. q->last_ptr_id[level] + prio);
  747. if (cl == start) /* fix start if we just deleted it */
  748. start = next;
  749. cl = next;
  750. goto next;
  751. }
  752. skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
  753. if (likely(skb != NULL))
  754. break;
  755. if (!cl->warned) {
  756. printk(KERN_WARNING
  757. "htb: class %X isn't work conserving ?!\n",
  758. cl->classid);
  759. cl->warned = 1;
  760. }
  761. q->nwc_hit++;
  762. htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
  763. ptr[0]) + prio);
  764. cl = htb_lookup_leaf(q->row[level] + prio, prio,
  765. q->ptr[level] + prio,
  766. q->last_ptr_id[level] + prio);
  767. } while (cl != start);
  768. if (likely(skb != NULL)) {
  769. if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
  770. cl->un.leaf.deficit[level] += cl->un.leaf.quantum;
  771. htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
  772. ptr[0]) + prio);
  773. }
  774. /* this used to be after charge_class but this constelation
  775. gives us slightly better performance */
  776. if (!cl->un.leaf.q->q.qlen)
  777. htb_deactivate(q, cl);
  778. htb_charge_class(q, cl, level, skb);
  779. }
  780. return skb;
  781. }
  782. static struct sk_buff *htb_dequeue(struct Qdisc *sch)
  783. {
  784. struct sk_buff *skb = NULL;
  785. struct htb_sched *q = qdisc_priv(sch);
  786. int level;
  787. psched_time_t next_event;
  788. /* try to dequeue direct packets as high prio (!) to minimize cpu work */
  789. skb = __skb_dequeue(&q->direct_queue);
  790. if (skb != NULL) {
  791. sch->flags &= ~TCQ_F_THROTTLED;
  792. sch->q.qlen--;
  793. return skb;
  794. }
  795. if (!sch->q.qlen)
  796. goto fin;
  797. q->now = psched_get_time();
  798. next_event = q->now + 5 * PSCHED_TICKS_PER_SEC;
  799. q->nwc_hit = 0;
  800. for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
  801. /* common case optimization - skip event handler quickly */
  802. int m;
  803. psched_time_t event;
  804. if (q->now >= q->near_ev_cache[level]) {
  805. event = htb_do_events(q, level);
  806. if (!event)
  807. event = q->now + PSCHED_TICKS_PER_SEC;
  808. q->near_ev_cache[level] = event;
  809. } else
  810. event = q->near_ev_cache[level];
  811. if (event && next_event > event)
  812. next_event = event;
  813. m = ~q->row_mask[level];
  814. while (m != (int)(-1)) {
  815. int prio = ffz(m);
  816. m |= 1 << prio;
  817. skb = htb_dequeue_tree(q, prio, level);
  818. if (likely(skb != NULL)) {
  819. sch->q.qlen--;
  820. sch->flags &= ~TCQ_F_THROTTLED;
  821. goto fin;
  822. }
  823. }
  824. }
  825. sch->qstats.overlimits++;
  826. qdisc_watchdog_schedule(&q->watchdog, next_event);
  827. fin:
  828. return skb;
  829. }
  830. /* try to drop from each class (by prio) until one succeed */
  831. static unsigned int htb_drop(struct Qdisc *sch)
  832. {
  833. struct htb_sched *q = qdisc_priv(sch);
  834. int prio;
  835. for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
  836. struct list_head *p;
  837. list_for_each(p, q->drops + prio) {
  838. struct htb_class *cl = list_entry(p, struct htb_class,
  839. un.leaf.drop_list);
  840. unsigned int len;
  841. if (cl->un.leaf.q->ops->drop &&
  842. (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
  843. sch->q.qlen--;
  844. if (!cl->un.leaf.q->q.qlen)
  845. htb_deactivate(q, cl);
  846. return len;
  847. }
  848. }
  849. }
  850. return 0;
  851. }
  852. /* reset all classes */
  853. /* always caled under BH & queue lock */
  854. static void htb_reset(struct Qdisc *sch)
  855. {
  856. struct htb_sched *q = qdisc_priv(sch);
  857. int i;
  858. for (i = 0; i < HTB_HSIZE; i++) {
  859. struct hlist_node *p;
  860. struct htb_class *cl;
  861. hlist_for_each_entry(cl, p, q->hash + i, hlist) {
  862. if (cl->level)
  863. memset(&cl->un.inner, 0, sizeof(cl->un.inner));
  864. else {
  865. if (cl->un.leaf.q)
  866. qdisc_reset(cl->un.leaf.q);
  867. INIT_LIST_HEAD(&cl->un.leaf.drop_list);
  868. }
  869. cl->prio_activity = 0;
  870. cl->cmode = HTB_CAN_SEND;
  871. }
  872. }
  873. qdisc_watchdog_cancel(&q->watchdog);
  874. __skb_queue_purge(&q->direct_queue);
  875. sch->q.qlen = 0;
  876. memset(q->row, 0, sizeof(q->row));
  877. memset(q->row_mask, 0, sizeof(q->row_mask));
  878. memset(q->wait_pq, 0, sizeof(q->wait_pq));
  879. memset(q->ptr, 0, sizeof(q->ptr));
  880. for (i = 0; i < TC_HTB_NUMPRIO; i++)
  881. INIT_LIST_HEAD(q->drops + i);
  882. }
  883. static int htb_init(struct Qdisc *sch, struct rtattr *opt)
  884. {
  885. struct htb_sched *q = qdisc_priv(sch);
  886. struct rtattr *tb[TCA_HTB_INIT];
  887. struct tc_htb_glob *gopt;
  888. int i;
  889. if (!opt || rtattr_parse_nested(tb, TCA_HTB_INIT, opt) ||
  890. tb[TCA_HTB_INIT - 1] == NULL ||
  891. RTA_PAYLOAD(tb[TCA_HTB_INIT - 1]) < sizeof(*gopt)) {
  892. printk(KERN_ERR "HTB: hey probably you have bad tc tool ?\n");
  893. return -EINVAL;
  894. }
  895. gopt = RTA_DATA(tb[TCA_HTB_INIT - 1]);
  896. if (gopt->version != HTB_VER >> 16) {
  897. printk(KERN_ERR
  898. "HTB: need tc/htb version %d (minor is %d), you have %d\n",
  899. HTB_VER >> 16, HTB_VER & 0xffff, gopt->version);
  900. return -EINVAL;
  901. }
  902. INIT_LIST_HEAD(&q->root);
  903. for (i = 0; i < HTB_HSIZE; i++)
  904. INIT_HLIST_HEAD(q->hash + i);
  905. for (i = 0; i < TC_HTB_NUMPRIO; i++)
  906. INIT_LIST_HEAD(q->drops + i);
  907. qdisc_watchdog_init(&q->watchdog, sch);
  908. skb_queue_head_init(&q->direct_queue);
  909. q->direct_qlen = sch->dev->tx_queue_len;
  910. if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
  911. q->direct_qlen = 2;
  912. if ((q->rate2quantum = gopt->rate2quantum) < 1)
  913. q->rate2quantum = 1;
  914. q->defcls = gopt->defcls;
  915. return 0;
  916. }
  917. static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
  918. {
  919. struct htb_sched *q = qdisc_priv(sch);
  920. unsigned char *b = skb_tail_pointer(skb);
  921. struct rtattr *rta;
  922. struct tc_htb_glob gopt;
  923. spin_lock_bh(&sch->dev->queue_lock);
  924. gopt.direct_pkts = q->direct_pkts;
  925. gopt.version = HTB_VER;
  926. gopt.rate2quantum = q->rate2quantum;
  927. gopt.defcls = q->defcls;
  928. gopt.debug = 0;
  929. rta = (struct rtattr *)b;
  930. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  931. RTA_PUT(skb, TCA_HTB_INIT, sizeof(gopt), &gopt);
  932. rta->rta_len = skb_tail_pointer(skb) - b;
  933. spin_unlock_bh(&sch->dev->queue_lock);
  934. return skb->len;
  935. rtattr_failure:
  936. spin_unlock_bh(&sch->dev->queue_lock);
  937. nlmsg_trim(skb, skb_tail_pointer(skb));
  938. return -1;
  939. }
  940. static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
  941. struct sk_buff *skb, struct tcmsg *tcm)
  942. {
  943. struct htb_class *cl = (struct htb_class *)arg;
  944. unsigned char *b = skb_tail_pointer(skb);
  945. struct rtattr *rta;
  946. struct tc_htb_opt opt;
  947. spin_lock_bh(&sch->dev->queue_lock);
  948. tcm->tcm_parent = cl->parent ? cl->parent->classid : TC_H_ROOT;
  949. tcm->tcm_handle = cl->classid;
  950. if (!cl->level && cl->un.leaf.q)
  951. tcm->tcm_info = cl->un.leaf.q->handle;
  952. rta = (struct rtattr *)b;
  953. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  954. memset(&opt, 0, sizeof(opt));
  955. opt.rate = cl->rate->rate;
  956. opt.buffer = cl->buffer;
  957. opt.ceil = cl->ceil->rate;
  958. opt.cbuffer = cl->cbuffer;
  959. opt.quantum = cl->un.leaf.quantum;
  960. opt.prio = cl->un.leaf.prio;
  961. opt.level = cl->level;
  962. RTA_PUT(skb, TCA_HTB_PARMS, sizeof(opt), &opt);
  963. rta->rta_len = skb_tail_pointer(skb) - b;
  964. spin_unlock_bh(&sch->dev->queue_lock);
  965. return skb->len;
  966. rtattr_failure:
  967. spin_unlock_bh(&sch->dev->queue_lock);
  968. nlmsg_trim(skb, b);
  969. return -1;
  970. }
  971. static int
  972. htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
  973. {
  974. struct htb_class *cl = (struct htb_class *)arg;
  975. if (!cl->level && cl->un.leaf.q)
  976. cl->qstats.qlen = cl->un.leaf.q->q.qlen;
  977. cl->xstats.tokens = cl->tokens;
  978. cl->xstats.ctokens = cl->ctokens;
  979. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  980. gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
  981. gnet_stats_copy_queue(d, &cl->qstats) < 0)
  982. return -1;
  983. return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
  984. }
  985. static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  986. struct Qdisc **old)
  987. {
  988. struct htb_class *cl = (struct htb_class *)arg;
  989. if (cl && !cl->level) {
  990. if (new == NULL &&
  991. (new = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  992. cl->classid))
  993. == NULL)
  994. return -ENOBUFS;
  995. sch_tree_lock(sch);
  996. if ((*old = xchg(&cl->un.leaf.q, new)) != NULL) {
  997. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  998. qdisc_reset(*old);
  999. }
  1000. sch_tree_unlock(sch);
  1001. return 0;
  1002. }
  1003. return -ENOENT;
  1004. }
  1005. static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
  1006. {
  1007. struct htb_class *cl = (struct htb_class *)arg;
  1008. return (cl && !cl->level) ? cl->un.leaf.q : NULL;
  1009. }
  1010. static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
  1011. {
  1012. struct htb_class *cl = (struct htb_class *)arg;
  1013. if (cl->un.leaf.q->q.qlen == 0)
  1014. htb_deactivate(qdisc_priv(sch), cl);
  1015. }
  1016. static unsigned long htb_get(struct Qdisc *sch, u32 classid)
  1017. {
  1018. struct htb_class *cl = htb_find(classid, sch);
  1019. if (cl)
  1020. cl->refcnt++;
  1021. return (unsigned long)cl;
  1022. }
  1023. static inline int htb_parent_last_child(struct htb_class *cl)
  1024. {
  1025. if (!cl->parent)
  1026. /* the root class */
  1027. return 0;
  1028. if (!(cl->parent->children.next == &cl->sibling &&
  1029. cl->parent->children.prev == &cl->sibling))
  1030. /* not the last child */
  1031. return 0;
  1032. return 1;
  1033. }
  1034. static void htb_parent_to_leaf(struct htb_class *cl, struct Qdisc *new_q)
  1035. {
  1036. struct htb_class *parent = cl->parent;
  1037. BUG_TRAP(!cl->level && cl->un.leaf.q && !cl->prio_activity);
  1038. parent->level = 0;
  1039. memset(&parent->un.inner, 0, sizeof(parent->un.inner));
  1040. INIT_LIST_HEAD(&parent->un.leaf.drop_list);
  1041. parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
  1042. parent->un.leaf.quantum = parent->quantum;
  1043. parent->un.leaf.prio = parent->prio;
  1044. parent->tokens = parent->buffer;
  1045. parent->ctokens = parent->cbuffer;
  1046. parent->t_c = psched_get_time();
  1047. parent->cmode = HTB_CAN_SEND;
  1048. }
  1049. static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
  1050. {
  1051. struct htb_sched *q = qdisc_priv(sch);
  1052. if (!cl->level) {
  1053. BUG_TRAP(cl->un.leaf.q);
  1054. qdisc_destroy(cl->un.leaf.q);
  1055. }
  1056. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  1057. qdisc_put_rtab(cl->rate);
  1058. qdisc_put_rtab(cl->ceil);
  1059. tcf_destroy_chain(cl->filter_list);
  1060. while (!list_empty(&cl->children))
  1061. htb_destroy_class(sch, list_entry(cl->children.next,
  1062. struct htb_class, sibling));
  1063. /* note: this delete may happen twice (see htb_delete) */
  1064. hlist_del_init(&cl->hlist);
  1065. list_del(&cl->sibling);
  1066. if (cl->prio_activity)
  1067. htb_deactivate(q, cl);
  1068. if (cl->cmode != HTB_CAN_SEND)
  1069. htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
  1070. kfree(cl);
  1071. }
  1072. /* always caled under BH & queue lock */
  1073. static void htb_destroy(struct Qdisc *sch)
  1074. {
  1075. struct htb_sched *q = qdisc_priv(sch);
  1076. qdisc_watchdog_cancel(&q->watchdog);
  1077. /* This line used to be after htb_destroy_class call below
  1078. and surprisingly it worked in 2.4. But it must precede it
  1079. because filter need its target class alive to be able to call
  1080. unbind_filter on it (without Oops). */
  1081. tcf_destroy_chain(q->filter_list);
  1082. while (!list_empty(&q->root))
  1083. htb_destroy_class(sch, list_entry(q->root.next,
  1084. struct htb_class, sibling));
  1085. __skb_queue_purge(&q->direct_queue);
  1086. }
  1087. static int htb_delete(struct Qdisc *sch, unsigned long arg)
  1088. {
  1089. struct htb_sched *q = qdisc_priv(sch);
  1090. struct htb_class *cl = (struct htb_class *)arg;
  1091. unsigned int qlen;
  1092. struct Qdisc *new_q = NULL;
  1093. int last_child = 0;
  1094. // TODO: why don't allow to delete subtree ? references ? does
  1095. // tc subsys quarantee us that in htb_destroy it holds no class
  1096. // refs so that we can remove children safely there ?
  1097. if (!list_empty(&cl->children) || cl->filter_cnt)
  1098. return -EBUSY;
  1099. if (!cl->level && htb_parent_last_child(cl)) {
  1100. new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  1101. cl->parent->classid);
  1102. last_child = 1;
  1103. }
  1104. sch_tree_lock(sch);
  1105. if (!cl->level) {
  1106. qlen = cl->un.leaf.q->q.qlen;
  1107. qdisc_reset(cl->un.leaf.q);
  1108. qdisc_tree_decrease_qlen(cl->un.leaf.q, qlen);
  1109. }
  1110. /* delete from hash and active; remainder in destroy_class */
  1111. hlist_del_init(&cl->hlist);
  1112. if (cl->prio_activity)
  1113. htb_deactivate(q, cl);
  1114. if (last_child)
  1115. htb_parent_to_leaf(cl, new_q);
  1116. if (--cl->refcnt == 0)
  1117. htb_destroy_class(sch, cl);
  1118. sch_tree_unlock(sch);
  1119. return 0;
  1120. }
  1121. static void htb_put(struct Qdisc *sch, unsigned long arg)
  1122. {
  1123. struct htb_class *cl = (struct htb_class *)arg;
  1124. if (--cl->refcnt == 0)
  1125. htb_destroy_class(sch, cl);
  1126. }
  1127. static int htb_change_class(struct Qdisc *sch, u32 classid,
  1128. u32 parentid, struct rtattr **tca,
  1129. unsigned long *arg)
  1130. {
  1131. int err = -EINVAL;
  1132. struct htb_sched *q = qdisc_priv(sch);
  1133. struct htb_class *cl = (struct htb_class *)*arg, *parent;
  1134. struct rtattr *opt = tca[TCA_OPTIONS - 1];
  1135. struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
  1136. struct rtattr *tb[TCA_HTB_RTAB];
  1137. struct tc_htb_opt *hopt;
  1138. /* extract all subattrs from opt attr */
  1139. if (!opt || rtattr_parse_nested(tb, TCA_HTB_RTAB, opt) ||
  1140. tb[TCA_HTB_PARMS - 1] == NULL ||
  1141. RTA_PAYLOAD(tb[TCA_HTB_PARMS - 1]) < sizeof(*hopt))
  1142. goto failure;
  1143. parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
  1144. hopt = RTA_DATA(tb[TCA_HTB_PARMS - 1]);
  1145. rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB - 1]);
  1146. ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB - 1]);
  1147. if (!rtab || !ctab)
  1148. goto failure;
  1149. if (!cl) { /* new class */
  1150. struct Qdisc *new_q;
  1151. int prio;
  1152. struct {
  1153. struct rtattr rta;
  1154. struct gnet_estimator opt;
  1155. } est = {
  1156. .rta = {
  1157. .rta_len = RTA_LENGTH(sizeof(est.opt)),
  1158. .rta_type = TCA_RATE,
  1159. },
  1160. .opt = {
  1161. /* 4s interval, 16s averaging constant */
  1162. .interval = 2,
  1163. .ewma_log = 2,
  1164. },
  1165. };
  1166. /* check for valid classid */
  1167. if (!classid || TC_H_MAJ(classid ^ sch->handle)
  1168. || htb_find(classid, sch))
  1169. goto failure;
  1170. /* check maximal depth */
  1171. if (parent && parent->parent && parent->parent->level < 2) {
  1172. printk(KERN_ERR "htb: tree is too deep\n");
  1173. goto failure;
  1174. }
  1175. err = -ENOBUFS;
  1176. if ((cl = kzalloc(sizeof(*cl), GFP_KERNEL)) == NULL)
  1177. goto failure;
  1178. gen_new_estimator(&cl->bstats, &cl->rate_est,
  1179. &sch->dev->queue_lock,
  1180. tca[TCA_RATE-1] ? : &est.rta);
  1181. cl->refcnt = 1;
  1182. INIT_LIST_HEAD(&cl->sibling);
  1183. INIT_HLIST_NODE(&cl->hlist);
  1184. INIT_LIST_HEAD(&cl->children);
  1185. INIT_LIST_HEAD(&cl->un.leaf.drop_list);
  1186. RB_CLEAR_NODE(&cl->pq_node);
  1187. for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
  1188. RB_CLEAR_NODE(&cl->node[prio]);
  1189. /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
  1190. so that can't be used inside of sch_tree_lock
  1191. -- thanks to Karlis Peisenieks */
  1192. new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, classid);
  1193. sch_tree_lock(sch);
  1194. if (parent && !parent->level) {
  1195. unsigned int qlen = parent->un.leaf.q->q.qlen;
  1196. /* turn parent into inner node */
  1197. qdisc_reset(parent->un.leaf.q);
  1198. qdisc_tree_decrease_qlen(parent->un.leaf.q, qlen);
  1199. qdisc_destroy(parent->un.leaf.q);
  1200. if (parent->prio_activity)
  1201. htb_deactivate(q, parent);
  1202. /* remove from evt list because of level change */
  1203. if (parent->cmode != HTB_CAN_SEND) {
  1204. htb_safe_rb_erase(&parent->pq_node, q->wait_pq);
  1205. parent->cmode = HTB_CAN_SEND;
  1206. }
  1207. parent->level = (parent->parent ? parent->parent->level
  1208. : TC_HTB_MAXDEPTH) - 1;
  1209. memset(&parent->un.inner, 0, sizeof(parent->un.inner));
  1210. }
  1211. /* leaf (we) needs elementary qdisc */
  1212. cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
  1213. cl->classid = classid;
  1214. cl->parent = parent;
  1215. /* set class to be in HTB_CAN_SEND state */
  1216. cl->tokens = hopt->buffer;
  1217. cl->ctokens = hopt->cbuffer;
  1218. cl->mbuffer = 60 * PSCHED_TICKS_PER_SEC; /* 1min */
  1219. cl->t_c = psched_get_time();
  1220. cl->cmode = HTB_CAN_SEND;
  1221. /* attach to the hash list and parent's family */
  1222. hlist_add_head(&cl->hlist, q->hash + htb_hash(classid));
  1223. list_add_tail(&cl->sibling,
  1224. parent ? &parent->children : &q->root);
  1225. } else {
  1226. if (tca[TCA_RATE-1])
  1227. gen_replace_estimator(&cl->bstats, &cl->rate_est,
  1228. &sch->dev->queue_lock,
  1229. tca[TCA_RATE-1]);
  1230. sch_tree_lock(sch);
  1231. }
  1232. /* it used to be a nasty bug here, we have to check that node
  1233. is really leaf before changing cl->un.leaf ! */
  1234. if (!cl->level) {
  1235. cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
  1236. if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
  1237. printk(KERN_WARNING
  1238. "HTB: quantum of class %X is small. Consider r2q change.\n",
  1239. cl->classid);
  1240. cl->un.leaf.quantum = 1000;
  1241. }
  1242. if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
  1243. printk(KERN_WARNING
  1244. "HTB: quantum of class %X is big. Consider r2q change.\n",
  1245. cl->classid);
  1246. cl->un.leaf.quantum = 200000;
  1247. }
  1248. if (hopt->quantum)
  1249. cl->un.leaf.quantum = hopt->quantum;
  1250. if ((cl->un.leaf.prio = hopt->prio) >= TC_HTB_NUMPRIO)
  1251. cl->un.leaf.prio = TC_HTB_NUMPRIO - 1;
  1252. /* backup for htb_parent_to_leaf */
  1253. cl->quantum = cl->un.leaf.quantum;
  1254. cl->prio = cl->un.leaf.prio;
  1255. }
  1256. cl->buffer = hopt->buffer;
  1257. cl->cbuffer = hopt->cbuffer;
  1258. if (cl->rate)
  1259. qdisc_put_rtab(cl->rate);
  1260. cl->rate = rtab;
  1261. if (cl->ceil)
  1262. qdisc_put_rtab(cl->ceil);
  1263. cl->ceil = ctab;
  1264. sch_tree_unlock(sch);
  1265. *arg = (unsigned long)cl;
  1266. return 0;
  1267. failure:
  1268. if (rtab)
  1269. qdisc_put_rtab(rtab);
  1270. if (ctab)
  1271. qdisc_put_rtab(ctab);
  1272. return err;
  1273. }
  1274. static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
  1275. {
  1276. struct htb_sched *q = qdisc_priv(sch);
  1277. struct htb_class *cl = (struct htb_class *)arg;
  1278. struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
  1279. return fl;
  1280. }
  1281. static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
  1282. u32 classid)
  1283. {
  1284. struct htb_sched *q = qdisc_priv(sch);
  1285. struct htb_class *cl = htb_find(classid, sch);
  1286. /*if (cl && !cl->level) return 0;
  1287. The line above used to be there to prevent attaching filters to
  1288. leaves. But at least tc_index filter uses this just to get class
  1289. for other reasons so that we have to allow for it.
  1290. ----
  1291. 19.6.2002 As Werner explained it is ok - bind filter is just
  1292. another way to "lock" the class - unlike "get" this lock can
  1293. be broken by class during destroy IIUC.
  1294. */
  1295. if (cl)
  1296. cl->filter_cnt++;
  1297. else
  1298. q->filter_cnt++;
  1299. return (unsigned long)cl;
  1300. }
  1301. static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
  1302. {
  1303. struct htb_sched *q = qdisc_priv(sch);
  1304. struct htb_class *cl = (struct htb_class *)arg;
  1305. if (cl)
  1306. cl->filter_cnt--;
  1307. else
  1308. q->filter_cnt--;
  1309. }
  1310. static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  1311. {
  1312. struct htb_sched *q = qdisc_priv(sch);
  1313. int i;
  1314. if (arg->stop)
  1315. return;
  1316. for (i = 0; i < HTB_HSIZE; i++) {
  1317. struct hlist_node *p;
  1318. struct htb_class *cl;
  1319. hlist_for_each_entry(cl, p, q->hash + i, hlist) {
  1320. if (arg->count < arg->skip) {
  1321. arg->count++;
  1322. continue;
  1323. }
  1324. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  1325. arg->stop = 1;
  1326. return;
  1327. }
  1328. arg->count++;
  1329. }
  1330. }
  1331. }
  1332. static struct Qdisc_class_ops htb_class_ops = {
  1333. .graft = htb_graft,
  1334. .leaf = htb_leaf,
  1335. .qlen_notify = htb_qlen_notify,
  1336. .get = htb_get,
  1337. .put = htb_put,
  1338. .change = htb_change_class,
  1339. .delete = htb_delete,
  1340. .walk = htb_walk,
  1341. .tcf_chain = htb_find_tcf,
  1342. .bind_tcf = htb_bind_filter,
  1343. .unbind_tcf = htb_unbind_filter,
  1344. .dump = htb_dump_class,
  1345. .dump_stats = htb_dump_class_stats,
  1346. };
  1347. static struct Qdisc_ops htb_qdisc_ops = {
  1348. .next = NULL,
  1349. .cl_ops = &htb_class_ops,
  1350. .id = "htb",
  1351. .priv_size = sizeof(struct htb_sched),
  1352. .enqueue = htb_enqueue,
  1353. .dequeue = htb_dequeue,
  1354. .requeue = htb_requeue,
  1355. .drop = htb_drop,
  1356. .init = htb_init,
  1357. .reset = htb_reset,
  1358. .destroy = htb_destroy,
  1359. .change = NULL /* htb_change */,
  1360. .dump = htb_dump,
  1361. .owner = THIS_MODULE,
  1362. };
  1363. static int __init htb_module_init(void)
  1364. {
  1365. return register_qdisc(&htb_qdisc_ops);
  1366. }
  1367. static void __exit htb_module_exit(void)
  1368. {
  1369. unregister_qdisc(&htb_qdisc_ops);
  1370. }
  1371. module_init(htb_module_init)
  1372. module_exit(htb_module_exit)
  1373. MODULE_LICENSE("GPL");