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