stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/sch_qfq.c Quick Fair Queueing Scheduler. |
| 3 | * |
| 4 | * Copyright (c) 2009 Fabio Checconi, Luigi Rizzo, and Paolo Valente. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/pkt_sched.h> |
| 17 | #include <net/sch_generic.h> |
| 18 | #include <net/pkt_sched.h> |
| 19 | #include <net/pkt_cls.h> |
| 20 | |
| 21 | |
| 22 | /* Quick Fair Queueing |
| 23 | =================== |
| 24 | |
| 25 | Sources: |
| 26 | |
| 27 | Fabio Checconi, Luigi Rizzo, and Paolo Valente: "QFQ: Efficient |
| 28 | Packet Scheduling with Tight Bandwidth Distribution Guarantees." |
| 29 | |
| 30 | See also: |
| 31 | http://retis.sssup.it/~fabio/linux/qfq/ |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | |
| 36 | Virtual time computations. |
| 37 | |
| 38 | S, F and V are all computed in fixed point arithmetic with |
| 39 | FRAC_BITS decimal bits. |
| 40 | |
| 41 | QFQ_MAX_INDEX is the maximum index allowed for a group. We need |
| 42 | one bit per index. |
| 43 | QFQ_MAX_WSHIFT is the maximum power of two supported as a weight. |
| 44 | |
| 45 | The layout of the bits is as below: |
| 46 | |
| 47 | [ MTU_SHIFT ][ FRAC_BITS ] |
| 48 | [ MAX_INDEX ][ MIN_SLOT_SHIFT ] |
| 49 | ^.__grp->index = 0 |
| 50 | *.__grp->slot_shift |
| 51 | |
| 52 | where MIN_SLOT_SHIFT is derived by difference from the others. |
| 53 | |
| 54 | The max group index corresponds to Lmax/w_min, where |
| 55 | Lmax=1<<MTU_SHIFT, w_min = 1 . |
| 56 | From this, and knowing how many groups (MAX_INDEX) we want, |
| 57 | we can derive the shift corresponding to each group. |
| 58 | |
| 59 | Because we often need to compute |
| 60 | F = S + len/w_i and V = V + len/wsum |
| 61 | instead of storing w_i store the value |
| 62 | inv_w = (1<<FRAC_BITS)/w_i |
| 63 | so we can do F = S + len * inv_w * wsum. |
| 64 | We use W_TOT in the formulas so we can easily move between |
| 65 | static and adaptive weight sum. |
| 66 | |
| 67 | The per-scheduler-instance data contain all the data structures |
| 68 | for the scheduler: bitmaps and bucket lists. |
| 69 | |
| 70 | */ |
| 71 | |
| 72 | /* |
| 73 | * Maximum number of consecutive slots occupied by backlogged classes |
| 74 | * inside a group. |
| 75 | */ |
| 76 | #define QFQ_MAX_SLOTS 32 |
| 77 | |
| 78 | /* |
| 79 | * Shifts used for class<->group mapping. We allow class weights that are |
| 80 | * in the range [1, 2^MAX_WSHIFT], and we try to map each class i to the |
| 81 | * group with the smallest index that can support the L_i / r_i configured |
| 82 | * for the class. |
| 83 | * |
| 84 | * grp->index is the index of the group; and grp->slot_shift |
| 85 | * is the shift for the corresponding (scaled) sigma_i. |
| 86 | */ |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 87 | #define QFQ_MAX_INDEX 24 |
| 88 | #define QFQ_MAX_WSHIFT 12 |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 89 | |
| 90 | #define QFQ_MAX_WEIGHT (1<<QFQ_MAX_WSHIFT) |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 91 | #define QFQ_MAX_WSUM (16*QFQ_MAX_WEIGHT) |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 92 | |
| 93 | #define FRAC_BITS 30 /* fixed point arithmetic */ |
| 94 | #define ONE_FP (1UL << FRAC_BITS) |
| 95 | #define IWSUM (ONE_FP/QFQ_MAX_WSUM) |
| 96 | |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 97 | #define QFQ_MTU_SHIFT 16 /* to support TSO/GSO */ |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 98 | #define QFQ_MIN_SLOT_SHIFT (FRAC_BITS + QFQ_MTU_SHIFT - QFQ_MAX_INDEX) |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 99 | #define QFQ_MIN_LMAX 256 /* min possible lmax for a class */ |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * Possible group states. These values are used as indexes for the bitmaps |
| 103 | * array of struct qfq_queue. |
| 104 | */ |
| 105 | enum qfq_state { ER, IR, EB, IB, QFQ_MAX_STATE }; |
| 106 | |
| 107 | struct qfq_group; |
| 108 | |
| 109 | struct qfq_class { |
| 110 | struct Qdisc_class_common common; |
| 111 | |
| 112 | unsigned int refcnt; |
| 113 | unsigned int filter_cnt; |
| 114 | |
| 115 | struct gnet_stats_basic_packed bstats; |
| 116 | struct gnet_stats_queue qstats; |
| 117 | struct gnet_stats_rate_est rate_est; |
| 118 | struct Qdisc *qdisc; |
| 119 | |
| 120 | struct hlist_node next; /* Link for the slot list. */ |
| 121 | u64 S, F; /* flow timestamps (exact) */ |
| 122 | |
| 123 | /* group we belong to. In principle we would need the index, |
| 124 | * which is log_2(lmax/weight), but we never reference it |
| 125 | * directly, only the group. |
| 126 | */ |
| 127 | struct qfq_group *grp; |
| 128 | |
| 129 | /* these are copied from the flowset. */ |
| 130 | u32 inv_w; /* ONE_FP/weight */ |
| 131 | u32 lmax; /* Max packet size for this flow. */ |
| 132 | }; |
| 133 | |
| 134 | struct qfq_group { |
| 135 | u64 S, F; /* group timestamps (approx). */ |
| 136 | unsigned int slot_shift; /* Slot shift. */ |
| 137 | unsigned int index; /* Group index. */ |
| 138 | unsigned int front; /* Index of the front slot. */ |
| 139 | unsigned long full_slots; /* non-empty slots */ |
| 140 | |
| 141 | /* Array of RR lists of active classes. */ |
| 142 | struct hlist_head slots[QFQ_MAX_SLOTS]; |
| 143 | }; |
| 144 | |
| 145 | struct qfq_sched { |
| 146 | struct tcf_proto *filter_list; |
| 147 | struct Qdisc_class_hash clhash; |
| 148 | |
| 149 | u64 V; /* Precise virtual time. */ |
| 150 | u32 wsum; /* weight sum */ |
| 151 | |
| 152 | unsigned long bitmaps[QFQ_MAX_STATE]; /* Group bitmaps. */ |
| 153 | struct qfq_group groups[QFQ_MAX_INDEX + 1]; /* The groups. */ |
| 154 | }; |
| 155 | |
| 156 | static struct qfq_class *qfq_find_class(struct Qdisc *sch, u32 classid) |
| 157 | { |
| 158 | struct qfq_sched *q = qdisc_priv(sch); |
| 159 | struct Qdisc_class_common *clc; |
| 160 | |
| 161 | clc = qdisc_class_find(&q->clhash, classid); |
| 162 | if (clc == NULL) |
| 163 | return NULL; |
| 164 | return container_of(clc, struct qfq_class, common); |
| 165 | } |
| 166 | |
| 167 | static void qfq_purge_queue(struct qfq_class *cl) |
| 168 | { |
| 169 | unsigned int len = cl->qdisc->q.qlen; |
| 170 | |
| 171 | qdisc_reset(cl->qdisc); |
| 172 | qdisc_tree_decrease_qlen(cl->qdisc, len); |
| 173 | } |
| 174 | |
| 175 | static const struct nla_policy qfq_policy[TCA_QFQ_MAX + 1] = { |
| 176 | [TCA_QFQ_WEIGHT] = { .type = NLA_U32 }, |
| 177 | [TCA_QFQ_LMAX] = { .type = NLA_U32 }, |
| 178 | }; |
| 179 | |
| 180 | /* |
| 181 | * Calculate a flow index, given its weight and maximum packet length. |
| 182 | * index = log_2(maxlen/weight) but we need to apply the scaling. |
| 183 | * This is used only once at flow creation. |
| 184 | */ |
| 185 | static int qfq_calc_index(u32 inv_w, unsigned int maxlen) |
| 186 | { |
| 187 | u64 slot_size = (u64)maxlen * inv_w; |
| 188 | unsigned long size_map; |
| 189 | int index = 0; |
| 190 | |
| 191 | size_map = slot_size >> QFQ_MIN_SLOT_SHIFT; |
| 192 | if (!size_map) |
| 193 | goto out; |
| 194 | |
| 195 | index = __fls(size_map) + 1; /* basically a log_2 */ |
| 196 | index -= !(slot_size - (1ULL << (index + QFQ_MIN_SLOT_SHIFT - 1))); |
| 197 | |
| 198 | if (index < 0) |
| 199 | index = 0; |
| 200 | out: |
| 201 | pr_debug("qfq calc_index: W = %lu, L = %u, I = %d\n", |
| 202 | (unsigned long) ONE_FP/inv_w, maxlen, index); |
| 203 | |
| 204 | return index; |
| 205 | } |
| 206 | |
Paolo Valente | be72f63 | 2012-08-07 07:27:25 +0000 | [diff] [blame] | 207 | /* Length of the next packet (0 if the queue is empty). */ |
| 208 | static unsigned int qdisc_peek_len(struct Qdisc *sch) |
| 209 | { |
| 210 | struct sk_buff *skb; |
| 211 | |
| 212 | skb = sch->ops->peek(sch); |
| 213 | return skb ? qdisc_pkt_len(skb) : 0; |
| 214 | } |
| 215 | |
| 216 | static void qfq_deactivate_class(struct qfq_sched *, struct qfq_class *); |
| 217 | static void qfq_activate_class(struct qfq_sched *q, struct qfq_class *cl, |
| 218 | unsigned int len); |
| 219 | |
| 220 | static void qfq_update_class_params(struct qfq_sched *q, struct qfq_class *cl, |
| 221 | u32 lmax, u32 inv_w, int delta_w) |
| 222 | { |
| 223 | int i; |
| 224 | |
| 225 | /* update qfq-specific data */ |
| 226 | cl->lmax = lmax; |
| 227 | cl->inv_w = inv_w; |
| 228 | i = qfq_calc_index(cl->inv_w, cl->lmax); |
| 229 | |
| 230 | cl->grp = &q->groups[i]; |
| 231 | |
| 232 | q->wsum += delta_w; |
| 233 | } |
| 234 | |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 235 | static void qfq_update_reactivate_class(struct qfq_sched *q, |
| 236 | struct qfq_class *cl, |
| 237 | u32 inv_w, u32 lmax, int delta_w) |
| 238 | { |
| 239 | bool need_reactivation = false; |
| 240 | int i = qfq_calc_index(inv_w, lmax); |
| 241 | |
| 242 | if (&q->groups[i] != cl->grp && cl->qdisc->q.qlen > 0) { |
| 243 | /* |
| 244 | * shift cl->F back, to not charge the |
| 245 | * class for the not-yet-served head |
| 246 | * packet |
| 247 | */ |
| 248 | cl->F = cl->S; |
| 249 | /* remove class from its slot in the old group */ |
| 250 | qfq_deactivate_class(q, cl); |
| 251 | need_reactivation = true; |
| 252 | } |
| 253 | |
| 254 | qfq_update_class_params(q, cl, lmax, inv_w, delta_w); |
| 255 | |
| 256 | if (need_reactivation) /* activate in new group */ |
| 257 | qfq_activate_class(q, cl, qdisc_peek_len(cl->qdisc)); |
| 258 | } |
| 259 | |
| 260 | |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 261 | static int qfq_change_class(struct Qdisc *sch, u32 classid, u32 parentid, |
| 262 | struct nlattr **tca, unsigned long *arg) |
| 263 | { |
| 264 | struct qfq_sched *q = qdisc_priv(sch); |
| 265 | struct qfq_class *cl = (struct qfq_class *)*arg; |
| 266 | struct nlattr *tb[TCA_QFQ_MAX + 1]; |
| 267 | u32 weight, lmax, inv_w; |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 268 | int err; |
Eric Dumazet | d32ae76f | 2012-01-02 11:47:50 +0000 | [diff] [blame] | 269 | int delta_w; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 270 | |
| 271 | if (tca[TCA_OPTIONS] == NULL) { |
| 272 | pr_notice("qfq: no options\n"); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
| 276 | err = nla_parse_nested(tb, TCA_QFQ_MAX, tca[TCA_OPTIONS], qfq_policy); |
| 277 | if (err < 0) |
| 278 | return err; |
| 279 | |
| 280 | if (tb[TCA_QFQ_WEIGHT]) { |
| 281 | weight = nla_get_u32(tb[TCA_QFQ_WEIGHT]); |
| 282 | if (!weight || weight > (1UL << QFQ_MAX_WSHIFT)) { |
| 283 | pr_notice("qfq: invalid weight %u\n", weight); |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | } else |
| 287 | weight = 1; |
| 288 | |
| 289 | inv_w = ONE_FP / weight; |
| 290 | weight = ONE_FP / inv_w; |
Eric Dumazet | d32ae76f | 2012-01-02 11:47:50 +0000 | [diff] [blame] | 291 | delta_w = weight - (cl ? ONE_FP / cl->inv_w : 0); |
| 292 | if (q->wsum + delta_w > QFQ_MAX_WSUM) { |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 293 | pr_notice("qfq: total weight out of range (%u + %u)\n", |
Eric Dumazet | d32ae76f | 2012-01-02 11:47:50 +0000 | [diff] [blame] | 294 | delta_w, q->wsum); |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | if (tb[TCA_QFQ_LMAX]) { |
| 299 | lmax = nla_get_u32(tb[TCA_QFQ_LMAX]); |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 300 | if (lmax < QFQ_MIN_LMAX || lmax > (1UL << QFQ_MTU_SHIFT)) { |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 301 | pr_notice("qfq: invalid max length %u\n", lmax); |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | } else |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 305 | lmax = psched_mtu(qdisc_dev(sch)); |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 306 | |
| 307 | if (cl != NULL) { |
| 308 | if (tca[TCA_RATE]) { |
| 309 | err = gen_replace_estimator(&cl->bstats, &cl->rate_est, |
| 310 | qdisc_root_sleeping_lock(sch), |
| 311 | tca[TCA_RATE]); |
| 312 | if (err) |
| 313 | return err; |
| 314 | } |
| 315 | |
Paolo Valente | be72f63 | 2012-08-07 07:27:25 +0000 | [diff] [blame] | 316 | if (lmax == cl->lmax && inv_w == cl->inv_w) |
| 317 | return 0; /* nothing to update */ |
| 318 | |
Paolo Valente | be72f63 | 2012-08-07 07:27:25 +0000 | [diff] [blame] | 319 | sch_tree_lock(sch); |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 320 | qfq_update_reactivate_class(q, cl, inv_w, lmax, delta_w); |
Paolo Valente | be72f63 | 2012-08-07 07:27:25 +0000 | [diff] [blame] | 321 | sch_tree_unlock(sch); |
| 322 | |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | cl = kzalloc(sizeof(struct qfq_class), GFP_KERNEL); |
| 327 | if (cl == NULL) |
| 328 | return -ENOBUFS; |
| 329 | |
| 330 | cl->refcnt = 1; |
| 331 | cl->common.classid = classid; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 332 | |
Paolo Valente | be72f63 | 2012-08-07 07:27:25 +0000 | [diff] [blame] | 333 | qfq_update_class_params(q, cl, lmax, inv_w, delta_w); |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 334 | |
| 335 | cl->qdisc = qdisc_create_dflt(sch->dev_queue, |
| 336 | &pfifo_qdisc_ops, classid); |
| 337 | if (cl->qdisc == NULL) |
| 338 | cl->qdisc = &noop_qdisc; |
| 339 | |
| 340 | if (tca[TCA_RATE]) { |
| 341 | err = gen_new_estimator(&cl->bstats, &cl->rate_est, |
| 342 | qdisc_root_sleeping_lock(sch), |
| 343 | tca[TCA_RATE]); |
| 344 | if (err) { |
| 345 | qdisc_destroy(cl->qdisc); |
| 346 | kfree(cl); |
| 347 | return err; |
| 348 | } |
| 349 | } |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 350 | |
| 351 | sch_tree_lock(sch); |
| 352 | qdisc_class_hash_insert(&q->clhash, &cl->common); |
| 353 | sch_tree_unlock(sch); |
| 354 | |
| 355 | qdisc_class_hash_grow(sch, &q->clhash); |
| 356 | |
| 357 | *arg = (unsigned long)cl; |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static void qfq_destroy_class(struct Qdisc *sch, struct qfq_class *cl) |
| 362 | { |
| 363 | struct qfq_sched *q = qdisc_priv(sch); |
| 364 | |
| 365 | if (cl->inv_w) { |
| 366 | q->wsum -= ONE_FP / cl->inv_w; |
| 367 | cl->inv_w = 0; |
| 368 | } |
| 369 | |
| 370 | gen_kill_estimator(&cl->bstats, &cl->rate_est); |
| 371 | qdisc_destroy(cl->qdisc); |
| 372 | kfree(cl); |
| 373 | } |
| 374 | |
| 375 | static int qfq_delete_class(struct Qdisc *sch, unsigned long arg) |
| 376 | { |
| 377 | struct qfq_sched *q = qdisc_priv(sch); |
| 378 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 379 | |
| 380 | if (cl->filter_cnt > 0) |
| 381 | return -EBUSY; |
| 382 | |
| 383 | sch_tree_lock(sch); |
| 384 | |
| 385 | qfq_purge_queue(cl); |
| 386 | qdisc_class_hash_remove(&q->clhash, &cl->common); |
| 387 | |
| 388 | BUG_ON(--cl->refcnt == 0); |
| 389 | /* |
| 390 | * This shouldn't happen: we "hold" one cops->get() when called |
| 391 | * from tc_ctl_tclass; the destroy method is done from cops->put(). |
| 392 | */ |
| 393 | |
| 394 | sch_tree_unlock(sch); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static unsigned long qfq_get_class(struct Qdisc *sch, u32 classid) |
| 399 | { |
| 400 | struct qfq_class *cl = qfq_find_class(sch, classid); |
| 401 | |
| 402 | if (cl != NULL) |
| 403 | cl->refcnt++; |
| 404 | |
| 405 | return (unsigned long)cl; |
| 406 | } |
| 407 | |
| 408 | static void qfq_put_class(struct Qdisc *sch, unsigned long arg) |
| 409 | { |
| 410 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 411 | |
| 412 | if (--cl->refcnt == 0) |
| 413 | qfq_destroy_class(sch, cl); |
| 414 | } |
| 415 | |
| 416 | static struct tcf_proto **qfq_tcf_chain(struct Qdisc *sch, unsigned long cl) |
| 417 | { |
| 418 | struct qfq_sched *q = qdisc_priv(sch); |
| 419 | |
| 420 | if (cl) |
| 421 | return NULL; |
| 422 | |
| 423 | return &q->filter_list; |
| 424 | } |
| 425 | |
| 426 | static unsigned long qfq_bind_tcf(struct Qdisc *sch, unsigned long parent, |
| 427 | u32 classid) |
| 428 | { |
| 429 | struct qfq_class *cl = qfq_find_class(sch, classid); |
| 430 | |
| 431 | if (cl != NULL) |
| 432 | cl->filter_cnt++; |
| 433 | |
| 434 | return (unsigned long)cl; |
| 435 | } |
| 436 | |
| 437 | static void qfq_unbind_tcf(struct Qdisc *sch, unsigned long arg) |
| 438 | { |
| 439 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 440 | |
| 441 | cl->filter_cnt--; |
| 442 | } |
| 443 | |
| 444 | static int qfq_graft_class(struct Qdisc *sch, unsigned long arg, |
| 445 | struct Qdisc *new, struct Qdisc **old) |
| 446 | { |
| 447 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 448 | |
| 449 | if (new == NULL) { |
| 450 | new = qdisc_create_dflt(sch->dev_queue, |
| 451 | &pfifo_qdisc_ops, cl->common.classid); |
| 452 | if (new == NULL) |
| 453 | new = &noop_qdisc; |
| 454 | } |
| 455 | |
| 456 | sch_tree_lock(sch); |
| 457 | qfq_purge_queue(cl); |
| 458 | *old = cl->qdisc; |
| 459 | cl->qdisc = new; |
| 460 | sch_tree_unlock(sch); |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static struct Qdisc *qfq_class_leaf(struct Qdisc *sch, unsigned long arg) |
| 465 | { |
| 466 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 467 | |
| 468 | return cl->qdisc; |
| 469 | } |
| 470 | |
| 471 | static int qfq_dump_class(struct Qdisc *sch, unsigned long arg, |
| 472 | struct sk_buff *skb, struct tcmsg *tcm) |
| 473 | { |
| 474 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 475 | struct nlattr *nest; |
| 476 | |
| 477 | tcm->tcm_parent = TC_H_ROOT; |
| 478 | tcm->tcm_handle = cl->common.classid; |
| 479 | tcm->tcm_info = cl->qdisc->handle; |
| 480 | |
| 481 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 482 | if (nest == NULL) |
| 483 | goto nla_put_failure; |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 484 | if (nla_put_u32(skb, TCA_QFQ_WEIGHT, ONE_FP/cl->inv_w) || |
| 485 | nla_put_u32(skb, TCA_QFQ_LMAX, cl->lmax)) |
| 486 | goto nla_put_failure; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 487 | return nla_nest_end(skb, nest); |
| 488 | |
| 489 | nla_put_failure: |
| 490 | nla_nest_cancel(skb, nest); |
| 491 | return -EMSGSIZE; |
| 492 | } |
| 493 | |
| 494 | static int qfq_dump_class_stats(struct Qdisc *sch, unsigned long arg, |
| 495 | struct gnet_dump *d) |
| 496 | { |
| 497 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 498 | struct tc_qfq_stats xstats; |
| 499 | |
| 500 | memset(&xstats, 0, sizeof(xstats)); |
| 501 | cl->qdisc->qstats.qlen = cl->qdisc->q.qlen; |
| 502 | |
| 503 | xstats.weight = ONE_FP/cl->inv_w; |
| 504 | xstats.lmax = cl->lmax; |
| 505 | |
| 506 | if (gnet_stats_copy_basic(d, &cl->bstats) < 0 || |
| 507 | gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 || |
| 508 | gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0) |
| 509 | return -1; |
| 510 | |
| 511 | return gnet_stats_copy_app(d, &xstats, sizeof(xstats)); |
| 512 | } |
| 513 | |
| 514 | static void qfq_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 515 | { |
| 516 | struct qfq_sched *q = qdisc_priv(sch); |
| 517 | struct qfq_class *cl; |
| 518 | struct hlist_node *n; |
| 519 | unsigned int i; |
| 520 | |
| 521 | if (arg->stop) |
| 522 | return; |
| 523 | |
| 524 | for (i = 0; i < q->clhash.hashsize; i++) { |
| 525 | hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) { |
| 526 | if (arg->count < arg->skip) { |
| 527 | arg->count++; |
| 528 | continue; |
| 529 | } |
| 530 | if (arg->fn(sch, (unsigned long)cl, arg) < 0) { |
| 531 | arg->stop = 1; |
| 532 | return; |
| 533 | } |
| 534 | arg->count++; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch, |
| 540 | int *qerr) |
| 541 | { |
| 542 | struct qfq_sched *q = qdisc_priv(sch); |
| 543 | struct qfq_class *cl; |
| 544 | struct tcf_result res; |
| 545 | int result; |
| 546 | |
| 547 | if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) { |
| 548 | pr_debug("qfq_classify: found %d\n", skb->priority); |
| 549 | cl = qfq_find_class(sch, skb->priority); |
| 550 | if (cl != NULL) |
| 551 | return cl; |
| 552 | } |
| 553 | |
| 554 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; |
| 555 | result = tc_classify(skb, q->filter_list, &res); |
| 556 | if (result >= 0) { |
| 557 | #ifdef CONFIG_NET_CLS_ACT |
| 558 | switch (result) { |
| 559 | case TC_ACT_QUEUED: |
| 560 | case TC_ACT_STOLEN: |
| 561 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; |
| 562 | case TC_ACT_SHOT: |
| 563 | return NULL; |
| 564 | } |
| 565 | #endif |
| 566 | cl = (struct qfq_class *)res.class; |
| 567 | if (cl == NULL) |
| 568 | cl = qfq_find_class(sch, res.classid); |
| 569 | return cl; |
| 570 | } |
| 571 | |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | /* Generic comparison function, handling wraparound. */ |
| 576 | static inline int qfq_gt(u64 a, u64 b) |
| 577 | { |
| 578 | return (s64)(a - b) > 0; |
| 579 | } |
| 580 | |
| 581 | /* Round a precise timestamp to its slotted value. */ |
| 582 | static inline u64 qfq_round_down(u64 ts, unsigned int shift) |
| 583 | { |
| 584 | return ts & ~((1ULL << shift) - 1); |
| 585 | } |
| 586 | |
| 587 | /* return the pointer to the group with lowest index in the bitmap */ |
| 588 | static inline struct qfq_group *qfq_ffs(struct qfq_sched *q, |
| 589 | unsigned long bitmap) |
| 590 | { |
| 591 | int index = __ffs(bitmap); |
| 592 | return &q->groups[index]; |
| 593 | } |
| 594 | /* Calculate a mask to mimic what would be ffs_from(). */ |
| 595 | static inline unsigned long mask_from(unsigned long bitmap, int from) |
| 596 | { |
| 597 | return bitmap & ~((1UL << from) - 1); |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * The state computation relies on ER=0, IR=1, EB=2, IB=3 |
| 602 | * First compute eligibility comparing grp->S, q->V, |
| 603 | * then check if someone is blocking us and possibly add EB |
| 604 | */ |
| 605 | static int qfq_calc_state(struct qfq_sched *q, const struct qfq_group *grp) |
| 606 | { |
| 607 | /* if S > V we are not eligible */ |
| 608 | unsigned int state = qfq_gt(grp->S, q->V); |
| 609 | unsigned long mask = mask_from(q->bitmaps[ER], grp->index); |
| 610 | struct qfq_group *next; |
| 611 | |
| 612 | if (mask) { |
| 613 | next = qfq_ffs(q, mask); |
| 614 | if (qfq_gt(grp->F, next->F)) |
| 615 | state |= EB; |
| 616 | } |
| 617 | |
| 618 | return state; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | /* |
| 623 | * In principle |
| 624 | * q->bitmaps[dst] |= q->bitmaps[src] & mask; |
| 625 | * q->bitmaps[src] &= ~mask; |
| 626 | * but we should make sure that src != dst |
| 627 | */ |
| 628 | static inline void qfq_move_groups(struct qfq_sched *q, unsigned long mask, |
| 629 | int src, int dst) |
| 630 | { |
| 631 | q->bitmaps[dst] |= q->bitmaps[src] & mask; |
| 632 | q->bitmaps[src] &= ~mask; |
| 633 | } |
| 634 | |
| 635 | static void qfq_unblock_groups(struct qfq_sched *q, int index, u64 old_F) |
| 636 | { |
| 637 | unsigned long mask = mask_from(q->bitmaps[ER], index + 1); |
| 638 | struct qfq_group *next; |
| 639 | |
| 640 | if (mask) { |
| 641 | next = qfq_ffs(q, mask); |
| 642 | if (!qfq_gt(next->F, old_F)) |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | mask = (1UL << index) - 1; |
| 647 | qfq_move_groups(q, mask, EB, ER); |
| 648 | qfq_move_groups(q, mask, IB, IR); |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * perhaps |
| 653 | * |
| 654 | old_V ^= q->V; |
| 655 | old_V >>= QFQ_MIN_SLOT_SHIFT; |
| 656 | if (old_V) { |
| 657 | ... |
| 658 | } |
| 659 | * |
| 660 | */ |
| 661 | static void qfq_make_eligible(struct qfq_sched *q, u64 old_V) |
| 662 | { |
| 663 | unsigned long vslot = q->V >> QFQ_MIN_SLOT_SHIFT; |
| 664 | unsigned long old_vslot = old_V >> QFQ_MIN_SLOT_SHIFT; |
| 665 | |
| 666 | if (vslot != old_vslot) { |
| 667 | unsigned long mask = (1UL << fls(vslot ^ old_vslot)) - 1; |
| 668 | qfq_move_groups(q, mask, IR, ER); |
| 669 | qfq_move_groups(q, mask, IB, EB); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | |
| 674 | /* |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 675 | * If the weight and lmax (max_pkt_size) of the classes do not change, |
| 676 | * then QFQ guarantees that the slot index is never higher than |
| 677 | * 2 + ((1<<QFQ_MTU_SHIFT)/QFQ_MIN_LMAX) * (QFQ_MAX_WEIGHT/QFQ_MAX_WSUM). |
| 678 | * |
| 679 | * With the current values of the above constants, the index is |
| 680 | * then guaranteed to never be higher than 2 + 256 * (1 / 16) = 18. |
| 681 | * |
| 682 | * When the weight of a class is increased or the lmax of the class is |
| 683 | * decreased, a new class with smaller slot size may happen to be |
| 684 | * activated. The activation of this class should be properly delayed |
| 685 | * to when the service of the class has finished in the ideal system |
| 686 | * tracked by QFQ. If the activation of the class is not delayed to |
| 687 | * this reference time instant, then this class may be unjustly served |
| 688 | * before other classes waiting for service. This may cause |
| 689 | * (unfrequently) the above bound to the slot index to be violated for |
| 690 | * some of these unlucky classes. |
| 691 | * |
| 692 | * Instead of delaying the activation of the new class, which is quite |
| 693 | * complex, the following inaccurate but simple solution is used: if |
| 694 | * the slot index is higher than QFQ_MAX_SLOTS-2, then the timestamps |
| 695 | * of the class are shifted backward so as to let the slot index |
| 696 | * become equal to QFQ_MAX_SLOTS-2. This threshold is used because, if |
| 697 | * the slot index is above it, then the data structure implementing |
| 698 | * the bucket list either gets immediately corrupted or may get |
| 699 | * corrupted on a possible next packet arrival that causes the start |
| 700 | * time of the group to be shifted backward. |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 701 | */ |
| 702 | static void qfq_slot_insert(struct qfq_group *grp, struct qfq_class *cl, |
| 703 | u64 roundedS) |
| 704 | { |
| 705 | u64 slot = (roundedS - grp->S) >> grp->slot_shift; |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 706 | unsigned int i; /* slot index in the bucket list */ |
| 707 | |
| 708 | if (unlikely(slot > QFQ_MAX_SLOTS - 2)) { |
| 709 | u64 deltaS = roundedS - grp->S - |
| 710 | ((u64)(QFQ_MAX_SLOTS - 2)<<grp->slot_shift); |
| 711 | cl->S -= deltaS; |
| 712 | cl->F -= deltaS; |
| 713 | slot = QFQ_MAX_SLOTS - 2; |
| 714 | } |
| 715 | |
| 716 | i = (grp->front + slot) % QFQ_MAX_SLOTS; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 717 | |
| 718 | hlist_add_head(&cl->next, &grp->slots[i]); |
| 719 | __set_bit(slot, &grp->full_slots); |
| 720 | } |
| 721 | |
| 722 | /* Maybe introduce hlist_first_entry?? */ |
| 723 | static struct qfq_class *qfq_slot_head(struct qfq_group *grp) |
| 724 | { |
| 725 | return hlist_entry(grp->slots[grp->front].first, |
| 726 | struct qfq_class, next); |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * remove the entry from the slot |
| 731 | */ |
| 732 | static void qfq_front_slot_remove(struct qfq_group *grp) |
| 733 | { |
| 734 | struct qfq_class *cl = qfq_slot_head(grp); |
| 735 | |
| 736 | BUG_ON(!cl); |
| 737 | hlist_del(&cl->next); |
| 738 | if (hlist_empty(&grp->slots[grp->front])) |
| 739 | __clear_bit(0, &grp->full_slots); |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Returns the first full queue in a group. As a side effect, |
| 744 | * adjust the bucket list so the first non-empty bucket is at |
| 745 | * position 0 in full_slots. |
| 746 | */ |
| 747 | static struct qfq_class *qfq_slot_scan(struct qfq_group *grp) |
| 748 | { |
| 749 | unsigned int i; |
| 750 | |
| 751 | pr_debug("qfq slot_scan: grp %u full %#lx\n", |
| 752 | grp->index, grp->full_slots); |
| 753 | |
| 754 | if (grp->full_slots == 0) |
| 755 | return NULL; |
| 756 | |
| 757 | i = __ffs(grp->full_slots); /* zero based */ |
| 758 | if (i > 0) { |
| 759 | grp->front = (grp->front + i) % QFQ_MAX_SLOTS; |
| 760 | grp->full_slots >>= i; |
| 761 | } |
| 762 | |
| 763 | return qfq_slot_head(grp); |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * adjust the bucket list. When the start time of a group decreases, |
| 768 | * we move the index down (modulo QFQ_MAX_SLOTS) so we don't need to |
| 769 | * move the objects. The mask of occupied slots must be shifted |
| 770 | * because we use ffs() to find the first non-empty slot. |
| 771 | * This covers decreases in the group's start time, but what about |
| 772 | * increases of the start time ? |
| 773 | * Here too we should make sure that i is less than 32 |
| 774 | */ |
| 775 | static void qfq_slot_rotate(struct qfq_group *grp, u64 roundedS) |
| 776 | { |
| 777 | unsigned int i = (grp->S - roundedS) >> grp->slot_shift; |
| 778 | |
| 779 | grp->full_slots <<= i; |
| 780 | grp->front = (grp->front - i) % QFQ_MAX_SLOTS; |
| 781 | } |
| 782 | |
| 783 | static void qfq_update_eligible(struct qfq_sched *q, u64 old_V) |
| 784 | { |
| 785 | struct qfq_group *grp; |
| 786 | unsigned long ineligible; |
| 787 | |
| 788 | ineligible = q->bitmaps[IR] | q->bitmaps[IB]; |
| 789 | if (ineligible) { |
| 790 | if (!q->bitmaps[ER]) { |
| 791 | grp = qfq_ffs(q, ineligible); |
| 792 | if (qfq_gt(grp->S, q->V)) |
| 793 | q->V = grp->S; |
| 794 | } |
| 795 | qfq_make_eligible(q, old_V); |
| 796 | } |
| 797 | } |
| 798 | |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 799 | /* |
| 800 | * Updates the class, returns true if also the group needs to be updated. |
| 801 | */ |
| 802 | static bool qfq_update_class(struct qfq_group *grp, struct qfq_class *cl) |
| 803 | { |
| 804 | unsigned int len = qdisc_peek_len(cl->qdisc); |
| 805 | |
| 806 | cl->S = cl->F; |
| 807 | if (!len) |
| 808 | qfq_front_slot_remove(grp); /* queue is empty */ |
| 809 | else { |
| 810 | u64 roundedS; |
| 811 | |
| 812 | cl->F = cl->S + (u64)len * cl->inv_w; |
| 813 | roundedS = qfq_round_down(cl->S, grp->slot_shift); |
| 814 | if (roundedS == grp->S) |
| 815 | return false; |
| 816 | |
| 817 | qfq_front_slot_remove(grp); |
| 818 | qfq_slot_insert(grp, cl, roundedS); |
| 819 | } |
| 820 | |
| 821 | return true; |
| 822 | } |
| 823 | |
| 824 | static struct sk_buff *qfq_dequeue(struct Qdisc *sch) |
| 825 | { |
| 826 | struct qfq_sched *q = qdisc_priv(sch); |
| 827 | struct qfq_group *grp; |
| 828 | struct qfq_class *cl; |
| 829 | struct sk_buff *skb; |
| 830 | unsigned int len; |
| 831 | u64 old_V; |
| 832 | |
| 833 | if (!q->bitmaps[ER]) |
| 834 | return NULL; |
| 835 | |
| 836 | grp = qfq_ffs(q, q->bitmaps[ER]); |
| 837 | |
| 838 | cl = qfq_slot_head(grp); |
| 839 | skb = qdisc_dequeue_peeked(cl->qdisc); |
| 840 | if (!skb) { |
| 841 | WARN_ONCE(1, "qfq_dequeue: non-workconserving leaf\n"); |
| 842 | return NULL; |
| 843 | } |
| 844 | |
| 845 | sch->q.qlen--; |
| 846 | qdisc_bstats_update(sch, skb); |
| 847 | |
| 848 | old_V = q->V; |
| 849 | len = qdisc_pkt_len(skb); |
| 850 | q->V += (u64)len * IWSUM; |
| 851 | pr_debug("qfq dequeue: len %u F %lld now %lld\n", |
| 852 | len, (unsigned long long) cl->F, (unsigned long long) q->V); |
| 853 | |
| 854 | if (qfq_update_class(grp, cl)) { |
| 855 | u64 old_F = grp->F; |
| 856 | |
| 857 | cl = qfq_slot_scan(grp); |
| 858 | if (!cl) |
| 859 | __clear_bit(grp->index, &q->bitmaps[ER]); |
| 860 | else { |
| 861 | u64 roundedS = qfq_round_down(cl->S, grp->slot_shift); |
| 862 | unsigned int s; |
| 863 | |
| 864 | if (grp->S == roundedS) |
| 865 | goto skip_unblock; |
| 866 | grp->S = roundedS; |
| 867 | grp->F = roundedS + (2ULL << grp->slot_shift); |
| 868 | __clear_bit(grp->index, &q->bitmaps[ER]); |
| 869 | s = qfq_calc_state(q, grp); |
| 870 | __set_bit(grp->index, &q->bitmaps[s]); |
| 871 | } |
| 872 | |
| 873 | qfq_unblock_groups(q, grp->index, old_F); |
| 874 | } |
| 875 | |
| 876 | skip_unblock: |
| 877 | qfq_update_eligible(q, old_V); |
| 878 | |
| 879 | return skb; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Assign a reasonable start time for a new flow k in group i. |
| 884 | * Admissible values for \hat(F) are multiples of \sigma_i |
| 885 | * no greater than V+\sigma_i . Larger values mean that |
| 886 | * we had a wraparound so we consider the timestamp to be stale. |
| 887 | * |
| 888 | * If F is not stale and F >= V then we set S = F. |
| 889 | * Otherwise we should assign S = V, but this may violate |
| 890 | * the ordering in ER. So, if we have groups in ER, set S to |
| 891 | * the F_j of the first group j which would be blocking us. |
| 892 | * We are guaranteed not to move S backward because |
| 893 | * otherwise our group i would still be blocked. |
| 894 | */ |
| 895 | static void qfq_update_start(struct qfq_sched *q, struct qfq_class *cl) |
| 896 | { |
| 897 | unsigned long mask; |
Eric Dumazet | 6bafcac | 2012-01-02 05:47:57 +0000 | [diff] [blame] | 898 | u64 limit, roundedF; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 899 | int slot_shift = cl->grp->slot_shift; |
| 900 | |
| 901 | roundedF = qfq_round_down(cl->F, slot_shift); |
Eric Dumazet | 6bafcac | 2012-01-02 05:47:57 +0000 | [diff] [blame] | 902 | limit = qfq_round_down(q->V, slot_shift) + (1ULL << slot_shift); |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 903 | |
| 904 | if (!qfq_gt(cl->F, q->V) || qfq_gt(roundedF, limit)) { |
| 905 | /* timestamp was stale */ |
| 906 | mask = mask_from(q->bitmaps[ER], cl->grp->index); |
| 907 | if (mask) { |
| 908 | struct qfq_group *next = qfq_ffs(q, mask); |
| 909 | if (qfq_gt(roundedF, next->F)) { |
Paolo Valente | 7126195 | 2012-09-15 00:41:35 +0000 | [diff] [blame] | 910 | if (qfq_gt(limit, next->F)) |
| 911 | cl->S = next->F; |
| 912 | else /* preserve timestamp correctness */ |
| 913 | cl->S = limit; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 914 | return; |
| 915 | } |
| 916 | } |
| 917 | cl->S = q->V; |
| 918 | } else /* timestamp is not stale */ |
| 919 | cl->S = cl->F; |
| 920 | } |
| 921 | |
| 922 | static int qfq_enqueue(struct sk_buff *skb, struct Qdisc *sch) |
| 923 | { |
| 924 | struct qfq_sched *q = qdisc_priv(sch); |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 925 | struct qfq_class *cl; |
David S. Miller | f54ba77 | 2012-09-27 18:35:47 -0400 | [diff] [blame] | 926 | int err = 0; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 927 | |
| 928 | cl = qfq_classify(skb, sch, &err); |
| 929 | if (cl == NULL) { |
| 930 | if (err & __NET_XMIT_BYPASS) |
| 931 | sch->qstats.drops++; |
| 932 | kfree_skb(skb); |
| 933 | return err; |
| 934 | } |
| 935 | pr_debug("qfq_enqueue: cl = %x\n", cl->common.classid); |
| 936 | |
Paolo Valente | 3015f3d | 2012-11-05 20:29:24 +0000 | [diff] [blame] | 937 | if (unlikely(cl->lmax < qdisc_pkt_len(skb))) { |
| 938 | pr_debug("qfq: increasing maxpkt from %u to %u for class %u", |
| 939 | cl->lmax, qdisc_pkt_len(skb), cl->common.classid); |
| 940 | qfq_update_reactivate_class(q, cl, cl->inv_w, |
| 941 | qdisc_pkt_len(skb), 0); |
| 942 | } |
| 943 | |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 944 | err = qdisc_enqueue(skb, cl->qdisc); |
| 945 | if (unlikely(err != NET_XMIT_SUCCESS)) { |
| 946 | pr_debug("qfq_enqueue: enqueue failed %d\n", err); |
| 947 | if (net_xmit_drop_count(err)) { |
| 948 | cl->qstats.drops++; |
| 949 | sch->qstats.drops++; |
| 950 | } |
| 951 | return err; |
| 952 | } |
| 953 | |
| 954 | bstats_update(&cl->bstats, skb); |
| 955 | ++sch->q.qlen; |
| 956 | |
| 957 | /* If the new skb is not the head of queue, then done here. */ |
| 958 | if (cl->qdisc->q.qlen != 1) |
| 959 | return err; |
| 960 | |
| 961 | /* If reach this point, queue q was idle */ |
Paolo Valente | be72f63 | 2012-08-07 07:27:25 +0000 | [diff] [blame] | 962 | qfq_activate_class(q, cl, qdisc_pkt_len(skb)); |
| 963 | |
| 964 | return err; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Handle class switch from idle to backlogged. |
| 969 | */ |
| 970 | static void qfq_activate_class(struct qfq_sched *q, struct qfq_class *cl, |
| 971 | unsigned int pkt_len) |
| 972 | { |
| 973 | struct qfq_group *grp = cl->grp; |
| 974 | u64 roundedS; |
| 975 | int s; |
| 976 | |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 977 | qfq_update_start(q, cl); |
| 978 | |
| 979 | /* compute new finish time and rounded start. */ |
Paolo Valente | be72f63 | 2012-08-07 07:27:25 +0000 | [diff] [blame] | 980 | cl->F = cl->S + (u64)pkt_len * cl->inv_w; |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 981 | roundedS = qfq_round_down(cl->S, grp->slot_shift); |
| 982 | |
| 983 | /* |
| 984 | * insert cl in the correct bucket. |
| 985 | * If cl->S >= grp->S we don't need to adjust the |
| 986 | * bucket list and simply go to the insertion phase. |
| 987 | * Otherwise grp->S is decreasing, we must make room |
| 988 | * in the bucket list, and also recompute the group state. |
| 989 | * Finally, if there were no flows in this group and nobody |
| 990 | * was in ER make sure to adjust V. |
| 991 | */ |
| 992 | if (grp->full_slots) { |
| 993 | if (!qfq_gt(grp->S, cl->S)) |
| 994 | goto skip_update; |
| 995 | |
| 996 | /* create a slot for this cl->S */ |
| 997 | qfq_slot_rotate(grp, roundedS); |
| 998 | /* group was surely ineligible, remove */ |
| 999 | __clear_bit(grp->index, &q->bitmaps[IR]); |
| 1000 | __clear_bit(grp->index, &q->bitmaps[IB]); |
| 1001 | } else if (!q->bitmaps[ER] && qfq_gt(roundedS, q->V)) |
| 1002 | q->V = roundedS; |
| 1003 | |
| 1004 | grp->S = roundedS; |
| 1005 | grp->F = roundedS + (2ULL << grp->slot_shift); |
| 1006 | s = qfq_calc_state(q, grp); |
| 1007 | __set_bit(grp->index, &q->bitmaps[s]); |
| 1008 | |
| 1009 | pr_debug("qfq enqueue: new state %d %#lx S %lld F %lld V %lld\n", |
| 1010 | s, q->bitmaps[s], |
| 1011 | (unsigned long long) cl->S, |
| 1012 | (unsigned long long) cl->F, |
| 1013 | (unsigned long long) q->V); |
| 1014 | |
| 1015 | skip_update: |
| 1016 | qfq_slot_insert(grp, cl, roundedS); |
stephen hemminger | 0545a30 | 2011-04-04 05:30:58 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | static void qfq_slot_remove(struct qfq_sched *q, struct qfq_group *grp, |
| 1021 | struct qfq_class *cl) |
| 1022 | { |
| 1023 | unsigned int i, offset; |
| 1024 | u64 roundedS; |
| 1025 | |
| 1026 | roundedS = qfq_round_down(cl->S, grp->slot_shift); |
| 1027 | offset = (roundedS - grp->S) >> grp->slot_shift; |
| 1028 | i = (grp->front + offset) % QFQ_MAX_SLOTS; |
| 1029 | |
| 1030 | hlist_del(&cl->next); |
| 1031 | if (hlist_empty(&grp->slots[i])) |
| 1032 | __clear_bit(offset, &grp->full_slots); |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * called to forcibly destroy a queue. |
| 1037 | * If the queue is not in the front bucket, or if it has |
| 1038 | * other queues in the front bucket, we can simply remove |
| 1039 | * the queue with no other side effects. |
| 1040 | * Otherwise we must propagate the event up. |
| 1041 | */ |
| 1042 | static void qfq_deactivate_class(struct qfq_sched *q, struct qfq_class *cl) |
| 1043 | { |
| 1044 | struct qfq_group *grp = cl->grp; |
| 1045 | unsigned long mask; |
| 1046 | u64 roundedS; |
| 1047 | int s; |
| 1048 | |
| 1049 | cl->F = cl->S; |
| 1050 | qfq_slot_remove(q, grp, cl); |
| 1051 | |
| 1052 | if (!grp->full_slots) { |
| 1053 | __clear_bit(grp->index, &q->bitmaps[IR]); |
| 1054 | __clear_bit(grp->index, &q->bitmaps[EB]); |
| 1055 | __clear_bit(grp->index, &q->bitmaps[IB]); |
| 1056 | |
| 1057 | if (test_bit(grp->index, &q->bitmaps[ER]) && |
| 1058 | !(q->bitmaps[ER] & ~((1UL << grp->index) - 1))) { |
| 1059 | mask = q->bitmaps[ER] & ((1UL << grp->index) - 1); |
| 1060 | if (mask) |
| 1061 | mask = ~((1UL << __fls(mask)) - 1); |
| 1062 | else |
| 1063 | mask = ~0UL; |
| 1064 | qfq_move_groups(q, mask, EB, ER); |
| 1065 | qfq_move_groups(q, mask, IB, IR); |
| 1066 | } |
| 1067 | __clear_bit(grp->index, &q->bitmaps[ER]); |
| 1068 | } else if (hlist_empty(&grp->slots[grp->front])) { |
| 1069 | cl = qfq_slot_scan(grp); |
| 1070 | roundedS = qfq_round_down(cl->S, grp->slot_shift); |
| 1071 | if (grp->S != roundedS) { |
| 1072 | __clear_bit(grp->index, &q->bitmaps[ER]); |
| 1073 | __clear_bit(grp->index, &q->bitmaps[IR]); |
| 1074 | __clear_bit(grp->index, &q->bitmaps[EB]); |
| 1075 | __clear_bit(grp->index, &q->bitmaps[IB]); |
| 1076 | grp->S = roundedS; |
| 1077 | grp->F = roundedS + (2ULL << grp->slot_shift); |
| 1078 | s = qfq_calc_state(q, grp); |
| 1079 | __set_bit(grp->index, &q->bitmaps[s]); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | qfq_update_eligible(q, q->V); |
| 1084 | } |
| 1085 | |
| 1086 | static void qfq_qlen_notify(struct Qdisc *sch, unsigned long arg) |
| 1087 | { |
| 1088 | struct qfq_sched *q = qdisc_priv(sch); |
| 1089 | struct qfq_class *cl = (struct qfq_class *)arg; |
| 1090 | |
| 1091 | if (cl->qdisc->q.qlen == 0) |
| 1092 | qfq_deactivate_class(q, cl); |
| 1093 | } |
| 1094 | |
| 1095 | static unsigned int qfq_drop(struct Qdisc *sch) |
| 1096 | { |
| 1097 | struct qfq_sched *q = qdisc_priv(sch); |
| 1098 | struct qfq_group *grp; |
| 1099 | unsigned int i, j, len; |
| 1100 | |
| 1101 | for (i = 0; i <= QFQ_MAX_INDEX; i++) { |
| 1102 | grp = &q->groups[i]; |
| 1103 | for (j = 0; j < QFQ_MAX_SLOTS; j++) { |
| 1104 | struct qfq_class *cl; |
| 1105 | struct hlist_node *n; |
| 1106 | |
| 1107 | hlist_for_each_entry(cl, n, &grp->slots[j], next) { |
| 1108 | |
| 1109 | if (!cl->qdisc->ops->drop) |
| 1110 | continue; |
| 1111 | |
| 1112 | len = cl->qdisc->ops->drop(cl->qdisc); |
| 1113 | if (len > 0) { |
| 1114 | sch->q.qlen--; |
| 1115 | if (!cl->qdisc->q.qlen) |
| 1116 | qfq_deactivate_class(q, cl); |
| 1117 | |
| 1118 | return len; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static int qfq_init_qdisc(struct Qdisc *sch, struct nlattr *opt) |
| 1128 | { |
| 1129 | struct qfq_sched *q = qdisc_priv(sch); |
| 1130 | struct qfq_group *grp; |
| 1131 | int i, j, err; |
| 1132 | |
| 1133 | err = qdisc_class_hash_init(&q->clhash); |
| 1134 | if (err < 0) |
| 1135 | return err; |
| 1136 | |
| 1137 | for (i = 0; i <= QFQ_MAX_INDEX; i++) { |
| 1138 | grp = &q->groups[i]; |
| 1139 | grp->index = i; |
| 1140 | grp->slot_shift = QFQ_MTU_SHIFT + FRAC_BITS |
| 1141 | - (QFQ_MAX_INDEX - i); |
| 1142 | for (j = 0; j < QFQ_MAX_SLOTS; j++) |
| 1143 | INIT_HLIST_HEAD(&grp->slots[j]); |
| 1144 | } |
| 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | static void qfq_reset_qdisc(struct Qdisc *sch) |
| 1150 | { |
| 1151 | struct qfq_sched *q = qdisc_priv(sch); |
| 1152 | struct qfq_group *grp; |
| 1153 | struct qfq_class *cl; |
| 1154 | struct hlist_node *n, *tmp; |
| 1155 | unsigned int i, j; |
| 1156 | |
| 1157 | for (i = 0; i <= QFQ_MAX_INDEX; i++) { |
| 1158 | grp = &q->groups[i]; |
| 1159 | for (j = 0; j < QFQ_MAX_SLOTS; j++) { |
| 1160 | hlist_for_each_entry_safe(cl, n, tmp, |
| 1161 | &grp->slots[j], next) { |
| 1162 | qfq_deactivate_class(q, cl); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | for (i = 0; i < q->clhash.hashsize; i++) { |
| 1168 | hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) |
| 1169 | qdisc_reset(cl->qdisc); |
| 1170 | } |
| 1171 | sch->q.qlen = 0; |
| 1172 | } |
| 1173 | |
| 1174 | static void qfq_destroy_qdisc(struct Qdisc *sch) |
| 1175 | { |
| 1176 | struct qfq_sched *q = qdisc_priv(sch); |
| 1177 | struct qfq_class *cl; |
| 1178 | struct hlist_node *n, *next; |
| 1179 | unsigned int i; |
| 1180 | |
| 1181 | tcf_destroy_chain(&q->filter_list); |
| 1182 | |
| 1183 | for (i = 0; i < q->clhash.hashsize; i++) { |
| 1184 | hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i], |
| 1185 | common.hnode) { |
| 1186 | qfq_destroy_class(sch, cl); |
| 1187 | } |
| 1188 | } |
| 1189 | qdisc_class_hash_destroy(&q->clhash); |
| 1190 | } |
| 1191 | |
| 1192 | static const struct Qdisc_class_ops qfq_class_ops = { |
| 1193 | .change = qfq_change_class, |
| 1194 | .delete = qfq_delete_class, |
| 1195 | .get = qfq_get_class, |
| 1196 | .put = qfq_put_class, |
| 1197 | .tcf_chain = qfq_tcf_chain, |
| 1198 | .bind_tcf = qfq_bind_tcf, |
| 1199 | .unbind_tcf = qfq_unbind_tcf, |
| 1200 | .graft = qfq_graft_class, |
| 1201 | .leaf = qfq_class_leaf, |
| 1202 | .qlen_notify = qfq_qlen_notify, |
| 1203 | .dump = qfq_dump_class, |
| 1204 | .dump_stats = qfq_dump_class_stats, |
| 1205 | .walk = qfq_walk, |
| 1206 | }; |
| 1207 | |
| 1208 | static struct Qdisc_ops qfq_qdisc_ops __read_mostly = { |
| 1209 | .cl_ops = &qfq_class_ops, |
| 1210 | .id = "qfq", |
| 1211 | .priv_size = sizeof(struct qfq_sched), |
| 1212 | .enqueue = qfq_enqueue, |
| 1213 | .dequeue = qfq_dequeue, |
| 1214 | .peek = qdisc_peek_dequeued, |
| 1215 | .drop = qfq_drop, |
| 1216 | .init = qfq_init_qdisc, |
| 1217 | .reset = qfq_reset_qdisc, |
| 1218 | .destroy = qfq_destroy_qdisc, |
| 1219 | .owner = THIS_MODULE, |
| 1220 | }; |
| 1221 | |
| 1222 | static int __init qfq_init(void) |
| 1223 | { |
| 1224 | return register_qdisc(&qfq_qdisc_ops); |
| 1225 | } |
| 1226 | |
| 1227 | static void __exit qfq_exit(void) |
| 1228 | { |
| 1229 | unregister_qdisc(&qfq_qdisc_ops); |
| 1230 | } |
| 1231 | |
| 1232 | module_init(qfq_init); |
| 1233 | module_exit(qfq_exit); |
| 1234 | MODULE_LICENSE("GPL"); |