sch_htb.c 43 KB

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