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