PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/net/sched/sch_htb.c

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