sch_htb.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. /* vim: ts=8 sw=8
  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/config.h>
  31. #include <linux/module.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/system.h>
  34. #include <linux/bitops.h>
  35. #include <linux/types.h>
  36. #include <linux/kernel.h>
  37. #include <linux/sched.h>
  38. #include <linux/string.h>
  39. #include <linux/mm.h>
  40. #include <linux/socket.h>
  41. #include <linux/sockios.h>
  42. #include <linux/in.h>
  43. #include <linux/errno.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/if_ether.h>
  46. #include <linux/inet.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/etherdevice.h>
  49. #include <linux/notifier.h>
  50. #include <net/ip.h>
  51. #include <net/route.h>
  52. #include <linux/skbuff.h>
  53. #include <linux/list.h>
  54. #include <linux/compiler.h>
  55. #include <net/sock.h>
  56. #include <net/pkt_sched.h>
  57. #include <linux/rbtree.h>
  58. /* HTB algorithm.
  59. Author: devik@cdi.cz
  60. ========================================================================
  61. HTB is like TBF with multiple classes. It is also similar to CBQ because
  62. it allows to assign priority to each class in hierarchy.
  63. In fact it is another implementation of Floyd's formal sharing.
  64. Levels:
  65. Each class is assigned level. Leaf has ALWAYS level 0 and root
  66. classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
  67. one less than their parent.
  68. */
  69. #define HTB_HSIZE 16 /* classid hash size */
  70. #define HTB_EWMAC 2 /* rate average over HTB_EWMAC*HTB_HSIZE sec */
  71. #undef HTB_DEBUG /* compile debugging support (activated by tc tool) */
  72. #define HTB_RATECM 1 /* whether to use rate computer */
  73. #define HTB_HYSTERESIS 1/* whether to use mode hysteresis for speedup */
  74. #define HTB_QLOCK(S) spin_lock_bh(&(S)->dev->queue_lock)
  75. #define HTB_QUNLOCK(S) spin_unlock_bh(&(S)->dev->queue_lock)
  76. #define HTB_VER 0x30011 /* major must be matched with number suplied by TC as version */
  77. #if HTB_VER >> 16 != TC_HTB_PROTOVER
  78. #error "Mismatched sch_htb.c and pkt_sch.h"
  79. #endif
  80. /* debugging support; S is subsystem, these are defined:
  81. 0 - netlink messages
  82. 1 - enqueue
  83. 2 - drop & requeue
  84. 3 - dequeue main
  85. 4 - dequeue one prio DRR part
  86. 5 - dequeue class accounting
  87. 6 - class overlimit status computation
  88. 7 - hint tree
  89. 8 - event queue
  90. 10 - rate estimator
  91. 11 - classifier
  92. 12 - fast dequeue cache
  93. L is level; 0 = none, 1 = basic info, 2 = detailed, 3 = full
  94. q->debug uint32 contains 16 2-bit fields one for subsystem starting
  95. from LSB
  96. */
  97. #ifdef HTB_DEBUG
  98. #define HTB_DBG_COND(S,L) (((q->debug>>(2*S))&3) >= L)
  99. #define HTB_DBG(S,L,FMT,ARG...) if (HTB_DBG_COND(S,L)) \
  100. printk(KERN_DEBUG FMT,##ARG)
  101. #define HTB_CHCL(cl) BUG_TRAP((cl)->magic == HTB_CMAGIC)
  102. #define HTB_PASSQ q,
  103. #define HTB_ARGQ struct htb_sched *q,
  104. #define static
  105. #undef __inline__
  106. #define __inline__
  107. #undef inline
  108. #define inline
  109. #define HTB_CMAGIC 0xFEFAFEF1
  110. #define htb_safe_rb_erase(N,R) do { BUG_TRAP((N)->rb_color != -1); \
  111. if ((N)->rb_color == -1) break; \
  112. rb_erase(N,R); \
  113. (N)->rb_color = -1; } while (0)
  114. #else
  115. #define HTB_DBG_COND(S,L) (0)
  116. #define HTB_DBG(S,L,FMT,ARG...)
  117. #define HTB_PASSQ
  118. #define HTB_ARGQ
  119. #define HTB_CHCL(cl)
  120. #define htb_safe_rb_erase(N,R) rb_erase(N,R)
  121. #endif
  122. /* used internaly to keep status of single class */
  123. enum htb_cmode {
  124. HTB_CANT_SEND, /* class can't send and can't borrow */
  125. HTB_MAY_BORROW, /* class can't send but may borrow */
  126. HTB_CAN_SEND /* class can send */
  127. };
  128. /* interior & leaf nodes; props specific to leaves are marked L: */
  129. struct htb_class
  130. {
  131. #ifdef HTB_DEBUG
  132. unsigned magic;
  133. #endif
  134. /* general class parameters */
  135. u32 classid;
  136. struct gnet_stats_basic bstats;
  137. struct gnet_stats_queue qstats;
  138. struct gnet_stats_rate_est rate_est;
  139. struct tc_htb_xstats xstats;/* our special stats */
  140. int refcnt; /* usage count of this class */
  141. #ifdef HTB_RATECM
  142. /* rate measurement counters */
  143. unsigned long rate_bytes,sum_bytes;
  144. unsigned long rate_packets,sum_packets;
  145. #endif
  146. /* topology */
  147. int level; /* our level (see above) */
  148. struct htb_class *parent; /* parent class */
  149. struct list_head hlist; /* classid hash list item */
  150. struct list_head sibling; /* sibling list item */
  151. struct list_head children; /* children list */
  152. union {
  153. struct htb_class_leaf {
  154. struct Qdisc *q;
  155. int prio;
  156. int aprio;
  157. int quantum;
  158. int deficit[TC_HTB_MAXDEPTH];
  159. struct list_head drop_list;
  160. } leaf;
  161. struct htb_class_inner {
  162. struct rb_root feed[TC_HTB_NUMPRIO]; /* feed trees */
  163. struct rb_node *ptr[TC_HTB_NUMPRIO]; /* current class ptr */
  164. /* When class changes from state 1->2 and disconnects from
  165. parent's feed then we lost ptr value and start from the
  166. first child again. Here we store classid of the
  167. last valid ptr (used when ptr is NULL). */
  168. u32 last_ptr_id[TC_HTB_NUMPRIO];
  169. } inner;
  170. } un;
  171. struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
  172. struct rb_node pq_node; /* node for event queue */
  173. unsigned long pq_key; /* the same type as jiffies global */
  174. int prio_activity; /* for which prios are we active */
  175. enum htb_cmode cmode; /* current mode of the class */
  176. /* class attached filters */
  177. struct tcf_proto *filter_list;
  178. int filter_cnt;
  179. int warned; /* only one warning about non work conserving .. */
  180. /* token bucket parameters */
  181. struct qdisc_rate_table *rate; /* rate table of the class itself */
  182. struct qdisc_rate_table *ceil; /* ceiling rate (limits borrows too) */
  183. long buffer,cbuffer; /* token bucket depth/rate */
  184. long mbuffer; /* max wait time */
  185. long tokens,ctokens; /* current number of tokens */
  186. psched_time_t t_c; /* checkpoint time */
  187. };
  188. /* TODO: maybe compute rate when size is too large .. or drop ? */
  189. static __inline__ long L2T(struct htb_class *cl,struct qdisc_rate_table *rate,
  190. int size)
  191. {
  192. int slot = size >> rate->rate.cell_log;
  193. if (slot > 255) {
  194. cl->xstats.giants++;
  195. slot = 255;
  196. }
  197. return rate->data[slot];
  198. }
  199. struct htb_sched
  200. {
  201. struct list_head root; /* root classes list */
  202. struct list_head hash[HTB_HSIZE]; /* hashed by classid */
  203. struct list_head drops[TC_HTB_NUMPRIO]; /* active leaves (for drops) */
  204. /* self list - roots of self generating tree */
  205. struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
  206. int row_mask[TC_HTB_MAXDEPTH];
  207. struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
  208. u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
  209. /* self wait list - roots of wait PQs per row */
  210. struct rb_root wait_pq[TC_HTB_MAXDEPTH];
  211. /* time of nearest event per level (row) */
  212. unsigned long near_ev_cache[TC_HTB_MAXDEPTH];
  213. /* cached value of jiffies in dequeue */
  214. unsigned long jiffies;
  215. /* whether we hit non-work conserving class during this dequeue; we use */
  216. int nwc_hit; /* this to disable mindelay complaint in dequeue */
  217. int defcls; /* class where unclassified flows go to */
  218. u32 debug; /* subsystem debug levels */
  219. /* filters for qdisc itself */
  220. struct tcf_proto *filter_list;
  221. int filter_cnt;
  222. int rate2quantum; /* quant = rate / rate2quantum */
  223. psched_time_t now; /* cached dequeue time */
  224. struct timer_list timer; /* send delay timer */
  225. #ifdef HTB_RATECM
  226. struct timer_list rttim; /* rate computer timer */
  227. int recmp_bucket; /* which hash bucket to recompute next */
  228. #endif
  229. /* non shaped skbs; let them go directly thru */
  230. struct sk_buff_head direct_queue;
  231. int direct_qlen; /* max qlen of above */
  232. long direct_pkts;
  233. };
  234. /* compute hash of size HTB_HSIZE for given handle */
  235. static __inline__ int htb_hash(u32 h)
  236. {
  237. #if HTB_HSIZE != 16
  238. #error "Declare new hash for your HTB_HSIZE"
  239. #endif
  240. h ^= h>>8; /* stolen from cbq_hash */
  241. h ^= h>>4;
  242. return h & 0xf;
  243. }
  244. /* find class in global hash table using given handle */
  245. static __inline__ struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
  246. {
  247. struct htb_sched *q = qdisc_priv(sch);
  248. struct list_head *p;
  249. if (TC_H_MAJ(handle) != sch->handle)
  250. return NULL;
  251. list_for_each (p,q->hash+htb_hash(handle)) {
  252. struct htb_class *cl = list_entry(p,struct htb_class,hlist);
  253. if (cl->classid == handle)
  254. return cl;
  255. }
  256. return NULL;
  257. }
  258. /**
  259. * htb_classify - classify a packet into class
  260. *
  261. * It returns NULL if the packet should be dropped or -1 if the packet
  262. * should be passed directly thru. In all other cases leaf class is returned.
  263. * We allow direct class selection by classid in priority. The we examine
  264. * filters in qdisc and in inner nodes (if higher filter points to the inner
  265. * node). If we end up with classid MAJOR:0 we enqueue the skb into special
  266. * internal fifo (direct). These packets then go directly thru. If we still
  267. * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessfull
  268. * then finish and return direct queue.
  269. */
  270. #define HTB_DIRECT (struct htb_class*)-1
  271. static inline u32 htb_classid(struct htb_class *cl)
  272. {
  273. return (cl && cl != HTB_DIRECT) ? cl->classid : TC_H_UNSPEC;
  274. }
  275. static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  276. {
  277. struct htb_sched *q = qdisc_priv(sch);
  278. struct htb_class *cl;
  279. struct tcf_result res;
  280. struct tcf_proto *tcf;
  281. int result;
  282. /* allow to select class by setting skb->priority to valid classid;
  283. note that nfmark can be used too by attaching filter fw with no
  284. rules in it */
  285. if (skb->priority == sch->handle)
  286. return HTB_DIRECT; /* X:0 (direct flow) selected */
  287. if ((cl = htb_find(skb->priority,sch)) != NULL && cl->level == 0)
  288. return cl;
  289. *qerr = NET_XMIT_BYPASS;
  290. tcf = q->filter_list;
  291. while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
  292. #ifdef CONFIG_NET_CLS_ACT
  293. switch (result) {
  294. case TC_ACT_QUEUED:
  295. case TC_ACT_STOLEN:
  296. *qerr = NET_XMIT_SUCCESS;
  297. case TC_ACT_SHOT:
  298. return NULL;
  299. }
  300. #elif defined(CONFIG_NET_CLS_POLICE)
  301. if (result == TC_POLICE_SHOT)
  302. return HTB_DIRECT;
  303. #endif
  304. if ((cl = (void*)res.class) == NULL) {
  305. if (res.classid == sch->handle)
  306. return HTB_DIRECT; /* X:0 (direct flow) */
  307. if ((cl = htb_find(res.classid,sch)) == NULL)
  308. break; /* filter selected invalid classid */
  309. }
  310. if (!cl->level)
  311. return cl; /* we hit leaf; return it */
  312. /* we have got inner class; apply inner filter chain */
  313. tcf = cl->filter_list;
  314. }
  315. /* classification failed; try to use default class */
  316. cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle),q->defcls),sch);
  317. if (!cl || cl->level)
  318. return HTB_DIRECT; /* bad default .. this is safe bet */
  319. return cl;
  320. }
  321. #ifdef HTB_DEBUG
  322. static void htb_next_rb_node(struct rb_node **n);
  323. #define HTB_DUMTREE(root,memb) if(root) { \
  324. struct rb_node *n = (root)->rb_node; \
  325. while (n->rb_left) n = n->rb_left; \
  326. while (n) { \
  327. struct htb_class *cl = rb_entry(n, struct htb_class, memb); \
  328. printk(" %x",cl->classid); htb_next_rb_node (&n); \
  329. } }
  330. static void htb_debug_dump (struct htb_sched *q)
  331. {
  332. int i,p;
  333. printk(KERN_DEBUG "htb*g j=%lu lj=%lu\n",jiffies,q->jiffies);
  334. /* rows */
  335. for (i=TC_HTB_MAXDEPTH-1;i>=0;i--) {
  336. printk(KERN_DEBUG "htb*r%d m=%x",i,q->row_mask[i]);
  337. for (p=0;p<TC_HTB_NUMPRIO;p++) {
  338. if (!q->row[i][p].rb_node) continue;
  339. printk(" p%d:",p);
  340. HTB_DUMTREE(q->row[i]+p,node[p]);
  341. }
  342. printk("\n");
  343. }
  344. /* classes */
  345. for (i = 0; i < HTB_HSIZE; i++) {
  346. struct list_head *l;
  347. list_for_each (l,q->hash+i) {
  348. struct htb_class *cl = list_entry(l,struct htb_class,hlist);
  349. long diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
  350. printk(KERN_DEBUG "htb*c%x m=%d t=%ld c=%ld pq=%lu df=%ld ql=%d "
  351. "pa=%x f:",
  352. cl->classid,cl->cmode,cl->tokens,cl->ctokens,
  353. cl->pq_node.rb_color==-1?0:cl->pq_key,diff,
  354. cl->level?0:cl->un.leaf.q->q.qlen,cl->prio_activity);
  355. if (cl->level)
  356. for (p=0;p<TC_HTB_NUMPRIO;p++) {
  357. if (!cl->un.inner.feed[p].rb_node) continue;
  358. printk(" p%d a=%x:",p,cl->un.inner.ptr[p]?rb_entry(cl->un.inner.ptr[p], struct htb_class,node[p])->classid:0);
  359. HTB_DUMTREE(cl->un.inner.feed+p,node[p]);
  360. }
  361. printk("\n");
  362. }
  363. }
  364. }
  365. #endif
  366. /**
  367. * htb_add_to_id_tree - adds class to the round robin list
  368. *
  369. * Routine adds class to the list (actually tree) sorted by classid.
  370. * Make sure that class is not already on such list for given prio.
  371. */
  372. static void htb_add_to_id_tree (HTB_ARGQ struct rb_root *root,
  373. struct htb_class *cl,int prio)
  374. {
  375. struct rb_node **p = &root->rb_node, *parent = NULL;
  376. HTB_DBG(7,3,"htb_add_id_tree cl=%X prio=%d\n",cl->classid,prio);
  377. #ifdef HTB_DEBUG
  378. if (cl->node[prio].rb_color != -1) { BUG_TRAP(0); return; }
  379. HTB_CHCL(cl);
  380. if (*p) {
  381. struct htb_class *x = rb_entry(*p,struct htb_class,node[prio]);
  382. HTB_CHCL(x);
  383. }
  384. #endif
  385. while (*p) {
  386. struct htb_class *c; parent = *p;
  387. c = rb_entry(parent, struct htb_class, node[prio]);
  388. HTB_CHCL(c);
  389. if (cl->classid > c->classid)
  390. p = &parent->rb_right;
  391. else
  392. p = &parent->rb_left;
  393. }
  394. rb_link_node(&cl->node[prio], parent, p);
  395. rb_insert_color(&cl->node[prio], root);
  396. }
  397. /**
  398. * htb_add_to_wait_tree - adds class to the event queue with delay
  399. *
  400. * The class is added to priority event queue to indicate that class will
  401. * change its mode in cl->pq_key microseconds. Make sure that class is not
  402. * already in the queue.
  403. */
  404. static void htb_add_to_wait_tree (struct htb_sched *q,
  405. struct htb_class *cl,long delay,int debug_hint)
  406. {
  407. struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
  408. HTB_DBG(7,3,"htb_add_wt cl=%X key=%lu\n",cl->classid,cl->pq_key);
  409. #ifdef HTB_DEBUG
  410. if (cl->pq_node.rb_color != -1) { BUG_TRAP(0); return; }
  411. HTB_CHCL(cl);
  412. if ((delay <= 0 || delay > cl->mbuffer) && net_ratelimit())
  413. printk(KERN_ERR "HTB: suspicious delay in wait_tree d=%ld cl=%X h=%d\n",delay,cl->classid,debug_hint);
  414. #endif
  415. cl->pq_key = q->jiffies + PSCHED_US2JIFFIE(delay);
  416. if (cl->pq_key == q->jiffies)
  417. cl->pq_key++;
  418. /* update the nearest event cache */
  419. if (time_after(q->near_ev_cache[cl->level], cl->pq_key))
  420. q->near_ev_cache[cl->level] = cl->pq_key;
  421. while (*p) {
  422. struct htb_class *c; parent = *p;
  423. c = rb_entry(parent, struct htb_class, pq_node);
  424. if (time_after_eq(cl->pq_key, c->pq_key))
  425. p = &parent->rb_right;
  426. else
  427. p = &parent->rb_left;
  428. }
  429. rb_link_node(&cl->pq_node, parent, p);
  430. rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
  431. }
  432. /**
  433. * htb_next_rb_node - finds next node in binary tree
  434. *
  435. * When we are past last key we return NULL.
  436. * Average complexity is 2 steps per call.
  437. */
  438. static void htb_next_rb_node(struct rb_node **n)
  439. {
  440. *n = rb_next(*n);
  441. }
  442. /**
  443. * htb_add_class_to_row - add class to its row
  444. *
  445. * The class is added to row at priorities marked in mask.
  446. * It does nothing if mask == 0.
  447. */
  448. static inline void htb_add_class_to_row(struct htb_sched *q,
  449. struct htb_class *cl,int mask)
  450. {
  451. HTB_DBG(7,2,"htb_addrow cl=%X mask=%X rmask=%X\n",
  452. cl->classid,mask,q->row_mask[cl->level]);
  453. HTB_CHCL(cl);
  454. q->row_mask[cl->level] |= mask;
  455. while (mask) {
  456. int prio = ffz(~mask);
  457. mask &= ~(1 << prio);
  458. htb_add_to_id_tree(HTB_PASSQ q->row[cl->level]+prio,cl,prio);
  459. }
  460. }
  461. /**
  462. * htb_remove_class_from_row - removes class from its row
  463. *
  464. * The class is removed from row at priorities marked in mask.
  465. * It does nothing if mask == 0.
  466. */
  467. static __inline__ void htb_remove_class_from_row(struct htb_sched *q,
  468. struct htb_class *cl,int mask)
  469. {
  470. int m = 0;
  471. HTB_CHCL(cl);
  472. while (mask) {
  473. int prio = ffz(~mask);
  474. mask &= ~(1 << prio);
  475. if (q->ptr[cl->level][prio] == cl->node+prio)
  476. htb_next_rb_node(q->ptr[cl->level]+prio);
  477. htb_safe_rb_erase(cl->node + prio,q->row[cl->level]+prio);
  478. if (!q->row[cl->level][prio].rb_node)
  479. m |= 1 << prio;
  480. }
  481. HTB_DBG(7,2,"htb_delrow cl=%X mask=%X rmask=%X maskdel=%X\n",
  482. cl->classid,mask,q->row_mask[cl->level],m);
  483. q->row_mask[cl->level] &= ~m;
  484. }
  485. /**
  486. * htb_activate_prios - creates active classe's feed chain
  487. *
  488. * The class is connected to ancestors and/or appropriate rows
  489. * for priorities it is participating on. cl->cmode must be new
  490. * (activated) mode. It does nothing if cl->prio_activity == 0.
  491. */
  492. static void htb_activate_prios(struct htb_sched *q,struct htb_class *cl)
  493. {
  494. struct htb_class *p = cl->parent;
  495. long m,mask = cl->prio_activity;
  496. HTB_DBG(7,2,"htb_act_prios cl=%X mask=%lX cmode=%d\n",cl->classid,mask,cl->cmode);
  497. HTB_CHCL(cl);
  498. while (cl->cmode == HTB_MAY_BORROW && p && mask) {
  499. HTB_CHCL(p);
  500. m = mask; while (m) {
  501. int prio = ffz(~m);
  502. m &= ~(1 << prio);
  503. if (p->un.inner.feed[prio].rb_node)
  504. /* parent already has its feed in use so that
  505. reset bit in mask as parent is already ok */
  506. mask &= ~(1 << prio);
  507. htb_add_to_id_tree(HTB_PASSQ p->un.inner.feed+prio,cl,prio);
  508. }
  509. HTB_DBG(7,3,"htb_act_pr_aft p=%X pact=%X mask=%lX pmode=%d\n",
  510. p->classid,p->prio_activity,mask,p->cmode);
  511. p->prio_activity |= mask;
  512. cl = p; p = cl->parent;
  513. HTB_CHCL(cl);
  514. }
  515. if (cl->cmode == HTB_CAN_SEND && mask)
  516. htb_add_class_to_row(q,cl,mask);
  517. }
  518. /**
  519. * htb_deactivate_prios - remove class from feed chain
  520. *
  521. * cl->cmode must represent old mode (before deactivation). It does
  522. * nothing if cl->prio_activity == 0. Class is removed from all feed
  523. * chains and rows.
  524. */
  525. static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
  526. {
  527. struct htb_class *p = cl->parent;
  528. long m,mask = cl->prio_activity;
  529. HTB_DBG(7,2,"htb_deact_prios cl=%X mask=%lX cmode=%d\n",cl->classid,mask,cl->cmode);
  530. HTB_CHCL(cl);
  531. while (cl->cmode == HTB_MAY_BORROW && p && mask) {
  532. m = mask; mask = 0;
  533. while (m) {
  534. int prio = ffz(~m);
  535. m &= ~(1 << prio);
  536. if (p->un.inner.ptr[prio] == cl->node+prio) {
  537. /* we are removing child which is pointed to from
  538. parent feed - forget the pointer but remember
  539. classid */
  540. p->un.inner.last_ptr_id[prio] = cl->classid;
  541. p->un.inner.ptr[prio] = NULL;
  542. }
  543. htb_safe_rb_erase(cl->node + prio,p->un.inner.feed + prio);
  544. if (!p->un.inner.feed[prio].rb_node)
  545. mask |= 1 << prio;
  546. }
  547. HTB_DBG(7,3,"htb_deact_pr_aft p=%X pact=%X mask=%lX pmode=%d\n",
  548. p->classid,p->prio_activity,mask,p->cmode);
  549. p->prio_activity &= ~mask;
  550. cl = p; p = cl->parent;
  551. HTB_CHCL(cl);
  552. }
  553. if (cl->cmode == HTB_CAN_SEND && mask)
  554. htb_remove_class_from_row(q,cl,mask);
  555. }
  556. /**
  557. * htb_class_mode - computes and returns current class mode
  558. *
  559. * It computes cl's mode at time cl->t_c+diff and returns it. If mode
  560. * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
  561. * from now to time when cl will change its state.
  562. * Also it is worth to note that class mode doesn't change simply
  563. * at cl->{c,}tokens == 0 but there can rather be hysteresis of
  564. * 0 .. -cl->{c,}buffer range. It is meant to limit number of
  565. * mode transitions per time unit. The speed gain is about 1/6.
  566. */
  567. static __inline__ enum htb_cmode
  568. htb_class_mode(struct htb_class *cl,long *diff)
  569. {
  570. long toks;
  571. if ((toks = (cl->ctokens + *diff)) < (
  572. #if HTB_HYSTERESIS
  573. cl->cmode != HTB_CANT_SEND ? -cl->cbuffer :
  574. #endif
  575. 0)) {
  576. *diff = -toks;
  577. return HTB_CANT_SEND;
  578. }
  579. if ((toks = (cl->tokens + *diff)) >= (
  580. #if HTB_HYSTERESIS
  581. cl->cmode == HTB_CAN_SEND ? -cl->buffer :
  582. #endif
  583. 0))
  584. return HTB_CAN_SEND;
  585. *diff = -toks;
  586. return HTB_MAY_BORROW;
  587. }
  588. /**
  589. * htb_change_class_mode - changes classe's mode
  590. *
  591. * This should be the only way how to change classe's mode under normal
  592. * cirsumstances. Routine will update feed lists linkage, change mode
  593. * and add class to the wait event queue if appropriate. New mode should
  594. * be different from old one and cl->pq_key has to be valid if changing
  595. * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
  596. */
  597. static void
  598. htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff)
  599. {
  600. enum htb_cmode new_mode = htb_class_mode(cl,diff);
  601. HTB_CHCL(cl);
  602. HTB_DBG(7,1,"htb_chging_clmode %d->%d cl=%X\n",cl->cmode,new_mode,cl->classid);
  603. if (new_mode == cl->cmode)
  604. return;
  605. if (cl->prio_activity) { /* not necessary: speed optimization */
  606. if (cl->cmode != HTB_CANT_SEND)
  607. htb_deactivate_prios(q,cl);
  608. cl->cmode = new_mode;
  609. if (new_mode != HTB_CANT_SEND)
  610. htb_activate_prios(q,cl);
  611. } else
  612. cl->cmode = new_mode;
  613. }
  614. /**
  615. * htb_activate - inserts leaf cl into appropriate active feeds
  616. *
  617. * Routine learns (new) priority of leaf and activates feed chain
  618. * for the prio. It can be called on already active leaf safely.
  619. * It also adds leaf into droplist.
  620. */
  621. static __inline__ void htb_activate(struct htb_sched *q,struct htb_class *cl)
  622. {
  623. BUG_TRAP(!cl->level && cl->un.leaf.q && cl->un.leaf.q->q.qlen);
  624. HTB_CHCL(cl);
  625. if (!cl->prio_activity) {
  626. cl->prio_activity = 1 << (cl->un.leaf.aprio = cl->un.leaf.prio);
  627. htb_activate_prios(q,cl);
  628. list_add_tail(&cl->un.leaf.drop_list,q->drops+cl->un.leaf.aprio);
  629. }
  630. }
  631. /**
  632. * htb_deactivate - remove leaf cl from active feeds
  633. *
  634. * Make sure that leaf is active. In the other words it can't be called
  635. * with non-active leaf. It also removes class from the drop list.
  636. */
  637. static __inline__ void
  638. htb_deactivate(struct htb_sched *q,struct htb_class *cl)
  639. {
  640. BUG_TRAP(cl->prio_activity);
  641. HTB_CHCL(cl);
  642. htb_deactivate_prios(q,cl);
  643. cl->prio_activity = 0;
  644. list_del_init(&cl->un.leaf.drop_list);
  645. }
  646. static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  647. {
  648. int ret;
  649. struct htb_sched *q = qdisc_priv(sch);
  650. struct htb_class *cl = htb_classify(skb,sch,&ret);
  651. if (cl == HTB_DIRECT) {
  652. /* enqueue to helper queue */
  653. if (q->direct_queue.qlen < q->direct_qlen) {
  654. __skb_queue_tail(&q->direct_queue, skb);
  655. q->direct_pkts++;
  656. } else {
  657. kfree_skb(skb);
  658. sch->qstats.drops++;
  659. return NET_XMIT_DROP;
  660. }
  661. #ifdef CONFIG_NET_CLS_ACT
  662. } else if (!cl) {
  663. if (ret == NET_XMIT_BYPASS)
  664. sch->qstats.drops++;
  665. kfree_skb (skb);
  666. return ret;
  667. #endif
  668. } else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
  669. sch->qstats.drops++;
  670. cl->qstats.drops++;
  671. return NET_XMIT_DROP;
  672. } else {
  673. cl->bstats.packets++; cl->bstats.bytes += skb->len;
  674. htb_activate (q,cl);
  675. }
  676. sch->q.qlen++;
  677. sch->bstats.packets++; sch->bstats.bytes += skb->len;
  678. HTB_DBG(1,1,"htb_enq_ok cl=%X skb=%p\n",(cl && cl != HTB_DIRECT)?cl->classid:0,skb);
  679. return NET_XMIT_SUCCESS;
  680. }
  681. /* TODO: requeuing packet charges it to policers again !! */
  682. static int htb_requeue(struct sk_buff *skb, struct Qdisc *sch)
  683. {
  684. struct htb_sched *q = qdisc_priv(sch);
  685. int ret = NET_XMIT_SUCCESS;
  686. struct htb_class *cl = htb_classify(skb,sch, &ret);
  687. struct sk_buff *tskb;
  688. if (cl == HTB_DIRECT || !cl) {
  689. /* enqueue to helper queue */
  690. if (q->direct_queue.qlen < q->direct_qlen && cl) {
  691. __skb_queue_head(&q->direct_queue, skb);
  692. } else {
  693. __skb_queue_head(&q->direct_queue, skb);
  694. tskb = __skb_dequeue_tail(&q->direct_queue);
  695. kfree_skb (tskb);
  696. sch->qstats.drops++;
  697. return NET_XMIT_CN;
  698. }
  699. } else if (cl->un.leaf.q->ops->requeue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
  700. sch->qstats.drops++;
  701. cl->qstats.drops++;
  702. return NET_XMIT_DROP;
  703. } else
  704. htb_activate (q,cl);
  705. sch->q.qlen++;
  706. sch->qstats.requeues++;
  707. HTB_DBG(1,1,"htb_req_ok cl=%X skb=%p\n",(cl && cl != HTB_DIRECT)?cl->classid:0,skb);
  708. return NET_XMIT_SUCCESS;
  709. }
  710. static void htb_timer(unsigned long arg)
  711. {
  712. struct Qdisc *sch = (struct Qdisc*)arg;
  713. sch->flags &= ~TCQ_F_THROTTLED;
  714. wmb();
  715. netif_schedule(sch->dev);
  716. }
  717. #ifdef HTB_RATECM
  718. #define RT_GEN(D,R) R+=D-(R/HTB_EWMAC);D=0
  719. static void htb_rate_timer(unsigned long arg)
  720. {
  721. struct Qdisc *sch = (struct Qdisc*)arg;
  722. struct htb_sched *q = qdisc_priv(sch);
  723. struct list_head *p;
  724. /* lock queue so that we can muck with it */
  725. HTB_QLOCK(sch);
  726. HTB_DBG(10,1,"htb_rttmr j=%ld\n",jiffies);
  727. q->rttim.expires = jiffies + HZ;
  728. add_timer(&q->rttim);
  729. /* scan and recompute one bucket at time */
  730. if (++q->recmp_bucket >= HTB_HSIZE)
  731. q->recmp_bucket = 0;
  732. list_for_each (p,q->hash+q->recmp_bucket) {
  733. struct htb_class *cl = list_entry(p,struct htb_class,hlist);
  734. HTB_DBG(10,2,"htb_rttmr_cl cl=%X sbyte=%lu spkt=%lu\n",
  735. cl->classid,cl->sum_bytes,cl->sum_packets);
  736. RT_GEN (cl->sum_bytes,cl->rate_bytes);
  737. RT_GEN (cl->sum_packets,cl->rate_packets);
  738. }
  739. HTB_QUNLOCK(sch);
  740. }
  741. #endif
  742. /**
  743. * htb_charge_class - charges amount "bytes" to leaf and ancestors
  744. *
  745. * Routine assumes that packet "bytes" long was dequeued from leaf cl
  746. * borrowing from "level". It accounts bytes to ceil leaky bucket for
  747. * leaf and all ancestors and to rate bucket for ancestors at levels
  748. * "level" and higher. It also handles possible change of mode resulting
  749. * from the update. Note that mode can also increase here (MAY_BORROW to
  750. * CAN_SEND) because we can use more precise clock that event queue here.
  751. * In such case we remove class from event queue first.
  752. */
  753. static void htb_charge_class(struct htb_sched *q,struct htb_class *cl,
  754. int level,int bytes)
  755. {
  756. long toks,diff;
  757. enum htb_cmode old_mode;
  758. HTB_DBG(5,1,"htb_chrg_cl cl=%X lev=%d len=%d\n",cl->classid,level,bytes);
  759. #define HTB_ACCNT(T,B,R) toks = diff + cl->T; \
  760. if (toks > cl->B) toks = cl->B; \
  761. toks -= L2T(cl, cl->R, bytes); \
  762. if (toks <= -cl->mbuffer) toks = 1-cl->mbuffer; \
  763. cl->T = toks
  764. while (cl) {
  765. HTB_CHCL(cl);
  766. diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
  767. #ifdef HTB_DEBUG
  768. if (diff > cl->mbuffer || diff < 0 || PSCHED_TLESS(q->now, cl->t_c)) {
  769. if (net_ratelimit())
  770. printk(KERN_ERR "HTB: bad diff in charge, cl=%X diff=%lX now=%Lu then=%Lu j=%lu\n",
  771. cl->classid, diff,
  772. #ifdef CONFIG_NET_SCH_CLK_GETTIMEOFDAY
  773. q->now.tv_sec * 1000000ULL + q->now.tv_usec,
  774. cl->t_c.tv_sec * 1000000ULL + cl->t_c.tv_usec,
  775. #else
  776. (unsigned long long) q->now,
  777. (unsigned long long) cl->t_c,
  778. #endif
  779. q->jiffies);
  780. diff = 1000;
  781. }
  782. #endif
  783. if (cl->level >= level) {
  784. if (cl->level == level) cl->xstats.lends++;
  785. HTB_ACCNT (tokens,buffer,rate);
  786. } else {
  787. cl->xstats.borrows++;
  788. cl->tokens += diff; /* we moved t_c; update tokens */
  789. }
  790. HTB_ACCNT (ctokens,cbuffer,ceil);
  791. cl->t_c = q->now;
  792. HTB_DBG(5,2,"htb_chrg_clp cl=%X diff=%ld tok=%ld ctok=%ld\n",cl->classid,diff,cl->tokens,cl->ctokens);
  793. old_mode = cl->cmode; diff = 0;
  794. htb_change_class_mode(q,cl,&diff);
  795. if (old_mode != cl->cmode) {
  796. if (old_mode != HTB_CAN_SEND)
  797. htb_safe_rb_erase(&cl->pq_node,q->wait_pq+cl->level);
  798. if (cl->cmode != HTB_CAN_SEND)
  799. htb_add_to_wait_tree (q,cl,diff,1);
  800. }
  801. #ifdef HTB_RATECM
  802. /* update rate counters */
  803. cl->sum_bytes += bytes; cl->sum_packets++;
  804. #endif
  805. /* update byte stats except for leaves which are already updated */
  806. if (cl->level) {
  807. cl->bstats.bytes += bytes;
  808. cl->bstats.packets++;
  809. }
  810. cl = cl->parent;
  811. }
  812. }
  813. /**
  814. * htb_do_events - make mode changes to classes at the level
  815. *
  816. * Scans event queue for pending events and applies them. Returns jiffies to
  817. * next pending event (0 for no event in pq).
  818. * Note: Aplied are events whose have cl->pq_key <= jiffies.
  819. */
  820. static long htb_do_events(struct htb_sched *q,int level)
  821. {
  822. int i;
  823. HTB_DBG(8,1,"htb_do_events l=%d root=%p rmask=%X\n",
  824. level,q->wait_pq[level].rb_node,q->row_mask[level]);
  825. for (i = 0; i < 500; i++) {
  826. struct htb_class *cl;
  827. long diff;
  828. struct rb_node *p = q->wait_pq[level].rb_node;
  829. if (!p) return 0;
  830. while (p->rb_left) p = p->rb_left;
  831. cl = rb_entry(p, struct htb_class, pq_node);
  832. if (time_after(cl->pq_key, q->jiffies)) {
  833. HTB_DBG(8,3,"htb_do_ev_ret delay=%ld\n",cl->pq_key - q->jiffies);
  834. return cl->pq_key - q->jiffies;
  835. }
  836. htb_safe_rb_erase(p,q->wait_pq+level);
  837. diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer);
  838. #ifdef HTB_DEBUG
  839. if (diff > cl->mbuffer || diff < 0 || PSCHED_TLESS(q->now, cl->t_c)) {
  840. if (net_ratelimit())
  841. printk(KERN_ERR "HTB: bad diff in events, cl=%X diff=%lX now=%Lu then=%Lu j=%lu\n",
  842. cl->classid, diff,
  843. #ifdef CONFIG_NET_SCH_CLK_GETTIMEOFDAY
  844. q->now.tv_sec * 1000000ULL + q->now.tv_usec,
  845. cl->t_c.tv_sec * 1000000ULL + cl->t_c.tv_usec,
  846. #else
  847. (unsigned long long) q->now,
  848. (unsigned long long) cl->t_c,
  849. #endif
  850. q->jiffies);
  851. diff = 1000;
  852. }
  853. #endif
  854. htb_change_class_mode(q,cl,&diff);
  855. if (cl->cmode != HTB_CAN_SEND)
  856. htb_add_to_wait_tree (q,cl,diff,2);
  857. }
  858. if (net_ratelimit())
  859. printk(KERN_WARNING "htb: too many events !\n");
  860. return HZ/10;
  861. }
  862. /* Returns class->node+prio from id-tree where classe's id is >= id. NULL
  863. is no such one exists. */
  864. static struct rb_node *
  865. htb_id_find_next_upper(int prio,struct rb_node *n,u32 id)
  866. {
  867. struct rb_node *r = NULL;
  868. while (n) {
  869. struct htb_class *cl = rb_entry(n,struct htb_class,node[prio]);
  870. if (id == cl->classid) return n;
  871. if (id > cl->classid) {
  872. n = n->rb_right;
  873. } else {
  874. r = n;
  875. n = n->rb_left;
  876. }
  877. }
  878. return r;
  879. }
  880. /**
  881. * htb_lookup_leaf - returns next leaf class in DRR order
  882. *
  883. * Find leaf where current feed pointers points to.
  884. */
  885. static struct htb_class *
  886. htb_lookup_leaf(HTB_ARGQ struct rb_root *tree,int prio,struct rb_node **pptr,u32 *pid)
  887. {
  888. int i;
  889. struct {
  890. struct rb_node *root;
  891. struct rb_node **pptr;
  892. u32 *pid;
  893. } stk[TC_HTB_MAXDEPTH],*sp = stk;
  894. BUG_TRAP(tree->rb_node);
  895. sp->root = tree->rb_node;
  896. sp->pptr = pptr;
  897. sp->pid = pid;
  898. for (i = 0; i < 65535; i++) {
  899. HTB_DBG(4,2,"htb_lleaf ptr=%p pid=%X\n",*sp->pptr,*sp->pid);
  900. if (!*sp->pptr && *sp->pid) {
  901. /* ptr was invalidated but id is valid - try to recover
  902. the original or next ptr */
  903. *sp->pptr = htb_id_find_next_upper(prio,sp->root,*sp->pid);
  904. }
  905. *sp->pid = 0; /* ptr is valid now so that remove this hint as it
  906. can become out of date quickly */
  907. if (!*sp->pptr) { /* we are at right end; rewind & go up */
  908. *sp->pptr = sp->root;
  909. while ((*sp->pptr)->rb_left)
  910. *sp->pptr = (*sp->pptr)->rb_left;
  911. if (sp > stk) {
  912. sp--;
  913. BUG_TRAP(*sp->pptr); if(!*sp->pptr) return NULL;
  914. htb_next_rb_node (sp->pptr);
  915. }
  916. } else {
  917. struct htb_class *cl;
  918. cl = rb_entry(*sp->pptr,struct htb_class,node[prio]);
  919. HTB_CHCL(cl);
  920. if (!cl->level)
  921. return cl;
  922. (++sp)->root = cl->un.inner.feed[prio].rb_node;
  923. sp->pptr = cl->un.inner.ptr+prio;
  924. sp->pid = cl->un.inner.last_ptr_id+prio;
  925. }
  926. }
  927. BUG_TRAP(0);
  928. return NULL;
  929. }
  930. /* dequeues packet at given priority and level; call only if
  931. you are sure that there is active class at prio/level */
  932. static struct sk_buff *
  933. htb_dequeue_tree(struct htb_sched *q,int prio,int level)
  934. {
  935. struct sk_buff *skb = NULL;
  936. struct htb_class *cl,*start;
  937. /* look initial class up in the row */
  938. start = cl = htb_lookup_leaf (HTB_PASSQ q->row[level]+prio,prio,
  939. q->ptr[level]+prio,q->last_ptr_id[level]+prio);
  940. do {
  941. next:
  942. BUG_TRAP(cl);
  943. if (!cl) return NULL;
  944. HTB_DBG(4,1,"htb_deq_tr prio=%d lev=%d cl=%X defic=%d\n",
  945. prio,level,cl->classid,cl->un.leaf.deficit[level]);
  946. /* class can be empty - it is unlikely but can be true if leaf
  947. qdisc drops packets in enqueue routine or if someone used
  948. graft operation on the leaf since last dequeue;
  949. simply deactivate and skip such class */
  950. if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
  951. struct htb_class *next;
  952. htb_deactivate(q,cl);
  953. /* row/level might become empty */
  954. if ((q->row_mask[level] & (1 << prio)) == 0)
  955. return NULL;
  956. next = htb_lookup_leaf (HTB_PASSQ q->row[level]+prio,
  957. prio,q->ptr[level]+prio,q->last_ptr_id[level]+prio);
  958. if (cl == start) /* fix start if we just deleted it */
  959. start = next;
  960. cl = next;
  961. goto next;
  962. }
  963. if (likely((skb = cl->un.leaf.q->dequeue(cl->un.leaf.q)) != NULL))
  964. break;
  965. if (!cl->warned) {
  966. printk(KERN_WARNING "htb: class %X isn't work conserving ?!\n",cl->classid);
  967. cl->warned = 1;
  968. }
  969. q->nwc_hit++;
  970. htb_next_rb_node((level?cl->parent->un.inner.ptr:q->ptr[0])+prio);
  971. cl = htb_lookup_leaf (HTB_PASSQ q->row[level]+prio,prio,q->ptr[level]+prio,
  972. q->last_ptr_id[level]+prio);
  973. } while (cl != start);
  974. if (likely(skb != NULL)) {
  975. if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
  976. HTB_DBG(4,2,"htb_next_cl oldptr=%p quant_add=%d\n",
  977. level?cl->parent->un.inner.ptr[prio]:q->ptr[0][prio],cl->un.leaf.quantum);
  978. cl->un.leaf.deficit[level] += cl->un.leaf.quantum;
  979. htb_next_rb_node((level?cl->parent->un.inner.ptr:q->ptr[0])+prio);
  980. }
  981. /* this used to be after charge_class but this constelation
  982. gives us slightly better performance */
  983. if (!cl->un.leaf.q->q.qlen)
  984. htb_deactivate (q,cl);
  985. htb_charge_class (q,cl,level,skb->len);
  986. }
  987. return skb;
  988. }
  989. static void htb_delay_by(struct Qdisc *sch,long delay)
  990. {
  991. struct htb_sched *q = qdisc_priv(sch);
  992. if (delay <= 0) delay = 1;
  993. if (unlikely(delay > 5*HZ)) {
  994. if (net_ratelimit())
  995. printk(KERN_INFO "HTB delay %ld > 5sec\n", delay);
  996. delay = 5*HZ;
  997. }
  998. /* why don't use jiffies here ? because expires can be in past */
  999. mod_timer(&q->timer, q->jiffies + delay);
  1000. sch->flags |= TCQ_F_THROTTLED;
  1001. sch->qstats.overlimits++;
  1002. HTB_DBG(3,1,"htb_deq t_delay=%ld\n",delay);
  1003. }
  1004. static struct sk_buff *htb_dequeue(struct Qdisc *sch)
  1005. {
  1006. struct sk_buff *skb = NULL;
  1007. struct htb_sched *q = qdisc_priv(sch);
  1008. int level;
  1009. long min_delay;
  1010. #ifdef HTB_DEBUG
  1011. int evs_used = 0;
  1012. #endif
  1013. q->jiffies = jiffies;
  1014. HTB_DBG(3,1,"htb_deq dircnt=%d qlen=%d\n",skb_queue_len(&q->direct_queue),
  1015. sch->q.qlen);
  1016. /* try to dequeue direct packets as high prio (!) to minimize cpu work */
  1017. if ((skb = __skb_dequeue(&q->direct_queue)) != NULL) {
  1018. sch->flags &= ~TCQ_F_THROTTLED;
  1019. sch->q.qlen--;
  1020. return skb;
  1021. }
  1022. if (!sch->q.qlen) goto fin;
  1023. PSCHED_GET_TIME(q->now);
  1024. min_delay = LONG_MAX;
  1025. q->nwc_hit = 0;
  1026. for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
  1027. /* common case optimization - skip event handler quickly */
  1028. int m;
  1029. long delay;
  1030. if (time_after_eq(q->jiffies, q->near_ev_cache[level])) {
  1031. delay = htb_do_events(q,level);
  1032. q->near_ev_cache[level] = q->jiffies + (delay ? delay : HZ);
  1033. #ifdef HTB_DEBUG
  1034. evs_used++;
  1035. #endif
  1036. } else
  1037. delay = q->near_ev_cache[level] - q->jiffies;
  1038. if (delay && min_delay > delay)
  1039. min_delay = delay;
  1040. m = ~q->row_mask[level];
  1041. while (m != (int)(-1)) {
  1042. int prio = ffz (m);
  1043. m |= 1 << prio;
  1044. skb = htb_dequeue_tree(q,prio,level);
  1045. if (likely(skb != NULL)) {
  1046. sch->q.qlen--;
  1047. sch->flags &= ~TCQ_F_THROTTLED;
  1048. goto fin;
  1049. }
  1050. }
  1051. }
  1052. #ifdef HTB_DEBUG
  1053. if (!q->nwc_hit && min_delay >= 10*HZ && net_ratelimit()) {
  1054. if (min_delay == LONG_MAX) {
  1055. printk(KERN_ERR "HTB: dequeue bug (%d,%lu,%lu), report it please !\n",
  1056. evs_used,q->jiffies,jiffies);
  1057. htb_debug_dump(q);
  1058. } else
  1059. printk(KERN_WARNING "HTB: mindelay=%ld, some class has "
  1060. "too small rate\n",min_delay);
  1061. }
  1062. #endif
  1063. htb_delay_by (sch,min_delay > 5*HZ ? 5*HZ : min_delay);
  1064. fin:
  1065. HTB_DBG(3,1,"htb_deq_end %s j=%lu skb=%p\n",sch->dev->name,q->jiffies,skb);
  1066. return skb;
  1067. }
  1068. /* try to drop from each class (by prio) until one succeed */
  1069. static unsigned int htb_drop(struct Qdisc* sch)
  1070. {
  1071. struct htb_sched *q = qdisc_priv(sch);
  1072. int prio;
  1073. for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
  1074. struct list_head *p;
  1075. list_for_each (p,q->drops+prio) {
  1076. struct htb_class *cl = list_entry(p, struct htb_class,
  1077. un.leaf.drop_list);
  1078. unsigned int len;
  1079. if (cl->un.leaf.q->ops->drop &&
  1080. (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
  1081. sch->q.qlen--;
  1082. if (!cl->un.leaf.q->q.qlen)
  1083. htb_deactivate (q,cl);
  1084. return len;
  1085. }
  1086. }
  1087. }
  1088. return 0;
  1089. }
  1090. /* reset all classes */
  1091. /* always caled under BH & queue lock */
  1092. static void htb_reset(struct Qdisc* sch)
  1093. {
  1094. struct htb_sched *q = qdisc_priv(sch);
  1095. int i;
  1096. HTB_DBG(0,1,"htb_reset sch=%p, handle=%X\n",sch,sch->handle);
  1097. for (i = 0; i < HTB_HSIZE; i++) {
  1098. struct list_head *p;
  1099. list_for_each (p,q->hash+i) {
  1100. struct htb_class *cl = list_entry(p,struct htb_class,hlist);
  1101. if (cl->level)
  1102. memset(&cl->un.inner,0,sizeof(cl->un.inner));
  1103. else {
  1104. if (cl->un.leaf.q)
  1105. qdisc_reset(cl->un.leaf.q);
  1106. INIT_LIST_HEAD(&cl->un.leaf.drop_list);
  1107. }
  1108. cl->prio_activity = 0;
  1109. cl->cmode = HTB_CAN_SEND;
  1110. #ifdef HTB_DEBUG
  1111. cl->pq_node.rb_color = -1;
  1112. memset(cl->node,255,sizeof(cl->node));
  1113. #endif
  1114. }
  1115. }
  1116. sch->flags &= ~TCQ_F_THROTTLED;
  1117. del_timer(&q->timer);
  1118. __skb_queue_purge(&q->direct_queue);
  1119. sch->q.qlen = 0;
  1120. memset(q->row,0,sizeof(q->row));
  1121. memset(q->row_mask,0,sizeof(q->row_mask));
  1122. memset(q->wait_pq,0,sizeof(q->wait_pq));
  1123. memset(q->ptr,0,sizeof(q->ptr));
  1124. for (i = 0; i < TC_HTB_NUMPRIO; i++)
  1125. INIT_LIST_HEAD(q->drops+i);
  1126. }
  1127. static int htb_init(struct Qdisc *sch, struct rtattr *opt)
  1128. {
  1129. struct htb_sched *q = qdisc_priv(sch);
  1130. struct rtattr *tb[TCA_HTB_INIT];
  1131. struct tc_htb_glob *gopt;
  1132. int i;
  1133. #ifdef HTB_DEBUG
  1134. printk(KERN_INFO "HTB init, kernel part version %d.%d\n",
  1135. HTB_VER >> 16,HTB_VER & 0xffff);
  1136. #endif
  1137. if (!opt || rtattr_parse_nested(tb, TCA_HTB_INIT, opt) ||
  1138. tb[TCA_HTB_INIT-1] == NULL ||
  1139. RTA_PAYLOAD(tb[TCA_HTB_INIT-1]) < sizeof(*gopt)) {
  1140. printk(KERN_ERR "HTB: hey probably you have bad tc tool ?\n");
  1141. return -EINVAL;
  1142. }
  1143. gopt = RTA_DATA(tb[TCA_HTB_INIT-1]);
  1144. if (gopt->version != HTB_VER >> 16) {
  1145. printk(KERN_ERR "HTB: need tc/htb version %d (minor is %d), you have %d\n",
  1146. HTB_VER >> 16,HTB_VER & 0xffff,gopt->version);
  1147. return -EINVAL;
  1148. }
  1149. q->debug = gopt->debug;
  1150. HTB_DBG(0,1,"htb_init sch=%p handle=%X r2q=%d\n",sch,sch->handle,gopt->rate2quantum);
  1151. INIT_LIST_HEAD(&q->root);
  1152. for (i = 0; i < HTB_HSIZE; i++)
  1153. INIT_LIST_HEAD(q->hash+i);
  1154. for (i = 0; i < TC_HTB_NUMPRIO; i++)
  1155. INIT_LIST_HEAD(q->drops+i);
  1156. init_timer(&q->timer);
  1157. skb_queue_head_init(&q->direct_queue);
  1158. q->direct_qlen = sch->dev->tx_queue_len;
  1159. if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
  1160. q->direct_qlen = 2;
  1161. q->timer.function = htb_timer;
  1162. q->timer.data = (unsigned long)sch;
  1163. #ifdef HTB_RATECM
  1164. init_timer(&q->rttim);
  1165. q->rttim.function = htb_rate_timer;
  1166. q->rttim.data = (unsigned long)sch;
  1167. q->rttim.expires = jiffies + HZ;
  1168. add_timer(&q->rttim);
  1169. #endif
  1170. if ((q->rate2quantum = gopt->rate2quantum) < 1)
  1171. q->rate2quantum = 1;
  1172. q->defcls = gopt->defcls;
  1173. return 0;
  1174. }
  1175. static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
  1176. {
  1177. struct htb_sched *q = qdisc_priv(sch);
  1178. unsigned char *b = skb->tail;
  1179. struct rtattr *rta;
  1180. struct tc_htb_glob gopt;
  1181. HTB_DBG(0,1,"htb_dump sch=%p, handle=%X\n",sch,sch->handle);
  1182. HTB_QLOCK(sch);
  1183. gopt.direct_pkts = q->direct_pkts;
  1184. #ifdef HTB_DEBUG
  1185. if (HTB_DBG_COND(0,2))
  1186. htb_debug_dump(q);
  1187. #endif
  1188. gopt.version = HTB_VER;
  1189. gopt.rate2quantum = q->rate2quantum;
  1190. gopt.defcls = q->defcls;
  1191. gopt.debug = q->debug;
  1192. rta = (struct rtattr*)b;
  1193. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  1194. RTA_PUT(skb, TCA_HTB_INIT, sizeof(gopt), &gopt);
  1195. rta->rta_len = skb->tail - b;
  1196. HTB_QUNLOCK(sch);
  1197. return skb->len;
  1198. rtattr_failure:
  1199. HTB_QUNLOCK(sch);
  1200. skb_trim(skb, skb->tail - skb->data);
  1201. return -1;
  1202. }
  1203. static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
  1204. struct sk_buff *skb, struct tcmsg *tcm)
  1205. {
  1206. #ifdef HTB_DEBUG
  1207. struct htb_sched *q = qdisc_priv(sch);
  1208. #endif
  1209. struct htb_class *cl = (struct htb_class*)arg;
  1210. unsigned char *b = skb->tail;
  1211. struct rtattr *rta;
  1212. struct tc_htb_opt opt;
  1213. HTB_DBG(0,1,"htb_dump_class handle=%X clid=%X\n",sch->handle,cl->classid);
  1214. HTB_QLOCK(sch);
  1215. tcm->tcm_parent = cl->parent ? cl->parent->classid : TC_H_ROOT;
  1216. tcm->tcm_handle = cl->classid;
  1217. if (!cl->level && cl->un.leaf.q)
  1218. tcm->tcm_info = cl->un.leaf.q->handle;
  1219. rta = (struct rtattr*)b;
  1220. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  1221. memset (&opt,0,sizeof(opt));
  1222. opt.rate = cl->rate->rate; opt.buffer = cl->buffer;
  1223. opt.ceil = cl->ceil->rate; opt.cbuffer = cl->cbuffer;
  1224. opt.quantum = cl->un.leaf.quantum; opt.prio = cl->un.leaf.prio;
  1225. opt.level = cl->level;
  1226. RTA_PUT(skb, TCA_HTB_PARMS, sizeof(opt), &opt);
  1227. rta->rta_len = skb->tail - b;
  1228. HTB_QUNLOCK(sch);
  1229. return skb->len;
  1230. rtattr_failure:
  1231. HTB_QUNLOCK(sch);
  1232. skb_trim(skb, b - skb->data);
  1233. return -1;
  1234. }
  1235. static int
  1236. htb_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  1237. struct gnet_dump *d)
  1238. {
  1239. struct htb_class *cl = (struct htb_class*)arg;
  1240. #ifdef HTB_RATECM
  1241. cl->rate_est.bps = cl->rate_bytes/(HTB_EWMAC*HTB_HSIZE);
  1242. cl->rate_est.pps = cl->rate_packets/(HTB_EWMAC*HTB_HSIZE);
  1243. #endif
  1244. if (!cl->level && cl->un.leaf.q)
  1245. cl->qstats.qlen = cl->un.leaf.q->q.qlen;
  1246. cl->xstats.tokens = cl->tokens;
  1247. cl->xstats.ctokens = cl->ctokens;
  1248. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  1249. gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
  1250. gnet_stats_copy_queue(d, &cl->qstats) < 0)
  1251. return -1;
  1252. return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
  1253. }
  1254. static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  1255. struct Qdisc **old)
  1256. {
  1257. struct htb_class *cl = (struct htb_class*)arg;
  1258. if (cl && !cl->level) {
  1259. if (new == NULL && (new = qdisc_create_dflt(sch->dev,
  1260. &pfifo_qdisc_ops)) == NULL)
  1261. return -ENOBUFS;
  1262. sch_tree_lock(sch);
  1263. if ((*old = xchg(&cl->un.leaf.q, new)) != NULL) {
  1264. if (cl->prio_activity)
  1265. htb_deactivate (qdisc_priv(sch),cl);
  1266. /* TODO: is it correct ? Why CBQ doesn't do it ? */
  1267. sch->q.qlen -= (*old)->q.qlen;
  1268. qdisc_reset(*old);
  1269. }
  1270. sch_tree_unlock(sch);
  1271. return 0;
  1272. }
  1273. return -ENOENT;
  1274. }
  1275. static struct Qdisc * htb_leaf(struct Qdisc *sch, unsigned long arg)
  1276. {
  1277. struct htb_class *cl = (struct htb_class*)arg;
  1278. return (cl && !cl->level) ? cl->un.leaf.q : NULL;
  1279. }
  1280. static unsigned long htb_get(struct Qdisc *sch, u32 classid)
  1281. {
  1282. #ifdef HTB_DEBUG
  1283. struct htb_sched *q = qdisc_priv(sch);
  1284. #endif
  1285. struct htb_class *cl = htb_find(classid,sch);
  1286. HTB_DBG(0,1,"htb_get clid=%X q=%p cl=%p ref=%d\n",classid,q,cl,cl?cl->refcnt:0);
  1287. if (cl)
  1288. cl->refcnt++;
  1289. return (unsigned long)cl;
  1290. }
  1291. static void htb_destroy_filters(struct tcf_proto **fl)
  1292. {
  1293. struct tcf_proto *tp;
  1294. while ((tp = *fl) != NULL) {
  1295. *fl = tp->next;
  1296. tcf_destroy(tp);
  1297. }
  1298. }
  1299. static void htb_destroy_class(struct Qdisc* sch,struct htb_class *cl)
  1300. {
  1301. struct htb_sched *q = qdisc_priv(sch);
  1302. HTB_DBG(0,1,"htb_destrycls clid=%X ref=%d\n", cl?cl->classid:0,cl?cl->refcnt:0);
  1303. if (!cl->level) {
  1304. BUG_TRAP(cl->un.leaf.q);
  1305. sch->q.qlen -= cl->un.leaf.q->q.qlen;
  1306. qdisc_destroy(cl->un.leaf.q);
  1307. }
  1308. qdisc_put_rtab(cl->rate);
  1309. qdisc_put_rtab(cl->ceil);
  1310. htb_destroy_filters (&cl->filter_list);
  1311. while (!list_empty(&cl->children))
  1312. htb_destroy_class (sch,list_entry(cl->children.next,
  1313. struct htb_class,sibling));
  1314. /* note: this delete may happen twice (see htb_delete) */
  1315. list_del(&cl->hlist);
  1316. list_del(&cl->sibling);
  1317. if (cl->prio_activity)
  1318. htb_deactivate (q,cl);
  1319. if (cl->cmode != HTB_CAN_SEND)
  1320. htb_safe_rb_erase(&cl->pq_node,q->wait_pq+cl->level);
  1321. kfree(cl);
  1322. }
  1323. /* always caled under BH & queue lock */
  1324. static void htb_destroy(struct Qdisc* sch)
  1325. {
  1326. struct htb_sched *q = qdisc_priv(sch);
  1327. HTB_DBG(0,1,"htb_destroy q=%p\n",q);
  1328. del_timer_sync (&q->timer);
  1329. #ifdef HTB_RATECM
  1330. del_timer_sync (&q->rttim);
  1331. #endif
  1332. /* This line used to be after htb_destroy_class call below
  1333. and surprisingly it worked in 2.4. But it must precede it
  1334. because filter need its target class alive to be able to call
  1335. unbind_filter on it (without Oops). */
  1336. htb_destroy_filters(&q->filter_list);
  1337. while (!list_empty(&q->root))
  1338. htb_destroy_class (sch,list_entry(q->root.next,
  1339. struct htb_class,sibling));
  1340. __skb_queue_purge(&q->direct_queue);
  1341. }
  1342. static int htb_delete(struct Qdisc *sch, unsigned long arg)
  1343. {
  1344. struct htb_sched *q = qdisc_priv(sch);
  1345. struct htb_class *cl = (struct htb_class*)arg;
  1346. HTB_DBG(0,1,"htb_delete q=%p cl=%X ref=%d\n",q,cl?cl->classid:0,cl?cl->refcnt:0);
  1347. // TODO: why don't allow to delete subtree ? references ? does
  1348. // tc subsys quarantee us that in htb_destroy it holds no class
  1349. // refs so that we can remove children safely there ?
  1350. if (!list_empty(&cl->children) || cl->filter_cnt)
  1351. return -EBUSY;
  1352. sch_tree_lock(sch);
  1353. /* delete from hash and active; remainder in destroy_class */
  1354. list_del_init(&cl->hlist);
  1355. if (cl->prio_activity)
  1356. htb_deactivate (q,cl);
  1357. if (--cl->refcnt == 0)
  1358. htb_destroy_class(sch,cl);
  1359. sch_tree_unlock(sch);
  1360. return 0;
  1361. }
  1362. static void htb_put(struct Qdisc *sch, unsigned long arg)
  1363. {
  1364. #ifdef HTB_DEBUG
  1365. struct htb_sched *q = qdisc_priv(sch);
  1366. #endif
  1367. struct htb_class *cl = (struct htb_class*)arg;
  1368. HTB_DBG(0,1,"htb_put q=%p cl=%X ref=%d\n",q,cl?cl->classid:0,cl?cl->refcnt:0);
  1369. if (--cl->refcnt == 0)
  1370. htb_destroy_class(sch,cl);
  1371. }
  1372. static int htb_change_class(struct Qdisc *sch, u32 classid,
  1373. u32 parentid, struct rtattr **tca, unsigned long *arg)
  1374. {
  1375. int err = -EINVAL;
  1376. struct htb_sched *q = qdisc_priv(sch);
  1377. struct htb_class *cl = (struct htb_class*)*arg,*parent;
  1378. struct rtattr *opt = tca[TCA_OPTIONS-1];
  1379. struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
  1380. struct rtattr *tb[TCA_HTB_RTAB];
  1381. struct tc_htb_opt *hopt;
  1382. /* extract all subattrs from opt attr */
  1383. if (!opt || rtattr_parse_nested(tb, TCA_HTB_RTAB, opt) ||
  1384. tb[TCA_HTB_PARMS-1] == NULL ||
  1385. RTA_PAYLOAD(tb[TCA_HTB_PARMS-1]) < sizeof(*hopt))
  1386. goto failure;
  1387. parent = parentid == TC_H_ROOT ? NULL : htb_find (parentid,sch);
  1388. hopt = RTA_DATA(tb[TCA_HTB_PARMS-1]);
  1389. HTB_DBG(0,1,"htb_chg cl=%p(%X), clid=%X, parid=%X, opt/prio=%d, rate=%u, buff=%d, quant=%d\n", cl,cl?cl->classid:0,classid,parentid,(int)hopt->prio,hopt->rate.rate,hopt->buffer,hopt->quantum);
  1390. rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB-1]);
  1391. ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB-1]);
  1392. if (!rtab || !ctab) goto failure;
  1393. if (!cl) { /* new class */
  1394. struct Qdisc *new_q;
  1395. /* check for valid classid */
  1396. if (!classid || TC_H_MAJ(classid^sch->handle) || htb_find(classid,sch))
  1397. goto failure;
  1398. /* check maximal depth */
  1399. if (parent && parent->parent && parent->parent->level < 2) {
  1400. printk(KERN_ERR "htb: tree is too deep\n");
  1401. goto failure;
  1402. }
  1403. err = -ENOBUFS;
  1404. if ((cl = kmalloc(sizeof(*cl), GFP_KERNEL)) == NULL)
  1405. goto failure;
  1406. memset(cl, 0, sizeof(*cl));
  1407. cl->refcnt = 1;
  1408. INIT_LIST_HEAD(&cl->sibling);
  1409. INIT_LIST_HEAD(&cl->hlist);
  1410. INIT_LIST_HEAD(&cl->children);
  1411. INIT_LIST_HEAD(&cl->un.leaf.drop_list);
  1412. #ifdef HTB_DEBUG
  1413. cl->magic = HTB_CMAGIC;
  1414. #endif
  1415. /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
  1416. so that can't be used inside of sch_tree_lock
  1417. -- thanks to Karlis Peisenieks */
  1418. new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
  1419. sch_tree_lock(sch);
  1420. if (parent && !parent->level) {
  1421. /* turn parent into inner node */
  1422. sch->q.qlen -= parent->un.leaf.q->q.qlen;
  1423. qdisc_destroy (parent->un.leaf.q);
  1424. if (parent->prio_activity)
  1425. htb_deactivate (q,parent);
  1426. /* remove from evt list because of level change */
  1427. if (parent->cmode != HTB_CAN_SEND) {
  1428. htb_safe_rb_erase(&parent->pq_node,q->wait_pq /*+0*/);
  1429. parent->cmode = HTB_CAN_SEND;
  1430. }
  1431. parent->level = (parent->parent ? parent->parent->level
  1432. : TC_HTB_MAXDEPTH) - 1;
  1433. memset (&parent->un.inner,0,sizeof(parent->un.inner));
  1434. }
  1435. /* leaf (we) needs elementary qdisc */
  1436. cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
  1437. cl->classid = classid; cl->parent = parent;
  1438. /* set class to be in HTB_CAN_SEND state */
  1439. cl->tokens = hopt->buffer;
  1440. cl->ctokens = hopt->cbuffer;
  1441. cl->mbuffer = 60000000; /* 1min */
  1442. PSCHED_GET_TIME(cl->t_c);
  1443. cl->cmode = HTB_CAN_SEND;
  1444. /* attach to the hash list and parent's family */
  1445. list_add_tail(&cl->hlist, q->hash+htb_hash(classid));
  1446. list_add_tail(&cl->sibling, parent ? &parent->children : &q->root);
  1447. #ifdef HTB_DEBUG
  1448. {
  1449. int i;
  1450. for (i = 0; i < TC_HTB_NUMPRIO; i++) cl->node[i].rb_color = -1;
  1451. cl->pq_node.rb_color = -1;
  1452. }
  1453. #endif
  1454. } else sch_tree_lock(sch);
  1455. /* it used to be a nasty bug here, we have to check that node
  1456. is really leaf before changing cl->un.leaf ! */
  1457. if (!cl->level) {
  1458. cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
  1459. if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
  1460. printk(KERN_WARNING "HTB: quantum of class %X is small. Consider r2q change.\n", cl->classid);
  1461. cl->un.leaf.quantum = 1000;
  1462. }
  1463. if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
  1464. printk(KERN_WARNING "HTB: quantum of class %X is big. Consider r2q change.\n", cl->classid);
  1465. cl->un.leaf.quantum = 200000;
  1466. }
  1467. if (hopt->quantum)
  1468. cl->un.leaf.quantum = hopt->quantum;
  1469. if ((cl->un.leaf.prio = hopt->prio) >= TC_HTB_NUMPRIO)
  1470. cl->un.leaf.prio = TC_HTB_NUMPRIO - 1;
  1471. }
  1472. cl->buffer = hopt->buffer;
  1473. cl->cbuffer = hopt->cbuffer;
  1474. if (cl->rate) qdisc_put_rtab(cl->rate); cl->rate = rtab;
  1475. if (cl->ceil) qdisc_put_rtab(cl->ceil); cl->ceil = ctab;
  1476. sch_tree_unlock(sch);
  1477. *arg = (unsigned long)cl;
  1478. return 0;
  1479. failure:
  1480. if (rtab) qdisc_put_rtab(rtab);
  1481. if (ctab) qdisc_put_rtab(ctab);
  1482. return err;
  1483. }
  1484. static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
  1485. {
  1486. struct htb_sched *q = qdisc_priv(sch);
  1487. struct htb_class *cl = (struct htb_class *)arg;
  1488. struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
  1489. HTB_DBG(0,2,"htb_tcf q=%p clid=%X fref=%d fl=%p\n",q,cl?cl->classid:0,cl?cl->filter_cnt:q->filter_cnt,*fl);
  1490. return fl;
  1491. }
  1492. static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
  1493. u32 classid)
  1494. {
  1495. struct htb_sched *q = qdisc_priv(sch);
  1496. struct htb_class *cl = htb_find (classid,sch);
  1497. HTB_DBG(0,2,"htb_bind q=%p clid=%X cl=%p fref=%d\n",q,classid,cl,cl?cl->filter_cnt:q->filter_cnt);
  1498. /*if (cl && !cl->level) return 0;
  1499. The line above used to be there to prevent attaching filters to
  1500. leaves. But at least tc_index filter uses this just to get class
  1501. for other reasons so that we have to allow for it.
  1502. ----
  1503. 19.6.2002 As Werner explained it is ok - bind filter is just
  1504. another way to "lock" the class - unlike "get" this lock can
  1505. be broken by class during destroy IIUC.
  1506. */
  1507. if (cl)
  1508. cl->filter_cnt++;
  1509. else
  1510. q->filter_cnt++;
  1511. return (unsigned long)cl;
  1512. }
  1513. static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
  1514. {
  1515. struct htb_sched *q = qdisc_priv(sch);
  1516. struct htb_class *cl = (struct htb_class *)arg;
  1517. HTB_DBG(0,2,"htb_unbind q=%p cl=%p fref=%d\n",q,cl,cl?cl->filter_cnt:q->filter_cnt);
  1518. if (cl)
  1519. cl->filter_cnt--;
  1520. else
  1521. q->filter_cnt--;
  1522. }
  1523. static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  1524. {
  1525. struct htb_sched *q = qdisc_priv(sch);
  1526. int i;
  1527. if (arg->stop)
  1528. return;
  1529. for (i = 0; i < HTB_HSIZE; i++) {
  1530. struct list_head *p;
  1531. list_for_each (p,q->hash+i) {
  1532. struct htb_class *cl = list_entry(p,struct htb_class,hlist);
  1533. if (arg->count < arg->skip) {
  1534. arg->count++;
  1535. continue;
  1536. }
  1537. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  1538. arg->stop = 1;
  1539. return;
  1540. }
  1541. arg->count++;
  1542. }
  1543. }
  1544. }
  1545. static struct Qdisc_class_ops htb_class_ops = {
  1546. .graft = htb_graft,
  1547. .leaf = htb_leaf,
  1548. .get = htb_get,
  1549. .put = htb_put,
  1550. .change = htb_change_class,
  1551. .delete = htb_delete,
  1552. .walk = htb_walk,
  1553. .tcf_chain = htb_find_tcf,
  1554. .bind_tcf = htb_bind_filter,
  1555. .unbind_tcf = htb_unbind_filter,
  1556. .dump = htb_dump_class,
  1557. .dump_stats = htb_dump_class_stats,
  1558. };
  1559. static struct Qdisc_ops htb_qdisc_ops = {
  1560. .next = NULL,
  1561. .cl_ops = &htb_class_ops,
  1562. .id = "htb",
  1563. .priv_size = sizeof(struct htb_sched),
  1564. .enqueue = htb_enqueue,
  1565. .dequeue = htb_dequeue,
  1566. .requeue = htb_requeue,
  1567. .drop = htb_drop,
  1568. .init = htb_init,
  1569. .reset = htb_reset,
  1570. .destroy = htb_destroy,
  1571. .change = NULL /* htb_change */,
  1572. .dump = htb_dump,
  1573. .owner = THIS_MODULE,
  1574. };
  1575. static int __init htb_module_init(void)
  1576. {
  1577. return register_qdisc(&htb_qdisc_ops);
  1578. }
  1579. static void __exit htb_module_exit(void)
  1580. {
  1581. unregister_qdisc(&htb_qdisc_ops);
  1582. }
  1583. module_init(htb_module_init)
  1584. module_exit(htb_module_exit)
  1585. MODULE_LICENSE("GPL");