sch_gred.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * net/sched/sch_gred.c Generic Random Early Detection queue.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * Authors: J Hadi Salim (hadi@cyberus.ca) 1998-2002
  11. *
  12. * 991129: - Bug fix with grio mode
  13. * - a better sing. AvgQ mode with Grio(WRED)
  14. * - A finer grained VQ dequeue based on sugestion
  15. * from Ren Liu
  16. * - More error checks
  17. *
  18. * For all the glorious comments look at include/net/red.h
  19. */
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/skbuff.h>
  25. #include <net/pkt_sched.h>
  26. #include <net/red.h>
  27. #define GRED_DEF_PRIO (MAX_DPs / 2)
  28. #define GRED_VQ_MASK (MAX_DPs - 1)
  29. struct gred_sched_data;
  30. struct gred_sched;
  31. struct gred_sched_data {
  32. u32 limit; /* HARD maximal queue length */
  33. u32 DP; /* the drop parameters */
  34. u32 bytesin; /* bytes seen on virtualQ so far*/
  35. u32 packetsin; /* packets seen on virtualQ so far*/
  36. u32 backlog; /* bytes on the virtualQ */
  37. u8 prio; /* the prio of this vq */
  38. struct red_parms parms;
  39. struct red_vars vars;
  40. struct red_stats stats;
  41. };
  42. enum {
  43. GRED_WRED_MODE = 1,
  44. GRED_RIO_MODE,
  45. };
  46. struct gred_sched {
  47. struct gred_sched_data *tab[MAX_DPs];
  48. unsigned long flags;
  49. u32 red_flags;
  50. u32 DPs;
  51. u32 def;
  52. struct red_vars wred_set;
  53. };
  54. static inline int gred_wred_mode(struct gred_sched *table)
  55. {
  56. return test_bit(GRED_WRED_MODE, &table->flags);
  57. }
  58. static inline void gred_enable_wred_mode(struct gred_sched *table)
  59. {
  60. __set_bit(GRED_WRED_MODE, &table->flags);
  61. }
  62. static inline void gred_disable_wred_mode(struct gred_sched *table)
  63. {
  64. __clear_bit(GRED_WRED_MODE, &table->flags);
  65. }
  66. static inline int gred_rio_mode(struct gred_sched *table)
  67. {
  68. return test_bit(GRED_RIO_MODE, &table->flags);
  69. }
  70. static inline void gred_enable_rio_mode(struct gred_sched *table)
  71. {
  72. __set_bit(GRED_RIO_MODE, &table->flags);
  73. }
  74. static inline void gred_disable_rio_mode(struct gred_sched *table)
  75. {
  76. __clear_bit(GRED_RIO_MODE, &table->flags);
  77. }
  78. static inline int gred_wred_mode_check(struct Qdisc *sch)
  79. {
  80. struct gred_sched *table = qdisc_priv(sch);
  81. int i;
  82. /* Really ugly O(n^2) but shouldn't be necessary too frequent. */
  83. for (i = 0; i < table->DPs; i++) {
  84. struct gred_sched_data *q = table->tab[i];
  85. int n;
  86. if (q == NULL)
  87. continue;
  88. for (n = i + 1; n < table->DPs; n++)
  89. if (table->tab[n] && table->tab[n]->prio == q->prio)
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. static inline unsigned int gred_backlog(struct gred_sched *table,
  95. struct gred_sched_data *q,
  96. struct Qdisc *sch)
  97. {
  98. if (gred_wred_mode(table))
  99. return sch->qstats.backlog;
  100. else
  101. return q->backlog;
  102. }
  103. static inline u16 tc_index_to_dp(struct sk_buff *skb)
  104. {
  105. return skb->tc_index & GRED_VQ_MASK;
  106. }
  107. static inline void gred_load_wred_set(const struct gred_sched *table,
  108. struct gred_sched_data *q)
  109. {
  110. q->vars.qavg = table->wred_set.qavg;
  111. q->vars.qidlestart = table->wred_set.qidlestart;
  112. }
  113. static inline void gred_store_wred_set(struct gred_sched *table,
  114. struct gred_sched_data *q)
  115. {
  116. table->wred_set.qavg = q->vars.qavg;
  117. table->wred_set.qidlestart = q->vars.qidlestart;
  118. }
  119. static inline int gred_use_ecn(struct gred_sched *t)
  120. {
  121. return t->red_flags & TC_RED_ECN;
  122. }
  123. static inline int gred_use_harddrop(struct gred_sched *t)
  124. {
  125. return t->red_flags & TC_RED_HARDDROP;
  126. }
  127. static int gred_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  128. struct sk_buff **to_free)
  129. {
  130. struct gred_sched_data *q = NULL;
  131. struct gred_sched *t = qdisc_priv(sch);
  132. unsigned long qavg = 0;
  133. u16 dp = tc_index_to_dp(skb);
  134. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  135. dp = t->def;
  136. q = t->tab[dp];
  137. if (!q) {
  138. /* Pass through packets not assigned to a DP
  139. * if no default DP has been configured. This
  140. * allows for DP flows to be left untouched.
  141. */
  142. if (likely(sch->qstats.backlog + qdisc_pkt_len(skb) <=
  143. sch->limit))
  144. return qdisc_enqueue_tail(skb, sch);
  145. else
  146. goto drop;
  147. }
  148. /* fix tc_index? --could be controversial but needed for
  149. requeueing */
  150. skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
  151. }
  152. /* sum up all the qaves of prios < ours to get the new qave */
  153. if (!gred_wred_mode(t) && gred_rio_mode(t)) {
  154. int i;
  155. for (i = 0; i < t->DPs; i++) {
  156. if (t->tab[i] && t->tab[i]->prio < q->prio &&
  157. !red_is_idling(&t->tab[i]->vars))
  158. qavg += t->tab[i]->vars.qavg;
  159. }
  160. }
  161. q->packetsin++;
  162. q->bytesin += qdisc_pkt_len(skb);
  163. if (gred_wred_mode(t))
  164. gred_load_wred_set(t, q);
  165. q->vars.qavg = red_calc_qavg(&q->parms,
  166. &q->vars,
  167. gred_backlog(t, q, sch));
  168. if (red_is_idling(&q->vars))
  169. red_end_of_idle_period(&q->vars);
  170. if (gred_wred_mode(t))
  171. gred_store_wred_set(t, q);
  172. switch (red_action(&q->parms, &q->vars, q->vars.qavg + qavg)) {
  173. case RED_DONT_MARK:
  174. break;
  175. case RED_PROB_MARK:
  176. qdisc_qstats_overlimit(sch);
  177. if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
  178. q->stats.prob_drop++;
  179. goto congestion_drop;
  180. }
  181. q->stats.prob_mark++;
  182. break;
  183. case RED_HARD_MARK:
  184. qdisc_qstats_overlimit(sch);
  185. if (gred_use_harddrop(t) || !gred_use_ecn(t) ||
  186. !INET_ECN_set_ce(skb)) {
  187. q->stats.forced_drop++;
  188. goto congestion_drop;
  189. }
  190. q->stats.forced_mark++;
  191. break;
  192. }
  193. if (gred_backlog(t, q, sch) + qdisc_pkt_len(skb) <= q->limit) {
  194. q->backlog += qdisc_pkt_len(skb);
  195. return qdisc_enqueue_tail(skb, sch);
  196. }
  197. q->stats.pdrop++;
  198. drop:
  199. return qdisc_drop(skb, sch, to_free);
  200. congestion_drop:
  201. qdisc_drop(skb, sch, to_free);
  202. return NET_XMIT_CN;
  203. }
  204. static struct sk_buff *gred_dequeue(struct Qdisc *sch)
  205. {
  206. struct sk_buff *skb;
  207. struct gred_sched *t = qdisc_priv(sch);
  208. skb = qdisc_dequeue_head(sch);
  209. if (skb) {
  210. struct gred_sched_data *q;
  211. u16 dp = tc_index_to_dp(skb);
  212. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  213. net_warn_ratelimited("GRED: Unable to relocate VQ 0x%x after dequeue, screwing up backlog\n",
  214. tc_index_to_dp(skb));
  215. } else {
  216. q->backlog -= qdisc_pkt_len(skb);
  217. if (gred_wred_mode(t)) {
  218. if (!sch->qstats.backlog)
  219. red_start_of_idle_period(&t->wred_set);
  220. } else {
  221. if (!q->backlog)
  222. red_start_of_idle_period(&q->vars);
  223. }
  224. }
  225. return skb;
  226. }
  227. return NULL;
  228. }
  229. static void gred_reset(struct Qdisc *sch)
  230. {
  231. int i;
  232. struct gred_sched *t = qdisc_priv(sch);
  233. qdisc_reset_queue(sch);
  234. for (i = 0; i < t->DPs; i++) {
  235. struct gred_sched_data *q = t->tab[i];
  236. if (!q)
  237. continue;
  238. red_restart(&q->vars);
  239. q->backlog = 0;
  240. }
  241. }
  242. static inline void gred_destroy_vq(struct gred_sched_data *q)
  243. {
  244. kfree(q);
  245. }
  246. static inline int gred_change_table_def(struct Qdisc *sch, struct nlattr *dps)
  247. {
  248. struct gred_sched *table = qdisc_priv(sch);
  249. struct tc_gred_sopt *sopt;
  250. int i;
  251. if (!dps)
  252. return -EINVAL;
  253. sopt = nla_data(dps);
  254. if (sopt->DPs > MAX_DPs || sopt->DPs == 0 ||
  255. sopt->def_DP >= sopt->DPs)
  256. return -EINVAL;
  257. sch_tree_lock(sch);
  258. table->DPs = sopt->DPs;
  259. table->def = sopt->def_DP;
  260. table->red_flags = sopt->flags;
  261. /*
  262. * Every entry point to GRED is synchronized with the above code
  263. * and the DP is checked against DPs, i.e. shadowed VQs can no
  264. * longer be found so we can unlock right here.
  265. */
  266. sch_tree_unlock(sch);
  267. if (sopt->grio) {
  268. gred_enable_rio_mode(table);
  269. gred_disable_wred_mode(table);
  270. if (gred_wred_mode_check(sch))
  271. gred_enable_wred_mode(table);
  272. } else {
  273. gred_disable_rio_mode(table);
  274. gred_disable_wred_mode(table);
  275. }
  276. for (i = table->DPs; i < MAX_DPs; i++) {
  277. if (table->tab[i]) {
  278. pr_warn("GRED: Warning: Destroying shadowed VQ 0x%x\n",
  279. i);
  280. gred_destroy_vq(table->tab[i]);
  281. table->tab[i] = NULL;
  282. }
  283. }
  284. return 0;
  285. }
  286. static inline int gred_change_vq(struct Qdisc *sch, int dp,
  287. struct tc_gred_qopt *ctl, int prio,
  288. u8 *stab, u32 max_P,
  289. struct gred_sched_data **prealloc)
  290. {
  291. struct gred_sched *table = qdisc_priv(sch);
  292. struct gred_sched_data *q = table->tab[dp];
  293. if (!red_check_params(ctl->qth_min, ctl->qth_max, ctl->Wlog))
  294. return -EINVAL;
  295. if (!q) {
  296. table->tab[dp] = q = *prealloc;
  297. *prealloc = NULL;
  298. if (!q)
  299. return -ENOMEM;
  300. }
  301. q->DP = dp;
  302. q->prio = prio;
  303. if (ctl->limit > sch->limit)
  304. q->limit = sch->limit;
  305. else
  306. q->limit = ctl->limit;
  307. if (q->backlog == 0)
  308. red_end_of_idle_period(&q->vars);
  309. red_set_parms(&q->parms,
  310. ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
  311. ctl->Scell_log, stab, max_P);
  312. red_set_vars(&q->vars);
  313. return 0;
  314. }
  315. static const struct nla_policy gred_policy[TCA_GRED_MAX + 1] = {
  316. [TCA_GRED_PARMS] = { .len = sizeof(struct tc_gred_qopt) },
  317. [TCA_GRED_STAB] = { .len = 256 },
  318. [TCA_GRED_DPS] = { .len = sizeof(struct tc_gred_sopt) },
  319. [TCA_GRED_MAX_P] = { .type = NLA_U32 },
  320. [TCA_GRED_LIMIT] = { .type = NLA_U32 },
  321. };
  322. static int gred_change(struct Qdisc *sch, struct nlattr *opt,
  323. struct netlink_ext_ack *extack)
  324. {
  325. struct gred_sched *table = qdisc_priv(sch);
  326. struct tc_gred_qopt *ctl;
  327. struct nlattr *tb[TCA_GRED_MAX + 1];
  328. int err, prio = GRED_DEF_PRIO;
  329. u8 *stab;
  330. u32 max_P;
  331. struct gred_sched_data *prealloc;
  332. if (opt == NULL)
  333. return -EINVAL;
  334. err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy, NULL);
  335. if (err < 0)
  336. return err;
  337. if (tb[TCA_GRED_PARMS] == NULL && tb[TCA_GRED_STAB] == NULL) {
  338. if (tb[TCA_GRED_LIMIT] != NULL)
  339. sch->limit = nla_get_u32(tb[TCA_GRED_LIMIT]);
  340. return gred_change_table_def(sch, tb[TCA_GRED_DPS]);
  341. }
  342. if (tb[TCA_GRED_PARMS] == NULL ||
  343. tb[TCA_GRED_STAB] == NULL ||
  344. tb[TCA_GRED_LIMIT] != NULL)
  345. return -EINVAL;
  346. max_P = tb[TCA_GRED_MAX_P] ? nla_get_u32(tb[TCA_GRED_MAX_P]) : 0;
  347. err = -EINVAL;
  348. ctl = nla_data(tb[TCA_GRED_PARMS]);
  349. stab = nla_data(tb[TCA_GRED_STAB]);
  350. if (ctl->DP >= table->DPs)
  351. goto errout;
  352. if (gred_rio_mode(table)) {
  353. if (ctl->prio == 0) {
  354. int def_prio = GRED_DEF_PRIO;
  355. if (table->tab[table->def])
  356. def_prio = table->tab[table->def]->prio;
  357. printk(KERN_DEBUG "GRED: DP %u does not have a prio "
  358. "setting default to %d\n", ctl->DP, def_prio);
  359. prio = def_prio;
  360. } else
  361. prio = ctl->prio;
  362. }
  363. prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
  364. sch_tree_lock(sch);
  365. err = gred_change_vq(sch, ctl->DP, ctl, prio, stab, max_P, &prealloc);
  366. if (err < 0)
  367. goto errout_locked;
  368. if (gred_rio_mode(table)) {
  369. gred_disable_wred_mode(table);
  370. if (gred_wred_mode_check(sch))
  371. gred_enable_wred_mode(table);
  372. }
  373. err = 0;
  374. errout_locked:
  375. sch_tree_unlock(sch);
  376. kfree(prealloc);
  377. errout:
  378. return err;
  379. }
  380. static int gred_init(struct Qdisc *sch, struct nlattr *opt,
  381. struct netlink_ext_ack *extack)
  382. {
  383. struct nlattr *tb[TCA_GRED_MAX + 1];
  384. int err;
  385. if (!opt)
  386. return -EINVAL;
  387. err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy, NULL);
  388. if (err < 0)
  389. return err;
  390. if (tb[TCA_GRED_PARMS] || tb[TCA_GRED_STAB])
  391. return -EINVAL;
  392. if (tb[TCA_GRED_LIMIT])
  393. sch->limit = nla_get_u32(tb[TCA_GRED_LIMIT]);
  394. else
  395. sch->limit = qdisc_dev(sch)->tx_queue_len
  396. * psched_mtu(qdisc_dev(sch));
  397. return gred_change_table_def(sch, tb[TCA_GRED_DPS]);
  398. }
  399. static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
  400. {
  401. struct gred_sched *table = qdisc_priv(sch);
  402. struct nlattr *parms, *opts = NULL;
  403. int i;
  404. u32 max_p[MAX_DPs];
  405. struct tc_gred_sopt sopt = {
  406. .DPs = table->DPs,
  407. .def_DP = table->def,
  408. .grio = gred_rio_mode(table),
  409. .flags = table->red_flags,
  410. };
  411. opts = nla_nest_start(skb, TCA_OPTIONS);
  412. if (opts == NULL)
  413. goto nla_put_failure;
  414. if (nla_put(skb, TCA_GRED_DPS, sizeof(sopt), &sopt))
  415. goto nla_put_failure;
  416. for (i = 0; i < MAX_DPs; i++) {
  417. struct gred_sched_data *q = table->tab[i];
  418. max_p[i] = q ? q->parms.max_P : 0;
  419. }
  420. if (nla_put(skb, TCA_GRED_MAX_P, sizeof(max_p), max_p))
  421. goto nla_put_failure;
  422. if (nla_put_u32(skb, TCA_GRED_LIMIT, sch->limit))
  423. goto nla_put_failure;
  424. parms = nla_nest_start(skb, TCA_GRED_PARMS);
  425. if (parms == NULL)
  426. goto nla_put_failure;
  427. for (i = 0; i < MAX_DPs; i++) {
  428. struct gred_sched_data *q = table->tab[i];
  429. struct tc_gred_qopt opt;
  430. unsigned long qavg;
  431. memset(&opt, 0, sizeof(opt));
  432. if (!q) {
  433. /* hack -- fix at some point with proper message
  434. This is how we indicate to tc that there is no VQ
  435. at this DP */
  436. opt.DP = MAX_DPs + i;
  437. goto append_opt;
  438. }
  439. opt.limit = q->limit;
  440. opt.DP = q->DP;
  441. opt.backlog = gred_backlog(table, q, sch);
  442. opt.prio = q->prio;
  443. opt.qth_min = q->parms.qth_min >> q->parms.Wlog;
  444. opt.qth_max = q->parms.qth_max >> q->parms.Wlog;
  445. opt.Wlog = q->parms.Wlog;
  446. opt.Plog = q->parms.Plog;
  447. opt.Scell_log = q->parms.Scell_log;
  448. opt.other = q->stats.other;
  449. opt.early = q->stats.prob_drop;
  450. opt.forced = q->stats.forced_drop;
  451. opt.pdrop = q->stats.pdrop;
  452. opt.packets = q->packetsin;
  453. opt.bytesin = q->bytesin;
  454. if (gred_wred_mode(table))
  455. gred_load_wred_set(table, q);
  456. qavg = red_calc_qavg(&q->parms, &q->vars,
  457. q->vars.qavg >> q->parms.Wlog);
  458. opt.qave = qavg >> q->parms.Wlog;
  459. append_opt:
  460. if (nla_append(skb, sizeof(opt), &opt) < 0)
  461. goto nla_put_failure;
  462. }
  463. nla_nest_end(skb, parms);
  464. return nla_nest_end(skb, opts);
  465. nla_put_failure:
  466. nla_nest_cancel(skb, opts);
  467. return -EMSGSIZE;
  468. }
  469. static void gred_destroy(struct Qdisc *sch)
  470. {
  471. struct gred_sched *table = qdisc_priv(sch);
  472. int i;
  473. for (i = 0; i < table->DPs; i++) {
  474. if (table->tab[i])
  475. gred_destroy_vq(table->tab[i]);
  476. }
  477. }
  478. static struct Qdisc_ops gred_qdisc_ops __read_mostly = {
  479. .id = "gred",
  480. .priv_size = sizeof(struct gred_sched),
  481. .enqueue = gred_enqueue,
  482. .dequeue = gred_dequeue,
  483. .peek = qdisc_peek_head,
  484. .init = gred_init,
  485. .reset = gred_reset,
  486. .destroy = gred_destroy,
  487. .change = gred_change,
  488. .dump = gred_dump,
  489. .owner = THIS_MODULE,
  490. };
  491. static int __init gred_module_init(void)
  492. {
  493. return register_qdisc(&gred_qdisc_ops);
  494. }
  495. static void __exit gred_module_exit(void)
  496. {
  497. unregister_qdisc(&gred_qdisc_ops);
  498. }
  499. module_init(gred_module_init)
  500. module_exit(gred_module_exit)
  501. MODULE_LICENSE("GPL");