sch_htb.c 41 KB

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