sch_pie.c 15 KB

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