1 /* Copyright (C) 2013 Cisco Systems, Inc, 2013. 2 * 3 * This program is free software; you can redistribute it and/or 4 * modify it under the terms of the GNU General Public License 5 * as published by the Free Software Foundation; either version 2 6 * of the License. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * Author: Vijay Subramanian <vijaynsu@cisco.com> 14 * Author: Mythili Prabhu <mysuryan@cisco.com> 15 * 16 * ECN support is added by Naeem Khademi <naeemk@ifi.uio.no> 17 * University of Oslo, Norway. 18 * 19 * References: 20 * IETF draft submission: http://tools.ietf.org/html/draft-pan-aqm-pie-00 21 * IEEE Conference on High Performance Switching and Routing 2013 : 22 * "PIE: A * Lightweight Control Scheme to Address the Bufferbloat Problem" 23 */ 24 25 #include <linux/module.h> 26 #include <linux/slab.h> 27 #include <linux/types.h> 28 #include <linux/kernel.h> 29 #include <linux/errno.h> 30 #include <linux/skbuff.h> 31 #include <net/pkt_sched.h> 32 #include <net/inet_ecn.h> 33 34 #define QUEUE_THRESHOLD 10000 35 #define DQCOUNT_INVALID -1 36 #define MAX_PROB 0xffffffff 37 #define PIE_SCALE 8 38 39 /* parameters used */ 40 struct pie_params { 41 psched_time_t target; /* user specified target delay in pschedtime */ 42 u32 tupdate; /* timer frequency (in jiffies) */ 43 u32 limit; /* number of packets that can be enqueued */ 44 u32 alpha; /* alpha and beta are between 0 and 32 */ 45 u32 beta; /* and are used for shift relative to 1 */ 46 bool ecn; /* true if ecn is enabled */ 47 bool bytemode; /* to scale drop early prob based on pkt size */ 48 }; 49 50 /* variables used */ 51 struct pie_vars { 52 u32 prob; /* probability but scaled by u32 limit. */ 53 psched_time_t burst_time; 54 psched_time_t qdelay; 55 psched_time_t qdelay_old; 56 u64 dq_count; /* measured in bytes */ 57 psched_time_t dq_tstamp; /* drain rate */ 58 u32 avg_dq_rate; /* bytes per pschedtime tick,scaled */ 59 u32 qlen_old; /* in bytes */ 60 }; 61 62 /* statistics gathering */ 63 struct pie_stats { 64 u32 packets_in; /* total number of packets enqueued */ 65 u32 dropped; /* packets dropped due to pie_action */ 66 u32 overlimit; /* dropped due to lack of space in queue */ 67 u32 maxq; /* maximum queue size */ 68 u32 ecn_mark; /* packets marked with ECN */ 69 }; 70 71 /* private data for the Qdisc */ 72 struct pie_sched_data { 73 struct pie_params params; 74 struct pie_vars vars; 75 struct pie_stats stats; 76 struct timer_list adapt_timer; 77 struct Qdisc *sch; 78 }; 79 80 static void pie_params_init(struct pie_params *params) 81 { 82 params->alpha = 2; 83 params->beta = 20; 84 params->tupdate = usecs_to_jiffies(30 * USEC_PER_MSEC); /* 30 ms */ 85 params->limit = 1000; /* default of 1000 packets */ 86 params->target = PSCHED_NS2TICKS(20 * NSEC_PER_MSEC); /* 20 ms */ 87 params->ecn = false; 88 params->bytemode = false; 89 } 90 91 static void pie_vars_init(struct pie_vars *vars) 92 { 93 vars->dq_count = DQCOUNT_INVALID; 94 vars->avg_dq_rate = 0; 95 /* default of 100 ms in pschedtime */ 96 vars->burst_time = PSCHED_NS2TICKS(100 * NSEC_PER_MSEC); 97 } 98 99 static bool drop_early(struct Qdisc *sch, u32 packet_size) 100 { 101 struct pie_sched_data *q = qdisc_priv(sch); 102 u32 rnd; 103 u32 local_prob = q->vars.prob; 104 u32 mtu = psched_mtu(qdisc_dev(sch)); 105 106 /* If there is still burst allowance left skip random early drop */ 107 if (q->vars.burst_time > 0) 108 return false; 109 110 /* If current delay is less than half of target, and 111 * if drop prob is low already, disable early_drop 112 */ 113 if ((q->vars.qdelay < q->params.target / 2) 114 && (q->vars.prob < MAX_PROB / 5)) 115 return false; 116 117 /* If we have fewer than 2 mtu-sized packets, disable drop_early, 118 * similar to min_th in RED 119 */ 120 if (sch->qstats.backlog < 2 * mtu) 121 return false; 122 123 /* If bytemode is turned on, use packet size to compute new 124 * probablity. Smaller packets will have lower drop prob in this case 125 */ 126 if (q->params.bytemode && packet_size <= mtu) 127 local_prob = (local_prob / mtu) * packet_size; 128 else 129 local_prob = q->vars.prob; 130 131 rnd = prandom_u32(); 132 if (rnd < local_prob) 133 return true; 134 135 return false; 136 } 137 138 static int pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch, 139 struct sk_buff **to_free) 140 { 141 struct pie_sched_data *q = qdisc_priv(sch); 142 bool enqueue = false; 143 144 if (unlikely(qdisc_qlen(sch) >= sch->limit)) { 145 q->stats.overlimit++; 146 goto out; 147 } 148 149 if (!drop_early(sch, skb->len)) { 150 enqueue = true; 151 } else if (q->params.ecn && (q->vars.prob <= MAX_PROB / 10) && 152 INET_ECN_set_ce(skb)) { 153 /* If packet is ecn capable, mark it if drop probability 154 * is lower than 10%, else drop it. 155 */ 156 q->stats.ecn_mark++; 157 enqueue = true; 158 } 159 160 /* we can enqueue the packet */ 161 if (enqueue) { 162 q->stats.packets_in++; 163 if (qdisc_qlen(sch) > q->stats.maxq) 164 q->stats.maxq = qdisc_qlen(sch); 165 166 return qdisc_enqueue_tail(skb, sch); 167 } 168 169 out: 170 q->stats.dropped++; 171 return qdisc_drop(skb, sch, to_free); 172 } 173 174 static const struct nla_policy pie_policy[TCA_PIE_MAX + 1] = { 175 [TCA_PIE_TARGET] = {.type = NLA_U32}, 176 [TCA_PIE_LIMIT] = {.type = NLA_U32}, 177 [TCA_PIE_TUPDATE] = {.type = NLA_U32}, 178 [TCA_PIE_ALPHA] = {.type = NLA_U32}, 179 [TCA_PIE_BETA] = {.type = NLA_U32}, 180 [TCA_PIE_ECN] = {.type = NLA_U32}, 181 [TCA_PIE_BYTEMODE] = {.type = NLA_U32}, 182 }; 183 184 static int pie_change(struct Qdisc *sch, struct nlattr *opt) 185 { 186 struct pie_sched_data *q = qdisc_priv(sch); 187 struct nlattr *tb[TCA_PIE_MAX + 1]; 188 unsigned int qlen, dropped = 0; 189 int err; 190 191 if (!opt) 192 return -EINVAL; 193 194 err = nla_parse_nested(tb, TCA_PIE_MAX, opt, pie_policy, NULL); 195 if (err < 0) 196 return err; 197 198 sch_tree_lock(sch); 199 200 /* convert from microseconds to pschedtime */ 201 if (tb[TCA_PIE_TARGET]) { 202 /* target is in us */ 203 u32 target = nla_get_u32(tb[TCA_PIE_TARGET]); 204 205 /* convert to pschedtime */ 206 q->params.target = PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC); 207 } 208 209 /* tupdate is in jiffies */ 210 if (tb[TCA_PIE_TUPDATE]) 211 q->params.tupdate = usecs_to_jiffies(nla_get_u32(tb[TCA_PIE_TUPDATE])); 212 213 if (tb[TCA_PIE_LIMIT]) { 214 u32 limit = nla_get_u32(tb[TCA_PIE_LIMIT]); 215 216 q->params.limit = limit; 217 sch->limit = limit; 218 } 219 220 if (tb[TCA_PIE_ALPHA]) 221 q->params.alpha = nla_get_u32(tb[TCA_PIE_ALPHA]); 222 223 if (tb[TCA_PIE_BETA]) 224 q->params.beta = nla_get_u32(tb[TCA_PIE_BETA]); 225 226 if (tb[TCA_PIE_ECN]) 227 q->params.ecn = nla_get_u32(tb[TCA_PIE_ECN]); 228 229 if (tb[TCA_PIE_BYTEMODE]) 230 q->params.bytemode = nla_get_u32(tb[TCA_PIE_BYTEMODE]); 231 232 /* Drop excess packets if new limit is lower */ 233 qlen = sch->q.qlen; 234 while (sch->q.qlen > sch->limit) { 235 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q); 236 237 dropped += qdisc_pkt_len(skb); 238 qdisc_qstats_backlog_dec(sch, skb); 239 rtnl_qdisc_drop(skb, sch); 240 } 241 qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped); 242 243 sch_tree_unlock(sch); 244 return 0; 245 } 246 247 static void pie_process_dequeue(struct Qdisc *sch, struct sk_buff *skb) 248 { 249 250 struct pie_sched_data *q = qdisc_priv(sch); 251 int qlen = sch->qstats.backlog; /* current queue size in bytes */ 252 253 /* If current queue is about 10 packets or more and dq_count is unset 254 * we have enough packets to calculate the drain rate. Save 255 * current time as dq_tstamp and start measurement cycle. 256 */ 257 if (qlen >= QUEUE_THRESHOLD && q->vars.dq_count == DQCOUNT_INVALID) { 258 q->vars.dq_tstamp = psched_get_time(); 259 q->vars.dq_count = 0; 260 } 261 262 /* Calculate the average drain rate from this value. If queue length 263 * has receded to a small value viz., <= QUEUE_THRESHOLD bytes,reset 264 * the dq_count to -1 as we don't have enough packets to calculate the 265 * drain rate anymore The following if block is entered only when we 266 * have a substantial queue built up (QUEUE_THRESHOLD bytes or more) 267 * and we calculate the drain rate for the threshold here. dq_count is 268 * in bytes, time difference in psched_time, hence rate is in 269 * bytes/psched_time. 270 */ 271 if (q->vars.dq_count != DQCOUNT_INVALID) { 272 q->vars.dq_count += skb->len; 273 274 if (q->vars.dq_count >= QUEUE_THRESHOLD) { 275 psched_time_t now = psched_get_time(); 276 u32 dtime = now - q->vars.dq_tstamp; 277 u32 count = q->vars.dq_count << PIE_SCALE; 278 279 if (dtime == 0) 280 return; 281 282 count = count / dtime; 283 284 if (q->vars.avg_dq_rate == 0) 285 q->vars.avg_dq_rate = count; 286 else 287 q->vars.avg_dq_rate = 288 (q->vars.avg_dq_rate - 289 (q->vars.avg_dq_rate >> 3)) + (count >> 3); 290 291 /* If the queue has receded below the threshold, we hold 292 * on to the last drain rate calculated, else we reset 293 * dq_count to 0 to re-enter the if block when the next 294 * packet is dequeued 295 */ 296 if (qlen < QUEUE_THRESHOLD) 297 q->vars.dq_count = DQCOUNT_INVALID; 298 else { 299 q->vars.dq_count = 0; 300 q->vars.dq_tstamp = psched_get_time(); 301 } 302 303 if (q->vars.burst_time > 0) { 304 if (q->vars.burst_time > dtime) 305 q->vars.burst_time -= dtime; 306 else 307 q->vars.burst_time = 0; 308 } 309 } 310 } 311 } 312 313 static void calculate_probability(struct Qdisc *sch) 314 { 315 struct pie_sched_data *q = qdisc_priv(sch); 316 u32 qlen = sch->qstats.backlog; /* queue size in bytes */ 317 psched_time_t qdelay = 0; /* in pschedtime */ 318 psched_time_t qdelay_old = q->vars.qdelay; /* in pschedtime */ 319 s32 delta = 0; /* determines the change in probability */ 320 u32 oldprob; 321 u32 alpha, beta; 322 bool update_prob = true; 323 324 q->vars.qdelay_old = q->vars.qdelay; 325 326 if (q->vars.avg_dq_rate > 0) 327 qdelay = (qlen << PIE_SCALE) / q->vars.avg_dq_rate; 328 else 329 qdelay = 0; 330 331 /* If qdelay is zero and qlen is not, it means qlen is very small, less 332 * than dequeue_rate, so we do not update probabilty in this round 333 */ 334 if (qdelay == 0 && qlen != 0) 335 update_prob = false; 336 337 /* In the algorithm, alpha and beta are between 0 and 2 with typical 338 * value for alpha as 0.125. In this implementation, we use values 0-32 339 * passed from user space to represent this. Also, alpha and beta have 340 * unit of HZ and need to be scaled before they can used to update 341 * probability. alpha/beta are updated locally below by 1) scaling them 342 * appropriately 2) scaling down by 16 to come to 0-2 range. 343 * Please see paper for details. 344 * 345 * We scale alpha and beta differently depending on whether we are in 346 * light, medium or high dropping mode. 347 */ 348 if (q->vars.prob < MAX_PROB / 100) { 349 alpha = 350 (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 7; 351 beta = 352 (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 7; 353 } else if (q->vars.prob < MAX_PROB / 10) { 354 alpha = 355 (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 5; 356 beta = 357 (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 5; 358 } else { 359 alpha = 360 (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4; 361 beta = 362 (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4; 363 } 364 365 /* alpha and beta should be between 0 and 32, in multiples of 1/16 */ 366 delta += alpha * ((qdelay - q->params.target)); 367 delta += beta * ((qdelay - qdelay_old)); 368 369 oldprob = q->vars.prob; 370 371 /* to ensure we increase probability in steps of no more than 2% */ 372 if (delta > (s32) (MAX_PROB / (100 / 2)) && 373 q->vars.prob >= MAX_PROB / 10) 374 delta = (MAX_PROB / 100) * 2; 375 376 /* Non-linear drop: 377 * Tune drop probability to increase quickly for high delays(>= 250ms) 378 * 250ms is derived through experiments and provides error protection 379 */ 380 381 if (qdelay > (PSCHED_NS2TICKS(250 * NSEC_PER_MSEC))) 382 delta += MAX_PROB / (100 / 2); 383 384 q->vars.prob += delta; 385 386 if (delta > 0) { 387 /* prevent overflow */ 388 if (q->vars.prob < oldprob) { 389 q->vars.prob = MAX_PROB; 390 /* Prevent normalization error. If probability is at 391 * maximum value already, we normalize it here, and 392 * skip the check to do a non-linear drop in the next 393 * section. 394 */ 395 update_prob = false; 396 } 397 } else { 398 /* prevent underflow */ 399 if (q->vars.prob > oldprob) 400 q->vars.prob = 0; 401 } 402 403 /* Non-linear drop in probability: Reduce drop probability quickly if 404 * delay is 0 for 2 consecutive Tupdate periods. 405 */ 406 407 if ((qdelay == 0) && (qdelay_old == 0) && update_prob) 408 q->vars.prob = (q->vars.prob * 98) / 100; 409 410 q->vars.qdelay = qdelay; 411 q->vars.qlen_old = qlen; 412 413 /* We restart the measurement cycle if the following conditions are met 414 * 1. If the delay has been low for 2 consecutive Tupdate periods 415 * 2. Calculated drop probability is zero 416 * 3. We have atleast one estimate for the avg_dq_rate ie., 417 * is a non-zero value 418 */ 419 if ((q->vars.qdelay < q->params.target / 2) && 420 (q->vars.qdelay_old < q->params.target / 2) && 421 (q->vars.prob == 0) && 422 (q->vars.avg_dq_rate > 0)) 423 pie_vars_init(&q->vars); 424 } 425 426 static void pie_timer(struct timer_list *t) 427 { 428 struct pie_sched_data *q = from_timer(q, t, adapt_timer); 429 struct Qdisc *sch = q->sch; 430 spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch)); 431 432 spin_lock(root_lock); 433 calculate_probability(sch); 434 435 /* reset the timer to fire after 'tupdate'. tupdate is in jiffies. */ 436 if (q->params.tupdate) 437 mod_timer(&q->adapt_timer, jiffies + q->params.tupdate); 438 spin_unlock(root_lock); 439 440 } 441 442 static int pie_init(struct Qdisc *sch, struct nlattr *opt) 443 { 444 struct pie_sched_data *q = qdisc_priv(sch); 445 446 pie_params_init(&q->params); 447 pie_vars_init(&q->vars); 448 sch->limit = q->params.limit; 449 450 q->sch = sch; 451 timer_setup(&q->adapt_timer, pie_timer, 0); 452 453 if (opt) { 454 int err = pie_change(sch, opt); 455 456 if (err) 457 return err; 458 } 459 460 mod_timer(&q->adapt_timer, jiffies + HZ / 2); 461 return 0; 462 } 463 464 static int pie_dump(struct Qdisc *sch, struct sk_buff *skb) 465 { 466 struct pie_sched_data *q = qdisc_priv(sch); 467 struct nlattr *opts; 468 469 opts = nla_nest_start(skb, TCA_OPTIONS); 470 if (opts == NULL) 471 goto nla_put_failure; 472 473 /* convert target from pschedtime to us */ 474 if (nla_put_u32(skb, TCA_PIE_TARGET, 475 ((u32) PSCHED_TICKS2NS(q->params.target)) / 476 NSEC_PER_USEC) || 477 nla_put_u32(skb, TCA_PIE_LIMIT, sch->limit) || 478 nla_put_u32(skb, TCA_PIE_TUPDATE, jiffies_to_usecs(q->params.tupdate)) || 479 nla_put_u32(skb, TCA_PIE_ALPHA, q->params.alpha) || 480 nla_put_u32(skb, TCA_PIE_BETA, q->params.beta) || 481 nla_put_u32(skb, TCA_PIE_ECN, q->params.ecn) || 482 nla_put_u32(skb, TCA_PIE_BYTEMODE, q->params.bytemode)) 483 goto nla_put_failure; 484 485 return nla_nest_end(skb, opts); 486 487 nla_put_failure: 488 nla_nest_cancel(skb, opts); 489 return -1; 490 491 } 492 493 static int pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d) 494 { 495 struct pie_sched_data *q = qdisc_priv(sch); 496 struct tc_pie_xstats st = { 497 .prob = q->vars.prob, 498 .delay = ((u32) PSCHED_TICKS2NS(q->vars.qdelay)) / 499 NSEC_PER_USEC, 500 /* unscale and return dq_rate in bytes per sec */ 501 .avg_dq_rate = q->vars.avg_dq_rate * 502 (PSCHED_TICKS_PER_SEC) >> PIE_SCALE, 503 .packets_in = q->stats.packets_in, 504 .overlimit = q->stats.overlimit, 505 .maxq = q->stats.maxq, 506 .dropped = q->stats.dropped, 507 .ecn_mark = q->stats.ecn_mark, 508 }; 509 510 return gnet_stats_copy_app(d, &st, sizeof(st)); 511 } 512 513 static struct sk_buff *pie_qdisc_dequeue(struct Qdisc *sch) 514 { 515 struct sk_buff *skb; 516 skb = qdisc_dequeue_head(sch); 517 518 if (!skb) 519 return NULL; 520 521 pie_process_dequeue(sch, skb); 522 return skb; 523 } 524 525 static void pie_reset(struct Qdisc *sch) 526 { 527 struct pie_sched_data *q = qdisc_priv(sch); 528 qdisc_reset_queue(sch); 529 pie_vars_init(&q->vars); 530 } 531 532 static void pie_destroy(struct Qdisc *sch) 533 { 534 struct pie_sched_data *q = qdisc_priv(sch); 535 q->params.tupdate = 0; 536 del_timer_sync(&q->adapt_timer); 537 } 538 539 static struct Qdisc_ops pie_qdisc_ops __read_mostly = { 540 .id = "pie", 541 .priv_size = sizeof(struct pie_sched_data), 542 .enqueue = pie_qdisc_enqueue, 543 .dequeue = pie_qdisc_dequeue, 544 .peek = qdisc_peek_dequeued, 545 .init = pie_init, 546 .destroy = pie_destroy, 547 .reset = pie_reset, 548 .change = pie_change, 549 .dump = pie_dump, 550 .dump_stats = pie_dump_stats, 551 .owner = THIS_MODULE, 552 }; 553 554 static int __init pie_module_init(void) 555 { 556 return register_qdisc(&pie_qdisc_ops); 557 } 558 559 static void __exit pie_module_exit(void) 560 { 561 unregister_qdisc(&pie_qdisc_ops); 562 } 563 564 module_init(pie_module_init); 565 module_exit(pie_module_exit); 566 567 MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler"); 568 MODULE_AUTHOR("Vijay Subramanian"); 569 MODULE_AUTHOR("Mythili Prabhu"); 570 MODULE_LICENSE("GPL"); 571