sch_pie.c 16 KB

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