sch_pie.c 15 KB

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