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