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 <[email protected]>
  5. * Author: Mythili Prabhu <[email protected]>
  6. *
  7. * ECN support is added by Naeem Khademi <[email protected]>
  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. get_random_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. err = nla_parse_nested_deprecated(tb, TCA_PIE_MAX, opt, pie_policy,
  126. NULL);
  127. if (err < 0)
  128. return err;
  129. sch_tree_lock(sch);
  130. /* convert from microseconds to pschedtime */
  131. if (tb[TCA_PIE_TARGET]) {
  132. /* target is in us */
  133. u32 target = nla_get_u32(tb[TCA_PIE_TARGET]);
  134. /* convert to pschedtime */
  135. q->params.target = PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC);
  136. }
  137. /* tupdate is in jiffies */
  138. if (tb[TCA_PIE_TUPDATE])
  139. q->params.tupdate =
  140. usecs_to_jiffies(nla_get_u32(tb[TCA_PIE_TUPDATE]));
  141. if (tb[TCA_PIE_LIMIT]) {
  142. u32 limit = nla_get_u32(tb[TCA_PIE_LIMIT]);
  143. q->params.limit = limit;
  144. sch->limit = limit;
  145. }
  146. if (tb[TCA_PIE_ALPHA])
  147. q->params.alpha = nla_get_u32(tb[TCA_PIE_ALPHA]);
  148. if (tb[TCA_PIE_BETA])
  149. q->params.beta = nla_get_u32(tb[TCA_PIE_BETA]);
  150. if (tb[TCA_PIE_ECN])
  151. q->params.ecn = nla_get_u32(tb[TCA_PIE_ECN]);
  152. if (tb[TCA_PIE_BYTEMODE])
  153. q->params.bytemode = nla_get_u32(tb[TCA_PIE_BYTEMODE]);
  154. if (tb[TCA_PIE_DQ_RATE_ESTIMATOR])
  155. q->params.dq_rate_estimator =
  156. nla_get_u32(tb[TCA_PIE_DQ_RATE_ESTIMATOR]);
  157. /* Drop excess packets if new limit is lower */
  158. qlen = sch->q.qlen;
  159. while (sch->q.qlen > sch->limit) {
  160. struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
  161. dropped += qdisc_pkt_len(skb);
  162. qdisc_qstats_backlog_dec(sch, skb);
  163. rtnl_qdisc_drop(skb, sch);
  164. }
  165. qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
  166. sch_tree_unlock(sch);
  167. return 0;
  168. }
  169. void pie_process_dequeue(struct sk_buff *skb, struct pie_params *params,
  170. struct pie_vars *vars, u32 backlog)
  171. {
  172. psched_time_t now = psched_get_time();
  173. u32 dtime = 0;
  174. /* If dq_rate_estimator is disabled, calculate qdelay using the
  175. * packet timestamp.
  176. */
  177. if (!params->dq_rate_estimator) {
  178. vars->qdelay = now - pie_get_enqueue_time(skb);
  179. if (vars->dq_tstamp != DTIME_INVALID)
  180. dtime = now - vars->dq_tstamp;
  181. vars->dq_tstamp = now;
  182. if (backlog == 0)
  183. vars->qdelay = 0;
  184. if (dtime == 0)
  185. return;
  186. goto burst_allowance_reduction;
  187. }
  188. /* If current queue is about 10 packets or more and dq_count is unset
  189. * we have enough packets to calculate the drain rate. Save
  190. * current time as dq_tstamp and start measurement cycle.
  191. */
  192. if (backlog >= QUEUE_THRESHOLD && vars->dq_count == DQCOUNT_INVALID) {
  193. vars->dq_tstamp = psched_get_time();
  194. vars->dq_count = 0;
  195. }
  196. /* Calculate the average drain rate from this value. If queue length
  197. * has receded to a small value viz., <= QUEUE_THRESHOLD bytes, reset
  198. * the dq_count to -1 as we don't have enough packets to calculate the
  199. * drain rate anymore. The following if block is entered only when we
  200. * have a substantial queue built up (QUEUE_THRESHOLD bytes or more)
  201. * and we calculate the drain rate for the threshold here. dq_count is
  202. * in bytes, time difference in psched_time, hence rate is in
  203. * bytes/psched_time.
  204. */
  205. if (vars->dq_count != DQCOUNT_INVALID) {
  206. vars->dq_count += skb->len;
  207. if (vars->dq_count >= QUEUE_THRESHOLD) {
  208. u32 count = vars->dq_count << PIE_SCALE;
  209. dtime = now - vars->dq_tstamp;
  210. if (dtime == 0)
  211. return;
  212. count = count / dtime;
  213. if (vars->avg_dq_rate == 0)
  214. vars->avg_dq_rate = count;
  215. else
  216. vars->avg_dq_rate =
  217. (vars->avg_dq_rate -
  218. (vars->avg_dq_rate >> 3)) + (count >> 3);
  219. /* If the queue has receded below the threshold, we hold
  220. * on to the last drain rate calculated, else we reset
  221. * dq_count to 0 to re-enter the if block when the next
  222. * packet is dequeued
  223. */
  224. if (backlog < QUEUE_THRESHOLD) {
  225. vars->dq_count = DQCOUNT_INVALID;
  226. } else {
  227. vars->dq_count = 0;
  228. vars->dq_tstamp = psched_get_time();
  229. }
  230. goto burst_allowance_reduction;
  231. }
  232. }
  233. return;
  234. burst_allowance_reduction:
  235. if (vars->burst_time > 0) {
  236. if (vars->burst_time > dtime)
  237. vars->burst_time -= dtime;
  238. else
  239. vars->burst_time = 0;
  240. }
  241. }
  242. EXPORT_SYMBOL_GPL(pie_process_dequeue);
  243. void pie_calculate_probability(struct pie_params *params, struct pie_vars *vars,
  244. u32 backlog)
  245. {
  246. psched_time_t qdelay = 0; /* in pschedtime */
  247. psched_time_t qdelay_old = 0; /* in pschedtime */
  248. s64 delta = 0; /* determines the change in probability */
  249. u64 oldprob;
  250. u64 alpha, beta;
  251. u32 power;
  252. bool update_prob = true;
  253. if (params->dq_rate_estimator) {
  254. qdelay_old = vars->qdelay;
  255. vars->qdelay_old = vars->qdelay;
  256. if (vars->avg_dq_rate > 0)
  257. qdelay = (backlog << PIE_SCALE) / vars->avg_dq_rate;
  258. else
  259. qdelay = 0;
  260. } else {
  261. qdelay = vars->qdelay;
  262. qdelay_old = vars->qdelay_old;
  263. }
  264. /* If qdelay is zero and backlog is not, it means backlog is very small,
  265. * so we do not update probabilty in this round.
  266. */
  267. if (qdelay == 0 && backlog != 0)
  268. update_prob = false;
  269. /* In the algorithm, alpha and beta are between 0 and 2 with typical
  270. * value for alpha as 0.125. In this implementation, we use values 0-32
  271. * passed from user space to represent this. Also, alpha and beta have
  272. * unit of HZ and need to be scaled before they can used to update
  273. * probability. alpha/beta are updated locally below by scaling down
  274. * by 16 to come to 0-2 range.
  275. */
  276. alpha = ((u64)params->alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
  277. beta = ((u64)params->beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
  278. /* We scale alpha and beta differently depending on how heavy the
  279. * congestion is. Please see RFC 8033 for details.
  280. */
  281. if (vars->prob < MAX_PROB / 10) {
  282. alpha >>= 1;
  283. beta >>= 1;
  284. power = 100;
  285. while (vars->prob < div_u64(MAX_PROB, power) &&
  286. power <= 1000000) {
  287. alpha >>= 2;
  288. beta >>= 2;
  289. power *= 10;
  290. }
  291. }
  292. /* alpha and beta should be between 0 and 32, in multiples of 1/16 */
  293. delta += alpha * (qdelay - params->target);
  294. delta += beta * (qdelay - qdelay_old);
  295. oldprob = vars->prob;
  296. /* to ensure we increase probability in steps of no more than 2% */
  297. if (delta > (s64)(MAX_PROB / (100 / 2)) &&
  298. vars->prob >= MAX_PROB / 10)
  299. delta = (MAX_PROB / 100) * 2;
  300. /* Non-linear drop:
  301. * Tune drop probability to increase quickly for high delays(>= 250ms)
  302. * 250ms is derived through experiments and provides error protection
  303. */
  304. if (qdelay > (PSCHED_NS2TICKS(250 * NSEC_PER_MSEC)))
  305. delta += MAX_PROB / (100 / 2);
  306. vars->prob += delta;
  307. if (delta > 0) {
  308. /* prevent overflow */
  309. if (vars->prob < oldprob) {
  310. vars->prob = MAX_PROB;
  311. /* Prevent normalization error. If probability is at
  312. * maximum value already, we normalize it here, and
  313. * skip the check to do a non-linear drop in the next
  314. * section.
  315. */
  316. update_prob = false;
  317. }
  318. } else {
  319. /* prevent underflow */
  320. if (vars->prob > oldprob)
  321. vars->prob = 0;
  322. }
  323. /* Non-linear drop in probability: Reduce drop probability quickly if
  324. * delay is 0 for 2 consecutive Tupdate periods.
  325. */
  326. if (qdelay == 0 && qdelay_old == 0 && update_prob)
  327. /* Reduce drop probability to 98.4% */
  328. vars->prob -= vars->prob / 64;
  329. vars->qdelay = qdelay;
  330. vars->backlog_old = backlog;
  331. /* We restart the measurement cycle if the following conditions are met
  332. * 1. If the delay has been low for 2 consecutive Tupdate periods
  333. * 2. Calculated drop probability is zero
  334. * 3. If average dq_rate_estimator is enabled, we have at least one
  335. * estimate for the avg_dq_rate ie., is a non-zero value
  336. */
  337. if ((vars->qdelay < params->target / 2) &&
  338. (vars->qdelay_old < params->target / 2) &&
  339. vars->prob == 0 &&
  340. (!params->dq_rate_estimator || vars->avg_dq_rate > 0)) {
  341. pie_vars_init(vars);
  342. }
  343. if (!params->dq_rate_estimator)
  344. vars->qdelay_old = qdelay;
  345. }
  346. EXPORT_SYMBOL_GPL(pie_calculate_probability);
  347. static void pie_timer(struct timer_list *t)
  348. {
  349. struct pie_sched_data *q = from_timer(q, t, adapt_timer);
  350. struct Qdisc *sch = q->sch;
  351. spinlock_t *root_lock;
  352. rcu_read_lock();
  353. 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. rcu_read_unlock();
  361. }
  362. static int pie_init(struct Qdisc *sch, struct nlattr *opt,
  363. struct netlink_ext_ack *extack)
  364. {
  365. struct pie_sched_data *q = qdisc_priv(sch);
  366. pie_params_init(&q->params);
  367. pie_vars_init(&q->vars);
  368. sch->limit = q->params.limit;
  369. q->sch = sch;
  370. timer_setup(&q->adapt_timer, pie_timer, 0);
  371. if (opt) {
  372. int err = pie_change(sch, opt, extack);
  373. if (err)
  374. return err;
  375. }
  376. mod_timer(&q->adapt_timer, jiffies + HZ / 2);
  377. return 0;
  378. }
  379. static int pie_dump(struct Qdisc *sch, struct sk_buff *skb)
  380. {
  381. struct pie_sched_data *q = qdisc_priv(sch);
  382. struct nlattr *opts;
  383. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  384. if (!opts)
  385. goto nla_put_failure;
  386. /* convert target from pschedtime to us */
  387. if (nla_put_u32(skb, TCA_PIE_TARGET,
  388. ((u32)PSCHED_TICKS2NS(q->params.target)) /
  389. NSEC_PER_USEC) ||
  390. nla_put_u32(skb, TCA_PIE_LIMIT, sch->limit) ||
  391. nla_put_u32(skb, TCA_PIE_TUPDATE,
  392. jiffies_to_usecs(q->params.tupdate)) ||
  393. nla_put_u32(skb, TCA_PIE_ALPHA, q->params.alpha) ||
  394. nla_put_u32(skb, TCA_PIE_BETA, q->params.beta) ||
  395. nla_put_u32(skb, TCA_PIE_ECN, q->params.ecn) ||
  396. nla_put_u32(skb, TCA_PIE_BYTEMODE, q->params.bytemode) ||
  397. nla_put_u32(skb, TCA_PIE_DQ_RATE_ESTIMATOR,
  398. q->params.dq_rate_estimator))
  399. goto nla_put_failure;
  400. return nla_nest_end(skb, opts);
  401. nla_put_failure:
  402. nla_nest_cancel(skb, opts);
  403. return -1;
  404. }
  405. static int pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  406. {
  407. struct pie_sched_data *q = qdisc_priv(sch);
  408. struct tc_pie_xstats st = {
  409. .prob = q->vars.prob << BITS_PER_BYTE,
  410. .delay = ((u32)PSCHED_TICKS2NS(q->vars.qdelay)) /
  411. NSEC_PER_USEC,
  412. .packets_in = q->stats.packets_in,
  413. .overlimit = q->stats.overlimit,
  414. .maxq = q->stats.maxq,
  415. .dropped = q->stats.dropped,
  416. .ecn_mark = q->stats.ecn_mark,
  417. };
  418. /* avg_dq_rate is only valid if dq_rate_estimator is enabled */
  419. st.dq_rate_estimating = q->params.dq_rate_estimator;
  420. /* unscale and return dq_rate in bytes per sec */
  421. if (q->params.dq_rate_estimator)
  422. st.avg_dq_rate = q->vars.avg_dq_rate *
  423. (PSCHED_TICKS_PER_SEC) >> PIE_SCALE;
  424. return gnet_stats_copy_app(d, &st, sizeof(st));
  425. }
  426. static struct sk_buff *pie_qdisc_dequeue(struct Qdisc *sch)
  427. {
  428. struct pie_sched_data *q = qdisc_priv(sch);
  429. struct sk_buff *skb = qdisc_dequeue_head(sch);
  430. if (!skb)
  431. return NULL;
  432. pie_process_dequeue(skb, &q->params, &q->vars, sch->qstats.backlog);
  433. return skb;
  434. }
  435. static void pie_reset(struct Qdisc *sch)
  436. {
  437. struct pie_sched_data *q = qdisc_priv(sch);
  438. qdisc_reset_queue(sch);
  439. pie_vars_init(&q->vars);
  440. }
  441. static void pie_destroy(struct Qdisc *sch)
  442. {
  443. struct pie_sched_data *q = qdisc_priv(sch);
  444. q->params.tupdate = 0;
  445. del_timer_sync(&q->adapt_timer);
  446. }
  447. static struct Qdisc_ops pie_qdisc_ops __read_mostly = {
  448. .id = "pie",
  449. .priv_size = sizeof(struct pie_sched_data),
  450. .enqueue = pie_qdisc_enqueue,
  451. .dequeue = pie_qdisc_dequeue,
  452. .peek = qdisc_peek_dequeued,
  453. .init = pie_init,
  454. .destroy = pie_destroy,
  455. .reset = pie_reset,
  456. .change = pie_change,
  457. .dump = pie_dump,
  458. .dump_stats = pie_dump_stats,
  459. .owner = THIS_MODULE,
  460. };
  461. static int __init pie_module_init(void)
  462. {
  463. return register_qdisc(&pie_qdisc_ops);
  464. }
  465. static void __exit pie_module_exit(void)
  466. {
  467. unregister_qdisc(&pie_qdisc_ops);
  468. }
  469. module_init(pie_module_init);
  470. module_exit(pie_module_exit);
  471. MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler");
  472. MODULE_AUTHOR("Vijay Subramanian");
  473. MODULE_AUTHOR("Mythili Prabhu");
  474. MODULE_LICENSE("GPL");