sch_pie.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. 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. enum skb_drop_reason reason = SKB_DROP_REASON_QDISC_OVERLIMIT;
  77. struct pie_sched_data *q = qdisc_priv(sch);
  78. bool enqueue = false;
  79. if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
  80. q->stats.overlimit++;
  81. goto out;
  82. }
  83. reason = SKB_DROP_REASON_QDISC_CONGESTED;
  84. if (!pie_drop_early(sch, &q->params, &q->vars, sch->qstats.backlog,
  85. skb->len)) {
  86. enqueue = true;
  87. } else if (q->params.ecn && (q->vars.prob <= MAX_PROB / 10) &&
  88. INET_ECN_set_ce(skb)) {
  89. /* If packet is ecn capable, mark it if drop probability
  90. * is lower than 10%, else drop it.
  91. */
  92. q->stats.ecn_mark++;
  93. enqueue = true;
  94. }
  95. /* we can enqueue the packet */
  96. if (enqueue) {
  97. /* Set enqueue time only when dq_rate_estimator is disabled. */
  98. if (!q->params.dq_rate_estimator)
  99. pie_set_enqueue_time(skb);
  100. q->stats.packets_in++;
  101. if (qdisc_qlen(sch) > q->stats.maxq)
  102. q->stats.maxq = qdisc_qlen(sch);
  103. return qdisc_enqueue_tail(skb, sch);
  104. }
  105. out:
  106. q->stats.dropped++;
  107. q->vars.accu_prob = 0;
  108. return qdisc_drop_reason(skb, sch, to_free, reason);
  109. }
  110. static const struct nla_policy pie_policy[TCA_PIE_MAX + 1] = {
  111. [TCA_PIE_TARGET] = {.type = NLA_U32},
  112. [TCA_PIE_LIMIT] = {.type = NLA_U32},
  113. [TCA_PIE_TUPDATE] = {.type = NLA_U32},
  114. [TCA_PIE_ALPHA] = {.type = NLA_U32},
  115. [TCA_PIE_BETA] = {.type = NLA_U32},
  116. [TCA_PIE_ECN] = {.type = NLA_U32},
  117. [TCA_PIE_BYTEMODE] = {.type = NLA_U32},
  118. [TCA_PIE_DQ_RATE_ESTIMATOR] = {.type = NLA_U32},
  119. };
  120. static int pie_change(struct Qdisc *sch, struct nlattr *opt,
  121. struct netlink_ext_ack *extack)
  122. {
  123. unsigned int dropped_pkts = 0, dropped_bytes = 0;
  124. struct pie_sched_data *q = qdisc_priv(sch);
  125. struct nlattr *tb[TCA_PIE_MAX + 1];
  126. int err;
  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. WRITE_ONCE(q->params.target,
  138. PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC));
  139. }
  140. /* tupdate is in jiffies */
  141. if (tb[TCA_PIE_TUPDATE])
  142. WRITE_ONCE(q->params.tupdate,
  143. usecs_to_jiffies(nla_get_u32(tb[TCA_PIE_TUPDATE])));
  144. if (tb[TCA_PIE_LIMIT]) {
  145. u32 limit = nla_get_u32(tb[TCA_PIE_LIMIT]);
  146. WRITE_ONCE(q->params.limit, limit);
  147. WRITE_ONCE(sch->limit, limit);
  148. }
  149. if (tb[TCA_PIE_ALPHA])
  150. WRITE_ONCE(q->params.alpha, nla_get_u32(tb[TCA_PIE_ALPHA]));
  151. if (tb[TCA_PIE_BETA])
  152. WRITE_ONCE(q->params.beta, nla_get_u32(tb[TCA_PIE_BETA]));
  153. if (tb[TCA_PIE_ECN])
  154. WRITE_ONCE(q->params.ecn, nla_get_u32(tb[TCA_PIE_ECN]));
  155. if (tb[TCA_PIE_BYTEMODE])
  156. WRITE_ONCE(q->params.bytemode,
  157. nla_get_u32(tb[TCA_PIE_BYTEMODE]));
  158. if (tb[TCA_PIE_DQ_RATE_ESTIMATOR])
  159. WRITE_ONCE(q->params.dq_rate_estimator,
  160. nla_get_u32(tb[TCA_PIE_DQ_RATE_ESTIMATOR]));
  161. /* Drop excess packets if new limit is lower */
  162. while (sch->q.qlen > sch->limit) {
  163. struct sk_buff *skb = qdisc_dequeue_internal(sch, true);
  164. if (!skb)
  165. break;
  166. dropped_pkts++;
  167. dropped_bytes += qdisc_pkt_len(skb);
  168. rtnl_qdisc_drop(skb, sch);
  169. }
  170. qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
  171. sch_tree_unlock(sch);
  172. return 0;
  173. }
  174. void pie_process_dequeue(struct sk_buff *skb, struct pie_params *params,
  175. struct pie_vars *vars, u32 backlog)
  176. {
  177. psched_time_t now = psched_get_time();
  178. u32 dtime = 0;
  179. /* If dq_rate_estimator is disabled, calculate qdelay using the
  180. * packet timestamp.
  181. */
  182. if (!params->dq_rate_estimator) {
  183. vars->qdelay = now - pie_get_enqueue_time(skb);
  184. if (vars->dq_tstamp != DTIME_INVALID)
  185. dtime = now - vars->dq_tstamp;
  186. vars->dq_tstamp = now;
  187. if (backlog == 0)
  188. vars->qdelay = 0;
  189. if (dtime == 0)
  190. return;
  191. goto burst_allowance_reduction;
  192. }
  193. /* If current queue is about 10 packets or more and dq_count is unset
  194. * we have enough packets to calculate the drain rate. Save
  195. * current time as dq_tstamp and start measurement cycle.
  196. */
  197. if (backlog >= QUEUE_THRESHOLD && vars->dq_count == DQCOUNT_INVALID) {
  198. vars->dq_tstamp = psched_get_time();
  199. vars->dq_count = 0;
  200. }
  201. /* Calculate the average drain rate from this value. If queue length
  202. * has receded to a small value viz., <= QUEUE_THRESHOLD bytes, reset
  203. * the dq_count to -1 as we don't have enough packets to calculate the
  204. * drain rate anymore. The following if block is entered only when we
  205. * have a substantial queue built up (QUEUE_THRESHOLD bytes or more)
  206. * and we calculate the drain rate for the threshold here. dq_count is
  207. * in bytes, time difference in psched_time, hence rate is in
  208. * bytes/psched_time.
  209. */
  210. if (vars->dq_count != DQCOUNT_INVALID) {
  211. vars->dq_count += skb->len;
  212. if (vars->dq_count >= QUEUE_THRESHOLD) {
  213. u32 count = vars->dq_count << PIE_SCALE;
  214. dtime = now - vars->dq_tstamp;
  215. if (dtime == 0)
  216. return;
  217. count = count / dtime;
  218. if (vars->avg_dq_rate == 0)
  219. vars->avg_dq_rate = count;
  220. else
  221. vars->avg_dq_rate =
  222. (vars->avg_dq_rate -
  223. (vars->avg_dq_rate >> 3)) + (count >> 3);
  224. /* If the queue has receded below the threshold, we hold
  225. * on to the last drain rate calculated, else we reset
  226. * dq_count to 0 to re-enter the if block when the next
  227. * packet is dequeued
  228. */
  229. if (backlog < QUEUE_THRESHOLD) {
  230. vars->dq_count = DQCOUNT_INVALID;
  231. } else {
  232. vars->dq_count = 0;
  233. vars->dq_tstamp = psched_get_time();
  234. }
  235. goto burst_allowance_reduction;
  236. }
  237. }
  238. return;
  239. burst_allowance_reduction:
  240. if (vars->burst_time > 0) {
  241. if (vars->burst_time > dtime)
  242. vars->burst_time -= dtime;
  243. else
  244. vars->burst_time = 0;
  245. }
  246. }
  247. EXPORT_SYMBOL_GPL(pie_process_dequeue);
  248. void pie_calculate_probability(struct pie_params *params, struct pie_vars *vars,
  249. u32 backlog)
  250. {
  251. psched_time_t qdelay = 0; /* in pschedtime */
  252. psched_time_t qdelay_old = 0; /* in pschedtime */
  253. s64 delta = 0; /* determines the change in probability */
  254. u64 oldprob;
  255. u64 alpha, beta;
  256. u32 power;
  257. bool update_prob = true;
  258. if (params->dq_rate_estimator) {
  259. qdelay_old = vars->qdelay;
  260. vars->qdelay_old = vars->qdelay;
  261. if (vars->avg_dq_rate > 0)
  262. qdelay = (backlog << PIE_SCALE) / vars->avg_dq_rate;
  263. else
  264. qdelay = 0;
  265. } else {
  266. qdelay = vars->qdelay;
  267. qdelay_old = vars->qdelay_old;
  268. }
  269. /* If qdelay is zero and backlog is not, it means backlog is very small,
  270. * so we do not update probability in this round.
  271. */
  272. if (qdelay == 0 && backlog != 0)
  273. update_prob = false;
  274. /* In the algorithm, alpha and beta are between 0 and 2 with typical
  275. * value for alpha as 0.125. In this implementation, we use values 0-32
  276. * passed from user space to represent this. Also, alpha and beta have
  277. * unit of HZ and need to be scaled before they can used to update
  278. * probability. alpha/beta are updated locally below by scaling down
  279. * by 16 to come to 0-2 range.
  280. */
  281. alpha = ((u64)params->alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
  282. beta = ((u64)params->beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
  283. /* We scale alpha and beta differently depending on how heavy the
  284. * congestion is. Please see RFC 8033 for details.
  285. */
  286. if (vars->prob < MAX_PROB / 10) {
  287. alpha >>= 1;
  288. beta >>= 1;
  289. power = 100;
  290. while (vars->prob < div_u64(MAX_PROB, power) &&
  291. power <= 1000000) {
  292. alpha >>= 2;
  293. beta >>= 2;
  294. power *= 10;
  295. }
  296. }
  297. /* alpha and beta should be between 0 and 32, in multiples of 1/16 */
  298. delta += alpha * (qdelay - params->target);
  299. delta += beta * (qdelay - qdelay_old);
  300. oldprob = vars->prob;
  301. /* to ensure we increase probability in steps of no more than 2% */
  302. if (delta > (s64)(MAX_PROB / (100 / 2)) &&
  303. 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. vars->prob += delta;
  312. if (delta > 0) {
  313. /* prevent overflow */
  314. if (vars->prob < oldprob) {
  315. 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 (vars->prob > oldprob)
  326. 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. /* Reduce drop probability to 98.4% */
  333. vars->prob -= vars->prob / 64;
  334. vars->qdelay = qdelay;
  335. vars->backlog_old = backlog;
  336. /* We restart the measurement cycle if the following conditions are met
  337. * 1. If the delay has been low for 2 consecutive Tupdate periods
  338. * 2. Calculated drop probability is zero
  339. * 3. If average dq_rate_estimator is enabled, we have at least one
  340. * estimate for the avg_dq_rate ie., is a non-zero value
  341. */
  342. if ((vars->qdelay < params->target / 2) &&
  343. (vars->qdelay_old < params->target / 2) &&
  344. vars->prob == 0 &&
  345. (!params->dq_rate_estimator || vars->avg_dq_rate > 0)) {
  346. pie_vars_init(vars);
  347. }
  348. if (!params->dq_rate_estimator)
  349. vars->qdelay_old = qdelay;
  350. }
  351. EXPORT_SYMBOL_GPL(pie_calculate_probability);
  352. static void pie_timer(struct timer_list *t)
  353. {
  354. struct pie_sched_data *q = timer_container_of(q, t, adapt_timer);
  355. struct Qdisc *sch = q->sch;
  356. spinlock_t *root_lock;
  357. rcu_read_lock();
  358. root_lock = qdisc_lock(qdisc_root_sleeping(sch));
  359. spin_lock(root_lock);
  360. pie_calculate_probability(&q->params, &q->vars, sch->qstats.backlog);
  361. /* reset the timer to fire after 'tupdate'. tupdate is in jiffies. */
  362. if (q->params.tupdate)
  363. mod_timer(&q->adapt_timer, jiffies + q->params.tupdate);
  364. spin_unlock(root_lock);
  365. rcu_read_unlock();
  366. }
  367. static int pie_init(struct Qdisc *sch, struct nlattr *opt,
  368. struct netlink_ext_ack *extack)
  369. {
  370. struct pie_sched_data *q = qdisc_priv(sch);
  371. pie_params_init(&q->params);
  372. pie_vars_init(&q->vars);
  373. sch->limit = q->params.limit;
  374. q->sch = sch;
  375. timer_setup(&q->adapt_timer, pie_timer, 0);
  376. if (opt) {
  377. int err = pie_change(sch, opt, extack);
  378. if (err)
  379. return err;
  380. }
  381. mod_timer(&q->adapt_timer, jiffies + HZ / 2);
  382. return 0;
  383. }
  384. static int pie_dump(struct Qdisc *sch, struct sk_buff *skb)
  385. {
  386. struct pie_sched_data *q = qdisc_priv(sch);
  387. struct nlattr *opts;
  388. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  389. if (!opts)
  390. goto nla_put_failure;
  391. /* convert target from pschedtime to us */
  392. if (nla_put_u32(skb, TCA_PIE_TARGET,
  393. ((u32)PSCHED_TICKS2NS(READ_ONCE(q->params.target))) /
  394. NSEC_PER_USEC) ||
  395. nla_put_u32(skb, TCA_PIE_LIMIT, READ_ONCE(sch->limit)) ||
  396. nla_put_u32(skb, TCA_PIE_TUPDATE,
  397. jiffies_to_usecs(READ_ONCE(q->params.tupdate))) ||
  398. nla_put_u32(skb, TCA_PIE_ALPHA, READ_ONCE(q->params.alpha)) ||
  399. nla_put_u32(skb, TCA_PIE_BETA, READ_ONCE(q->params.beta)) ||
  400. nla_put_u32(skb, TCA_PIE_ECN, q->params.ecn) ||
  401. nla_put_u32(skb, TCA_PIE_BYTEMODE,
  402. READ_ONCE(q->params.bytemode)) ||
  403. nla_put_u32(skb, TCA_PIE_DQ_RATE_ESTIMATOR,
  404. READ_ONCE(q->params.dq_rate_estimator)))
  405. goto nla_put_failure;
  406. return nla_nest_end(skb, opts);
  407. nla_put_failure:
  408. nla_nest_cancel(skb, opts);
  409. return -1;
  410. }
  411. static int pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  412. {
  413. struct pie_sched_data *q = qdisc_priv(sch);
  414. struct tc_pie_xstats st = {
  415. .prob = q->vars.prob << BITS_PER_BYTE,
  416. .delay = ((u32)PSCHED_TICKS2NS(q->vars.qdelay)) /
  417. NSEC_PER_USEC,
  418. .packets_in = q->stats.packets_in,
  419. .overlimit = q->stats.overlimit,
  420. .maxq = q->stats.maxq,
  421. .dropped = q->stats.dropped,
  422. .ecn_mark = q->stats.ecn_mark,
  423. };
  424. /* avg_dq_rate is only valid if dq_rate_estimator is enabled */
  425. st.dq_rate_estimating = q->params.dq_rate_estimator;
  426. /* unscale and return dq_rate in bytes per sec */
  427. if (q->params.dq_rate_estimator)
  428. st.avg_dq_rate = q->vars.avg_dq_rate *
  429. (PSCHED_TICKS_PER_SEC) >> PIE_SCALE;
  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 pie_sched_data *q = qdisc_priv(sch);
  435. struct sk_buff *skb = qdisc_dequeue_head(sch);
  436. if (!skb)
  437. return NULL;
  438. pie_process_dequeue(skb, &q->params, &q->vars, sch->qstats.backlog);
  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. timer_delete_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. MODULE_ALIAS_NET_SCH("pie");
  468. static int __init pie_module_init(void)
  469. {
  470. return register_qdisc(&pie_qdisc_ops);
  471. }
  472. static void __exit pie_module_exit(void)
  473. {
  474. unregister_qdisc(&pie_qdisc_ops);
  475. }
  476. module_init(pie_module_init);
  477. module_exit(pie_module_exit);
  478. MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler");
  479. MODULE_AUTHOR("Vijay Subramanian");
  480. MODULE_AUTHOR("Mythili Prabhu");
  481. MODULE_LICENSE("GPL");