sch_htb.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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 tc_htb_xstats xstats; /* our special stats */
  108. /* token bucket parameters */
  109. s64 tokens, ctokens;/* current number of tokens */
  110. s64 t_c; /* checkpoint time */
  111. union {
  112. struct htb_class_leaf {
  113. struct list_head drop_list;
  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. };
  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 qdisc_skb_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, false)) >= 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 void htb_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  499. struct qdisc_skb_head *qh)
  500. {
  501. struct sk_buff *last = qh->tail;
  502. if (last) {
  503. skb->next = NULL;
  504. last->next = skb;
  505. qh->tail = skb;
  506. } else {
  507. qh->tail = skb;
  508. qh->head = skb;
  509. }
  510. qh->qlen++;
  511. }
  512. static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  513. struct sk_buff **to_free)
  514. {
  515. int uninitialized_var(ret);
  516. struct htb_sched *q = qdisc_priv(sch);
  517. struct htb_class *cl = htb_classify(skb, sch, &ret);
  518. if (cl == HTB_DIRECT) {
  519. /* enqueue to helper queue */
  520. if (q->direct_queue.qlen < q->direct_qlen) {
  521. htb_enqueue_tail(skb, sch, &q->direct_queue);
  522. q->direct_pkts++;
  523. } else {
  524. return qdisc_drop(skb, sch, to_free);
  525. }
  526. #ifdef CONFIG_NET_CLS_ACT
  527. } else if (!cl) {
  528. if (ret & __NET_XMIT_BYPASS)
  529. qdisc_qstats_drop(sch);
  530. __qdisc_drop(skb, to_free);
  531. return ret;
  532. #endif
  533. } else if ((ret = qdisc_enqueue(skb, cl->un.leaf.q,
  534. to_free)) != NET_XMIT_SUCCESS) {
  535. if (net_xmit_drop_count(ret)) {
  536. qdisc_qstats_drop(sch);
  537. cl->drops++;
  538. }
  539. return ret;
  540. } else {
  541. htb_activate(q, cl);
  542. }
  543. qdisc_qstats_backlog_inc(sch, skb);
  544. sch->q.qlen++;
  545. return NET_XMIT_SUCCESS;
  546. }
  547. static inline void htb_accnt_tokens(struct htb_class *cl, int bytes, s64 diff)
  548. {
  549. s64 toks = diff + cl->tokens;
  550. if (toks > cl->buffer)
  551. toks = cl->buffer;
  552. toks -= (s64) psched_l2t_ns(&cl->rate, bytes);
  553. if (toks <= -cl->mbuffer)
  554. toks = 1 - cl->mbuffer;
  555. cl->tokens = toks;
  556. }
  557. static inline void htb_accnt_ctokens(struct htb_class *cl, int bytes, s64 diff)
  558. {
  559. s64 toks = diff + cl->ctokens;
  560. if (toks > cl->cbuffer)
  561. toks = cl->cbuffer;
  562. toks -= (s64) psched_l2t_ns(&cl->ceil, bytes);
  563. if (toks <= -cl->mbuffer)
  564. toks = 1 - cl->mbuffer;
  565. cl->ctokens = toks;
  566. }
  567. /**
  568. * htb_charge_class - charges amount "bytes" to leaf and ancestors
  569. *
  570. * Routine assumes that packet "bytes" long was dequeued from leaf cl
  571. * borrowing from "level". It accounts bytes to ceil leaky bucket for
  572. * leaf and all ancestors and to rate bucket for ancestors at levels
  573. * "level" and higher. It also handles possible change of mode resulting
  574. * from the update. Note that mode can also increase here (MAY_BORROW to
  575. * CAN_SEND) because we can use more precise clock that event queue here.
  576. * In such case we remove class from event queue first.
  577. */
  578. static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
  579. int level, struct sk_buff *skb)
  580. {
  581. int bytes = qdisc_pkt_len(skb);
  582. enum htb_cmode old_mode;
  583. s64 diff;
  584. while (cl) {
  585. diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
  586. if (cl->level >= level) {
  587. if (cl->level == level)
  588. cl->xstats.lends++;
  589. htb_accnt_tokens(cl, bytes, diff);
  590. } else {
  591. cl->xstats.borrows++;
  592. cl->tokens += diff; /* we moved t_c; update tokens */
  593. }
  594. htb_accnt_ctokens(cl, bytes, diff);
  595. cl->t_c = q->now;
  596. old_mode = cl->cmode;
  597. diff = 0;
  598. htb_change_class_mode(q, cl, &diff);
  599. if (old_mode != cl->cmode) {
  600. if (old_mode != HTB_CAN_SEND)
  601. htb_safe_rb_erase(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
  602. if (cl->cmode != HTB_CAN_SEND)
  603. htb_add_to_wait_tree(q, cl, diff);
  604. }
  605. /* update basic stats except for leaves which are already updated */
  606. if (cl->level)
  607. bstats_update(&cl->bstats, skb);
  608. cl = cl->parent;
  609. }
  610. }
  611. /**
  612. * htb_do_events - make mode changes to classes at the level
  613. *
  614. * Scans event queue for pending events and applies them. Returns time of
  615. * next pending event (0 for no event in pq, q->now for too many events).
  616. * Note: Applied are events whose have cl->pq_key <= q->now.
  617. */
  618. static s64 htb_do_events(struct htb_sched *q, const int level,
  619. unsigned long start)
  620. {
  621. /* don't run for longer than 2 jiffies; 2 is used instead of
  622. * 1 to simplify things when jiffy is going to be incremented
  623. * too soon
  624. */
  625. unsigned long stop_at = start + 2;
  626. struct rb_root *wait_pq = &q->hlevel[level].wait_pq;
  627. while (time_before(jiffies, stop_at)) {
  628. struct htb_class *cl;
  629. s64 diff;
  630. struct rb_node *p = rb_first(wait_pq);
  631. if (!p)
  632. return 0;
  633. cl = rb_entry(p, struct htb_class, pq_node);
  634. if (cl->pq_key > q->now)
  635. return cl->pq_key;
  636. htb_safe_rb_erase(p, wait_pq);
  637. diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
  638. htb_change_class_mode(q, cl, &diff);
  639. if (cl->cmode != HTB_CAN_SEND)
  640. htb_add_to_wait_tree(q, cl, diff);
  641. }
  642. /* too much load - let's continue after a break for scheduling */
  643. if (!(q->warned & HTB_WARN_TOOMANYEVENTS)) {
  644. pr_warn("htb: too many events!\n");
  645. q->warned |= HTB_WARN_TOOMANYEVENTS;
  646. }
  647. return q->now;
  648. }
  649. /* Returns class->node+prio from id-tree where classe's id is >= id. NULL
  650. * is no such one exists.
  651. */
  652. static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
  653. u32 id)
  654. {
  655. struct rb_node *r = NULL;
  656. while (n) {
  657. struct htb_class *cl =
  658. rb_entry(n, struct htb_class, node[prio]);
  659. if (id > cl->common.classid) {
  660. n = n->rb_right;
  661. } else if (id < cl->common.classid) {
  662. r = n;
  663. n = n->rb_left;
  664. } else {
  665. return n;
  666. }
  667. }
  668. return r;
  669. }
  670. /**
  671. * htb_lookup_leaf - returns next leaf class in DRR order
  672. *
  673. * Find leaf where current feed pointers points to.
  674. */
  675. static struct htb_class *htb_lookup_leaf(struct htb_prio *hprio, const int prio)
  676. {
  677. int i;
  678. struct {
  679. struct rb_node *root;
  680. struct rb_node **pptr;
  681. u32 *pid;
  682. } stk[TC_HTB_MAXDEPTH], *sp = stk;
  683. BUG_ON(!hprio->row.rb_node);
  684. sp->root = hprio->row.rb_node;
  685. sp->pptr = &hprio->ptr;
  686. sp->pid = &hprio->last_ptr_id;
  687. for (i = 0; i < 65535; i++) {
  688. if (!*sp->pptr && *sp->pid) {
  689. /* ptr was invalidated but id is valid - try to recover
  690. * the original or next ptr
  691. */
  692. *sp->pptr =
  693. htb_id_find_next_upper(prio, sp->root, *sp->pid);
  694. }
  695. *sp->pid = 0; /* ptr is valid now so that remove this hint as it
  696. * can become out of date quickly
  697. */
  698. if (!*sp->pptr) { /* we are at right end; rewind & go up */
  699. *sp->pptr = sp->root;
  700. while ((*sp->pptr)->rb_left)
  701. *sp->pptr = (*sp->pptr)->rb_left;
  702. if (sp > stk) {
  703. sp--;
  704. if (!*sp->pptr) {
  705. WARN_ON(1);
  706. return NULL;
  707. }
  708. htb_next_rb_node(sp->pptr);
  709. }
  710. } else {
  711. struct htb_class *cl;
  712. struct htb_prio *clp;
  713. cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
  714. if (!cl->level)
  715. return cl;
  716. clp = &cl->un.inner.clprio[prio];
  717. (++sp)->root = clp->feed.rb_node;
  718. sp->pptr = &clp->ptr;
  719. sp->pid = &clp->last_ptr_id;
  720. }
  721. }
  722. WARN_ON(1);
  723. return NULL;
  724. }
  725. /* dequeues packet at given priority and level; call only if
  726. * you are sure that there is active class at prio/level
  727. */
  728. static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, const int prio,
  729. const int level)
  730. {
  731. struct sk_buff *skb = NULL;
  732. struct htb_class *cl, *start;
  733. struct htb_level *hlevel = &q->hlevel[level];
  734. struct htb_prio *hprio = &hlevel->hprio[prio];
  735. /* look initial class up in the row */
  736. start = cl = htb_lookup_leaf(hprio, prio);
  737. do {
  738. next:
  739. if (unlikely(!cl))
  740. return NULL;
  741. /* class can be empty - it is unlikely but can be true if leaf
  742. * qdisc drops packets in enqueue routine or if someone used
  743. * graft operation on the leaf since last dequeue;
  744. * simply deactivate and skip such class
  745. */
  746. if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
  747. struct htb_class *next;
  748. htb_deactivate(q, cl);
  749. /* row/level might become empty */
  750. if ((q->row_mask[level] & (1 << prio)) == 0)
  751. return NULL;
  752. next = htb_lookup_leaf(hprio, prio);
  753. if (cl == start) /* fix start if we just deleted it */
  754. start = next;
  755. cl = next;
  756. goto next;
  757. }
  758. skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
  759. if (likely(skb != NULL))
  760. break;
  761. qdisc_warn_nonwc("htb", cl->un.leaf.q);
  762. htb_next_rb_node(level ? &cl->parent->un.inner.clprio[prio].ptr:
  763. &q->hlevel[0].hprio[prio].ptr);
  764. cl = htb_lookup_leaf(hprio, prio);
  765. } while (cl != start);
  766. if (likely(skb != NULL)) {
  767. bstats_update(&cl->bstats, skb);
  768. cl->un.leaf.deficit[level] -= qdisc_pkt_len(skb);
  769. if (cl->un.leaf.deficit[level] < 0) {
  770. cl->un.leaf.deficit[level] += cl->quantum;
  771. htb_next_rb_node(level ? &cl->parent->un.inner.clprio[prio].ptr :
  772. &q->hlevel[0].hprio[prio].ptr);
  773. }
  774. /* this used to be after charge_class but this constelation
  775. * gives us slightly better performance
  776. */
  777. if (!cl->un.leaf.q->q.qlen)
  778. htb_deactivate(q, cl);
  779. htb_charge_class(q, cl, level, skb);
  780. }
  781. return skb;
  782. }
  783. static struct sk_buff *htb_dequeue(struct Qdisc *sch)
  784. {
  785. struct sk_buff *skb;
  786. struct htb_sched *q = qdisc_priv(sch);
  787. int level;
  788. s64 next_event;
  789. unsigned long start_at;
  790. /* try to dequeue direct packets as high prio (!) to minimize cpu work */
  791. skb = __qdisc_dequeue_head(&q->direct_queue);
  792. if (skb != NULL) {
  793. ok:
  794. qdisc_bstats_update(sch, skb);
  795. qdisc_qstats_backlog_dec(sch, skb);
  796. sch->q.qlen--;
  797. return skb;
  798. }
  799. if (!sch->q.qlen)
  800. goto fin;
  801. q->now = ktime_get_ns();
  802. start_at = jiffies;
  803. next_event = q->now + 5LLU * NSEC_PER_SEC;
  804. for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
  805. /* common case optimization - skip event handler quickly */
  806. int m;
  807. s64 event = q->near_ev_cache[level];
  808. if (q->now >= event) {
  809. event = htb_do_events(q, level, start_at);
  810. if (!event)
  811. event = q->now + NSEC_PER_SEC;
  812. q->near_ev_cache[level] = event;
  813. }
  814. if (next_event > event)
  815. next_event = event;
  816. m = ~q->row_mask[level];
  817. while (m != (int)(-1)) {
  818. int prio = ffz(m);
  819. m |= 1 << prio;
  820. skb = htb_dequeue_tree(q, prio, level);
  821. if (likely(skb != NULL))
  822. goto ok;
  823. }
  824. }
  825. qdisc_qstats_overlimit(sch);
  826. if (likely(next_event > q->now))
  827. qdisc_watchdog_schedule_ns(&q->watchdog, next_event);
  828. else
  829. schedule_work(&q->work);
  830. fin:
  831. return skb;
  832. }
  833. /* reset all classes */
  834. /* always caled under BH & queue lock */
  835. static void htb_reset(struct Qdisc *sch)
  836. {
  837. struct htb_sched *q = qdisc_priv(sch);
  838. struct htb_class *cl;
  839. unsigned int i;
  840. for (i = 0; i < q->clhash.hashsize; i++) {
  841. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  842. if (cl->level)
  843. memset(&cl->un.inner, 0, sizeof(cl->un.inner));
  844. else {
  845. if (cl->un.leaf.q)
  846. qdisc_reset(cl->un.leaf.q);
  847. INIT_LIST_HEAD(&cl->un.leaf.drop_list);
  848. }
  849. cl->prio_activity = 0;
  850. cl->cmode = HTB_CAN_SEND;
  851. }
  852. }
  853. qdisc_watchdog_cancel(&q->watchdog);
  854. __qdisc_reset_queue(&q->direct_queue);
  855. sch->q.qlen = 0;
  856. sch->qstats.backlog = 0;
  857. memset(q->hlevel, 0, sizeof(q->hlevel));
  858. memset(q->row_mask, 0, sizeof(q->row_mask));
  859. for (i = 0; i < TC_HTB_NUMPRIO; i++)
  860. INIT_LIST_HEAD(q->drops + i);
  861. }
  862. static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
  863. [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
  864. [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
  865. [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
  866. [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
  867. [TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 },
  868. [TCA_HTB_RATE64] = { .type = NLA_U64 },
  869. [TCA_HTB_CEIL64] = { .type = NLA_U64 },
  870. };
  871. static void htb_work_func(struct work_struct *work)
  872. {
  873. struct htb_sched *q = container_of(work, struct htb_sched, work);
  874. struct Qdisc *sch = q->watchdog.qdisc;
  875. rcu_read_lock();
  876. __netif_schedule(qdisc_root(sch));
  877. rcu_read_unlock();
  878. }
  879. static int htb_init(struct Qdisc *sch, struct nlattr *opt)
  880. {
  881. struct htb_sched *q = qdisc_priv(sch);
  882. struct nlattr *tb[TCA_HTB_MAX + 1];
  883. struct tc_htb_glob *gopt;
  884. int err;
  885. int i;
  886. if (!opt)
  887. return -EINVAL;
  888. err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy);
  889. if (err < 0)
  890. return err;
  891. if (!tb[TCA_HTB_INIT])
  892. return -EINVAL;
  893. gopt = nla_data(tb[TCA_HTB_INIT]);
  894. if (gopt->version != HTB_VER >> 16)
  895. return -EINVAL;
  896. err = qdisc_class_hash_init(&q->clhash);
  897. if (err < 0)
  898. return err;
  899. for (i = 0; i < TC_HTB_NUMPRIO; i++)
  900. INIT_LIST_HEAD(q->drops + i);
  901. qdisc_watchdog_init(&q->watchdog, sch);
  902. INIT_WORK(&q->work, htb_work_func);
  903. qdisc_skb_head_init(&q->direct_queue);
  904. if (tb[TCA_HTB_DIRECT_QLEN])
  905. q->direct_qlen = nla_get_u32(tb[TCA_HTB_DIRECT_QLEN]);
  906. else
  907. q->direct_qlen = qdisc_dev(sch)->tx_queue_len;
  908. if ((q->rate2quantum = gopt->rate2quantum) < 1)
  909. q->rate2quantum = 1;
  910. q->defcls = gopt->defcls;
  911. return 0;
  912. }
  913. static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
  914. {
  915. struct htb_sched *q = qdisc_priv(sch);
  916. struct nlattr *nest;
  917. struct tc_htb_glob gopt;
  918. /* Its safe to not acquire qdisc lock. As we hold RTNL,
  919. * no change can happen on the qdisc parameters.
  920. */
  921. gopt.direct_pkts = q->direct_pkts;
  922. gopt.version = HTB_VER;
  923. gopt.rate2quantum = q->rate2quantum;
  924. gopt.defcls = q->defcls;
  925. gopt.debug = 0;
  926. nest = nla_nest_start(skb, TCA_OPTIONS);
  927. if (nest == NULL)
  928. goto nla_put_failure;
  929. if (nla_put(skb, TCA_HTB_INIT, sizeof(gopt), &gopt) ||
  930. nla_put_u32(skb, TCA_HTB_DIRECT_QLEN, q->direct_qlen))
  931. goto nla_put_failure;
  932. return nla_nest_end(skb, nest);
  933. nla_put_failure:
  934. nla_nest_cancel(skb, nest);
  935. return -1;
  936. }
  937. static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
  938. struct sk_buff *skb, struct tcmsg *tcm)
  939. {
  940. struct htb_class *cl = (struct htb_class *)arg;
  941. struct nlattr *nest;
  942. struct tc_htb_opt opt;
  943. /* Its safe to not acquire qdisc lock. As we hold RTNL,
  944. * no change can happen on the class parameters.
  945. */
  946. tcm->tcm_parent = cl->parent ? cl->parent->common.classid : TC_H_ROOT;
  947. tcm->tcm_handle = cl->common.classid;
  948. if (!cl->level && cl->un.leaf.q)
  949. tcm->tcm_info = cl->un.leaf.q->handle;
  950. nest = nla_nest_start(skb, TCA_OPTIONS);
  951. if (nest == NULL)
  952. goto nla_put_failure;
  953. memset(&opt, 0, sizeof(opt));
  954. psched_ratecfg_getrate(&opt.rate, &cl->rate);
  955. opt.buffer = PSCHED_NS2TICKS(cl->buffer);
  956. psched_ratecfg_getrate(&opt.ceil, &cl->ceil);
  957. opt.cbuffer = PSCHED_NS2TICKS(cl->cbuffer);
  958. opt.quantum = cl->quantum;
  959. opt.prio = cl->prio;
  960. opt.level = cl->level;
  961. if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt))
  962. goto nla_put_failure;
  963. if ((cl->rate.rate_bytes_ps >= (1ULL << 32)) &&
  964. nla_put_u64_64bit(skb, TCA_HTB_RATE64, cl->rate.rate_bytes_ps,
  965. TCA_HTB_PAD))
  966. goto nla_put_failure;
  967. if ((cl->ceil.rate_bytes_ps >= (1ULL << 32)) &&
  968. nla_put_u64_64bit(skb, TCA_HTB_CEIL64, cl->ceil.rate_bytes_ps,
  969. TCA_HTB_PAD))
  970. goto nla_put_failure;
  971. return nla_nest_end(skb, nest);
  972. nla_put_failure:
  973. nla_nest_cancel(skb, nest);
  974. return -1;
  975. }
  976. static int
  977. htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
  978. {
  979. struct htb_class *cl = (struct htb_class *)arg;
  980. struct gnet_stats_queue qs = {
  981. .drops = cl->drops,
  982. };
  983. __u32 qlen = 0;
  984. if (!cl->level && cl->un.leaf.q) {
  985. qlen = cl->un.leaf.q->q.qlen;
  986. qs.backlog = cl->un.leaf.q->qstats.backlog;
  987. }
  988. cl->xstats.tokens = clamp_t(s64, PSCHED_NS2TICKS(cl->tokens),
  989. INT_MIN, INT_MAX);
  990. cl->xstats.ctokens = clamp_t(s64, PSCHED_NS2TICKS(cl->ctokens),
  991. INT_MIN, INT_MAX);
  992. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  993. d, NULL, &cl->bstats) < 0 ||
  994. gnet_stats_copy_rate_est(d, NULL, &cl->rate_est) < 0 ||
  995. gnet_stats_copy_queue(d, NULL, &qs, qlen) < 0)
  996. return -1;
  997. return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
  998. }
  999. static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  1000. struct Qdisc **old)
  1001. {
  1002. struct htb_class *cl = (struct htb_class *)arg;
  1003. if (cl->level)
  1004. return -EINVAL;
  1005. if (new == NULL &&
  1006. (new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  1007. cl->common.classid)) == NULL)
  1008. return -ENOBUFS;
  1009. *old = qdisc_replace(sch, new, &cl->un.leaf.q);
  1010. return 0;
  1011. }
  1012. static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
  1013. {
  1014. struct htb_class *cl = (struct htb_class *)arg;
  1015. return !cl->level ? cl->un.leaf.q : NULL;
  1016. }
  1017. static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
  1018. {
  1019. struct htb_class *cl = (struct htb_class *)arg;
  1020. if (cl->un.leaf.q->q.qlen == 0)
  1021. htb_deactivate(qdisc_priv(sch), cl);
  1022. }
  1023. static unsigned long htb_get(struct Qdisc *sch, u32 classid)
  1024. {
  1025. struct htb_class *cl = htb_find(classid, sch);
  1026. if (cl)
  1027. cl->refcnt++;
  1028. return (unsigned long)cl;
  1029. }
  1030. static inline int htb_parent_last_child(struct htb_class *cl)
  1031. {
  1032. if (!cl->parent)
  1033. /* the root class */
  1034. return 0;
  1035. if (cl->parent->children > 1)
  1036. /* not the last child */
  1037. return 0;
  1038. return 1;
  1039. }
  1040. static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
  1041. struct Qdisc *new_q)
  1042. {
  1043. struct htb_class *parent = cl->parent;
  1044. WARN_ON(cl->level || !cl->un.leaf.q || cl->prio_activity);
  1045. if (parent->cmode != HTB_CAN_SEND)
  1046. htb_safe_rb_erase(&parent->pq_node,
  1047. &q->hlevel[parent->level].wait_pq);
  1048. parent->level = 0;
  1049. memset(&parent->un.inner, 0, sizeof(parent->un.inner));
  1050. INIT_LIST_HEAD(&parent->un.leaf.drop_list);
  1051. parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
  1052. parent->tokens = parent->buffer;
  1053. parent->ctokens = parent->cbuffer;
  1054. parent->t_c = ktime_get_ns();
  1055. parent->cmode = HTB_CAN_SEND;
  1056. }
  1057. static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
  1058. {
  1059. if (!cl->level) {
  1060. WARN_ON(!cl->un.leaf.q);
  1061. qdisc_destroy(cl->un.leaf.q);
  1062. }
  1063. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  1064. tcf_destroy_chain(&cl->filter_list);
  1065. kfree(cl);
  1066. }
  1067. static void htb_destroy(struct Qdisc *sch)
  1068. {
  1069. struct htb_sched *q = qdisc_priv(sch);
  1070. struct hlist_node *next;
  1071. struct htb_class *cl;
  1072. unsigned int i;
  1073. cancel_work_sync(&q->work);
  1074. qdisc_watchdog_cancel(&q->watchdog);
  1075. /* This line used to be after htb_destroy_class call below
  1076. * and surprisingly it worked in 2.4. But it must precede it
  1077. * because filter need its target class alive to be able to call
  1078. * unbind_filter on it (without Oops).
  1079. */
  1080. tcf_destroy_chain(&q->filter_list);
  1081. for (i = 0; i < q->clhash.hashsize; i++) {
  1082. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode)
  1083. tcf_destroy_chain(&cl->filter_list);
  1084. }
  1085. for (i = 0; i < q->clhash.hashsize; i++) {
  1086. hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
  1087. common.hnode)
  1088. htb_destroy_class(sch, cl);
  1089. }
  1090. qdisc_class_hash_destroy(&q->clhash);
  1091. __qdisc_reset_queue(&q->direct_queue);
  1092. }
  1093. static int htb_delete(struct Qdisc *sch, unsigned long arg)
  1094. {
  1095. struct htb_sched *q = qdisc_priv(sch);
  1096. struct htb_class *cl = (struct htb_class *)arg;
  1097. struct Qdisc *new_q = NULL;
  1098. int last_child = 0;
  1099. /* TODO: why don't allow to delete subtree ? references ? does
  1100. * tc subsys guarantee us that in htb_destroy it holds no class
  1101. * refs so that we can remove children safely there ?
  1102. */
  1103. if (cl->children || cl->filter_cnt)
  1104. return -EBUSY;
  1105. if (!cl->level && htb_parent_last_child(cl)) {
  1106. new_q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  1107. cl->parent->common.classid);
  1108. last_child = 1;
  1109. }
  1110. sch_tree_lock(sch);
  1111. if (!cl->level) {
  1112. unsigned int qlen = cl->un.leaf.q->q.qlen;
  1113. unsigned int backlog = cl->un.leaf.q->qstats.backlog;
  1114. qdisc_reset(cl->un.leaf.q);
  1115. qdisc_tree_reduce_backlog(cl->un.leaf.q, qlen, backlog);
  1116. }
  1117. /* delete from hash and active; remainder in destroy_class */
  1118. qdisc_class_hash_remove(&q->clhash, &cl->common);
  1119. if (cl->parent)
  1120. cl->parent->children--;
  1121. if (cl->prio_activity)
  1122. htb_deactivate(q, cl);
  1123. if (cl->cmode != HTB_CAN_SEND)
  1124. htb_safe_rb_erase(&cl->pq_node,
  1125. &q->hlevel[cl->level].wait_pq);
  1126. if (last_child)
  1127. htb_parent_to_leaf(q, cl, new_q);
  1128. BUG_ON(--cl->refcnt == 0);
  1129. /*
  1130. * This shouldn't happen: we "hold" one cops->get() when called
  1131. * from tc_ctl_tclass; the destroy method is done from cops->put().
  1132. */
  1133. sch_tree_unlock(sch);
  1134. return 0;
  1135. }
  1136. static void htb_put(struct Qdisc *sch, unsigned long arg)
  1137. {
  1138. struct htb_class *cl = (struct htb_class *)arg;
  1139. if (--cl->refcnt == 0)
  1140. htb_destroy_class(sch, cl);
  1141. }
  1142. static int htb_change_class(struct Qdisc *sch, u32 classid,
  1143. u32 parentid, struct nlattr **tca,
  1144. unsigned long *arg)
  1145. {
  1146. int err = -EINVAL;
  1147. struct htb_sched *q = qdisc_priv(sch);
  1148. struct htb_class *cl = (struct htb_class *)*arg, *parent;
  1149. struct nlattr *opt = tca[TCA_OPTIONS];
  1150. struct nlattr *tb[TCA_HTB_MAX + 1];
  1151. struct tc_htb_opt *hopt;
  1152. u64 rate64, ceil64;
  1153. /* extract all subattrs from opt attr */
  1154. if (!opt)
  1155. goto failure;
  1156. err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy);
  1157. if (err < 0)
  1158. goto failure;
  1159. err = -EINVAL;
  1160. if (tb[TCA_HTB_PARMS] == NULL)
  1161. goto failure;
  1162. parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
  1163. hopt = nla_data(tb[TCA_HTB_PARMS]);
  1164. if (!hopt->rate.rate || !hopt->ceil.rate)
  1165. goto failure;
  1166. /* Keeping backward compatible with rate_table based iproute2 tc */
  1167. if (hopt->rate.linklayer == TC_LINKLAYER_UNAWARE)
  1168. qdisc_put_rtab(qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB]));
  1169. if (hopt->ceil.linklayer == TC_LINKLAYER_UNAWARE)
  1170. qdisc_put_rtab(qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB]));
  1171. if (!cl) { /* new class */
  1172. struct Qdisc *new_q;
  1173. int prio;
  1174. struct {
  1175. struct nlattr nla;
  1176. struct gnet_estimator opt;
  1177. } est = {
  1178. .nla = {
  1179. .nla_len = nla_attr_size(sizeof(est.opt)),
  1180. .nla_type = TCA_RATE,
  1181. },
  1182. .opt = {
  1183. /* 4s interval, 16s averaging constant */
  1184. .interval = 2,
  1185. .ewma_log = 2,
  1186. },
  1187. };
  1188. /* check for valid classid */
  1189. if (!classid || TC_H_MAJ(classid ^ sch->handle) ||
  1190. htb_find(classid, sch))
  1191. goto failure;
  1192. /* check maximal depth */
  1193. if (parent && parent->parent && parent->parent->level < 2) {
  1194. pr_err("htb: tree is too deep\n");
  1195. goto failure;
  1196. }
  1197. err = -ENOBUFS;
  1198. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  1199. if (!cl)
  1200. goto failure;
  1201. if (htb_rate_est || tca[TCA_RATE]) {
  1202. err = gen_new_estimator(&cl->bstats, NULL,
  1203. &cl->rate_est,
  1204. NULL,
  1205. qdisc_root_sleeping_running(sch),
  1206. tca[TCA_RATE] ? : &est.nla);
  1207. if (err) {
  1208. kfree(cl);
  1209. goto failure;
  1210. }
  1211. }
  1212. cl->refcnt = 1;
  1213. cl->children = 0;
  1214. INIT_LIST_HEAD(&cl->un.leaf.drop_list);
  1215. RB_CLEAR_NODE(&cl->pq_node);
  1216. for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
  1217. RB_CLEAR_NODE(&cl->node[prio]);
  1218. /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
  1219. * so that can't be used inside of sch_tree_lock
  1220. * -- thanks to Karlis Peisenieks
  1221. */
  1222. new_q = qdisc_create_dflt(sch->dev_queue,
  1223. &pfifo_qdisc_ops, classid);
  1224. sch_tree_lock(sch);
  1225. if (parent && !parent->level) {
  1226. unsigned int qlen = parent->un.leaf.q->q.qlen;
  1227. unsigned int backlog = parent->un.leaf.q->qstats.backlog;
  1228. /* turn parent into inner node */
  1229. qdisc_reset(parent->un.leaf.q);
  1230. qdisc_tree_reduce_backlog(parent->un.leaf.q, qlen, backlog);
  1231. qdisc_destroy(parent->un.leaf.q);
  1232. if (parent->prio_activity)
  1233. htb_deactivate(q, parent);
  1234. /* remove from evt list because of level change */
  1235. if (parent->cmode != HTB_CAN_SEND) {
  1236. htb_safe_rb_erase(&parent->pq_node, &q->hlevel[0].wait_pq);
  1237. parent->cmode = HTB_CAN_SEND;
  1238. }
  1239. parent->level = (parent->parent ? parent->parent->level
  1240. : TC_HTB_MAXDEPTH) - 1;
  1241. memset(&parent->un.inner, 0, sizeof(parent->un.inner));
  1242. }
  1243. /* leaf (we) needs elementary qdisc */
  1244. cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
  1245. cl->common.classid = classid;
  1246. cl->parent = parent;
  1247. /* set class to be in HTB_CAN_SEND state */
  1248. cl->tokens = PSCHED_TICKS2NS(hopt->buffer);
  1249. cl->ctokens = PSCHED_TICKS2NS(hopt->cbuffer);
  1250. cl->mbuffer = 60ULL * NSEC_PER_SEC; /* 1min */
  1251. cl->t_c = ktime_get_ns();
  1252. cl->cmode = HTB_CAN_SEND;
  1253. /* attach to the hash list and parent's family */
  1254. qdisc_class_hash_insert(&q->clhash, &cl->common);
  1255. if (parent)
  1256. parent->children++;
  1257. } else {
  1258. if (tca[TCA_RATE]) {
  1259. err = gen_replace_estimator(&cl->bstats, NULL,
  1260. &cl->rate_est,
  1261. NULL,
  1262. qdisc_root_sleeping_running(sch),
  1263. tca[TCA_RATE]);
  1264. if (err)
  1265. return err;
  1266. }
  1267. sch_tree_lock(sch);
  1268. }
  1269. rate64 = tb[TCA_HTB_RATE64] ? nla_get_u64(tb[TCA_HTB_RATE64]) : 0;
  1270. ceil64 = tb[TCA_HTB_CEIL64] ? nla_get_u64(tb[TCA_HTB_CEIL64]) : 0;
  1271. psched_ratecfg_precompute(&cl->rate, &hopt->rate, rate64);
  1272. psched_ratecfg_precompute(&cl->ceil, &hopt->ceil, ceil64);
  1273. /* it used to be a nasty bug here, we have to check that node
  1274. * is really leaf before changing cl->un.leaf !
  1275. */
  1276. if (!cl->level) {
  1277. u64 quantum = cl->rate.rate_bytes_ps;
  1278. do_div(quantum, q->rate2quantum);
  1279. cl->quantum = min_t(u64, quantum, INT_MAX);
  1280. if (!hopt->quantum && cl->quantum < 1000) {
  1281. pr_warn("HTB: quantum of class %X is small. Consider r2q change.\n",
  1282. cl->common.classid);
  1283. cl->quantum = 1000;
  1284. }
  1285. if (!hopt->quantum && cl->quantum > 200000) {
  1286. pr_warn("HTB: quantum of class %X is big. Consider r2q change.\n",
  1287. cl->common.classid);
  1288. cl->quantum = 200000;
  1289. }
  1290. if (hopt->quantum)
  1291. cl->quantum = hopt->quantum;
  1292. if ((cl->prio = hopt->prio) >= TC_HTB_NUMPRIO)
  1293. cl->prio = TC_HTB_NUMPRIO - 1;
  1294. }
  1295. cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
  1296. cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer);
  1297. sch_tree_unlock(sch);
  1298. qdisc_class_hash_grow(sch, &q->clhash);
  1299. *arg = (unsigned long)cl;
  1300. return 0;
  1301. failure:
  1302. return err;
  1303. }
  1304. static struct tcf_proto __rcu **htb_find_tcf(struct Qdisc *sch,
  1305. unsigned long arg)
  1306. {
  1307. struct htb_sched *q = qdisc_priv(sch);
  1308. struct htb_class *cl = (struct htb_class *)arg;
  1309. struct tcf_proto __rcu **fl = cl ? &cl->filter_list : &q->filter_list;
  1310. return fl;
  1311. }
  1312. static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
  1313. u32 classid)
  1314. {
  1315. struct htb_class *cl = htb_find(classid, sch);
  1316. /*if (cl && !cl->level) return 0;
  1317. * The line above used to be there to prevent attaching filters to
  1318. * leaves. But at least tc_index filter uses this just to get class
  1319. * for other reasons so that we have to allow for it.
  1320. * ----
  1321. * 19.6.2002 As Werner explained it is ok - bind filter is just
  1322. * another way to "lock" the class - unlike "get" this lock can
  1323. * be broken by class during destroy IIUC.
  1324. */
  1325. if (cl)
  1326. cl->filter_cnt++;
  1327. return (unsigned long)cl;
  1328. }
  1329. static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
  1330. {
  1331. struct htb_class *cl = (struct htb_class *)arg;
  1332. if (cl)
  1333. cl->filter_cnt--;
  1334. }
  1335. static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  1336. {
  1337. struct htb_sched *q = qdisc_priv(sch);
  1338. struct htb_class *cl;
  1339. unsigned int i;
  1340. if (arg->stop)
  1341. return;
  1342. for (i = 0; i < q->clhash.hashsize; i++) {
  1343. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  1344. if (arg->count < arg->skip) {
  1345. arg->count++;
  1346. continue;
  1347. }
  1348. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  1349. arg->stop = 1;
  1350. return;
  1351. }
  1352. arg->count++;
  1353. }
  1354. }
  1355. }
  1356. static const struct Qdisc_class_ops htb_class_ops = {
  1357. .graft = htb_graft,
  1358. .leaf = htb_leaf,
  1359. .qlen_notify = htb_qlen_notify,
  1360. .get = htb_get,
  1361. .put = htb_put,
  1362. .change = htb_change_class,
  1363. .delete = htb_delete,
  1364. .walk = htb_walk,
  1365. .tcf_chain = htb_find_tcf,
  1366. .bind_tcf = htb_bind_filter,
  1367. .unbind_tcf = htb_unbind_filter,
  1368. .dump = htb_dump_class,
  1369. .dump_stats = htb_dump_class_stats,
  1370. };
  1371. static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
  1372. .cl_ops = &htb_class_ops,
  1373. .id = "htb",
  1374. .priv_size = sizeof(struct htb_sched),
  1375. .enqueue = htb_enqueue,
  1376. .dequeue = htb_dequeue,
  1377. .peek = qdisc_peek_dequeued,
  1378. .init = htb_init,
  1379. .reset = htb_reset,
  1380. .destroy = htb_destroy,
  1381. .dump = htb_dump,
  1382. .owner = THIS_MODULE,
  1383. };
  1384. static int __init htb_module_init(void)
  1385. {
  1386. return register_qdisc(&htb_qdisc_ops);
  1387. }
  1388. static void __exit htb_module_exit(void)
  1389. {
  1390. unregister_qdisc(&htb_qdisc_ops);
  1391. }
  1392. module_init(htb_module_init)
  1393. module_exit(htb_module_exit)
  1394. MODULE_LICENSE("GPL");