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