sch_htb.c 42 KB

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