sch_gred.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 = 0; n < table->DPs; n++)
  89. if (table->tab[n] && table->tab[n] != q &&
  90. table->tab[n]->prio == q->prio)
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. static inline unsigned int gred_backlog(struct gred_sched *table,
  96. struct gred_sched_data *q,
  97. struct Qdisc *sch)
  98. {
  99. if (gred_wred_mode(table))
  100. return sch->qstats.backlog;
  101. else
  102. return q->backlog;
  103. }
  104. static inline u16 tc_index_to_dp(struct sk_buff *skb)
  105. {
  106. return skb->tc_index & GRED_VQ_MASK;
  107. }
  108. static inline void gred_load_wred_set(const struct gred_sched *table,
  109. struct gred_sched_data *q)
  110. {
  111. q->vars.qavg = table->wred_set.qavg;
  112. q->vars.qidlestart = table->wred_set.qidlestart;
  113. }
  114. static inline void gred_store_wred_set(struct gred_sched *table,
  115. struct gred_sched_data *q)
  116. {
  117. table->wred_set.qavg = q->vars.qavg;
  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. {
  129. struct gred_sched_data *q = NULL;
  130. struct gred_sched *t = qdisc_priv(sch);
  131. unsigned long qavg = 0;
  132. u16 dp = tc_index_to_dp(skb);
  133. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  134. dp = t->def;
  135. q = t->tab[dp];
  136. if (!q) {
  137. /* Pass through packets not assigned to a DP
  138. * if no default DP has been configured. This
  139. * allows for DP flows to be left untouched.
  140. */
  141. if (skb_queue_len(&sch->q) < qdisc_dev(sch)->tx_queue_len)
  142. return qdisc_enqueue_tail(skb, sch);
  143. else
  144. goto drop;
  145. }
  146. /* fix tc_index? --could be controversial but needed for
  147. requeueing */
  148. skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
  149. }
  150. /* sum up all the qaves of prios <= to ours to get the new qave */
  151. if (!gred_wred_mode(t) && gred_rio_mode(t)) {
  152. int i;
  153. for (i = 0; i < t->DPs; i++) {
  154. if (t->tab[i] && t->tab[i]->prio < q->prio &&
  155. !red_is_idling(&t->tab[i]->vars))
  156. qavg += t->tab[i]->vars.qavg;
  157. }
  158. }
  159. q->packetsin++;
  160. q->bytesin += qdisc_pkt_len(skb);
  161. if (gred_wred_mode(t))
  162. gred_load_wred_set(t, q);
  163. q->vars.qavg = red_calc_qavg(&q->parms,
  164. &q->vars,
  165. gred_backlog(t, q, sch));
  166. if (red_is_idling(&q->vars))
  167. red_end_of_idle_period(&q->vars);
  168. if (gred_wred_mode(t))
  169. gred_store_wred_set(t, q);
  170. switch (red_action(&q->parms, &q->vars, q->vars.qavg + qavg)) {
  171. case RED_DONT_MARK:
  172. break;
  173. case RED_PROB_MARK:
  174. sch->qstats.overlimits++;
  175. if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
  176. q->stats.prob_drop++;
  177. goto congestion_drop;
  178. }
  179. q->stats.prob_mark++;
  180. break;
  181. case RED_HARD_MARK:
  182. sch->qstats.overlimits++;
  183. if (gred_use_harddrop(t) || !gred_use_ecn(t) ||
  184. !INET_ECN_set_ce(skb)) {
  185. q->stats.forced_drop++;
  186. goto congestion_drop;
  187. }
  188. q->stats.forced_mark++;
  189. break;
  190. }
  191. if (q->backlog + qdisc_pkt_len(skb) <= q->limit) {
  192. q->backlog += qdisc_pkt_len(skb);
  193. return qdisc_enqueue_tail(skb, sch);
  194. }
  195. q->stats.pdrop++;
  196. drop:
  197. return qdisc_drop(skb, sch);
  198. congestion_drop:
  199. qdisc_drop(skb, sch);
  200. return NET_XMIT_CN;
  201. }
  202. static struct sk_buff *gred_dequeue(struct Qdisc *sch)
  203. {
  204. struct sk_buff *skb;
  205. struct gred_sched *t = qdisc_priv(sch);
  206. skb = qdisc_dequeue_head(sch);
  207. if (skb) {
  208. struct gred_sched_data *q;
  209. u16 dp = tc_index_to_dp(skb);
  210. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  211. net_warn_ratelimited("GRED: Unable to relocate VQ 0x%x after dequeue, screwing up backlog\n",
  212. tc_index_to_dp(skb));
  213. } else {
  214. q->backlog -= qdisc_pkt_len(skb);
  215. if (!q->backlog && !gred_wred_mode(t))
  216. red_start_of_idle_period(&q->vars);
  217. }
  218. return skb;
  219. }
  220. if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
  221. red_start_of_idle_period(&t->wred_set);
  222. return NULL;
  223. }
  224. static unsigned int gred_drop(struct Qdisc *sch)
  225. {
  226. struct sk_buff *skb;
  227. struct gred_sched *t = qdisc_priv(sch);
  228. skb = qdisc_dequeue_tail(sch);
  229. if (skb) {
  230. unsigned int len = qdisc_pkt_len(skb);
  231. struct gred_sched_data *q;
  232. u16 dp = tc_index_to_dp(skb);
  233. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  234. net_warn_ratelimited("GRED: Unable to relocate VQ 0x%x while dropping, screwing up backlog\n",
  235. tc_index_to_dp(skb));
  236. } else {
  237. q->backlog -= len;
  238. q->stats.other++;
  239. if (!q->backlog && !gred_wred_mode(t))
  240. red_start_of_idle_period(&q->vars);
  241. }
  242. qdisc_drop(skb, sch);
  243. return len;
  244. }
  245. if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
  246. red_start_of_idle_period(&t->wred_set);
  247. return 0;
  248. }
  249. static void gred_reset(struct Qdisc *sch)
  250. {
  251. int i;
  252. struct gred_sched *t = qdisc_priv(sch);
  253. qdisc_reset_queue(sch);
  254. for (i = 0; i < t->DPs; i++) {
  255. struct gred_sched_data *q = t->tab[i];
  256. if (!q)
  257. continue;
  258. red_restart(&q->vars);
  259. q->backlog = 0;
  260. }
  261. }
  262. static inline void gred_destroy_vq(struct gred_sched_data *q)
  263. {
  264. kfree(q);
  265. }
  266. static inline int gred_change_table_def(struct Qdisc *sch, struct nlattr *dps)
  267. {
  268. struct gred_sched *table = qdisc_priv(sch);
  269. struct tc_gred_sopt *sopt;
  270. int i;
  271. if (dps == NULL)
  272. return -EINVAL;
  273. sopt = nla_data(dps);
  274. if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
  275. return -EINVAL;
  276. sch_tree_lock(sch);
  277. table->DPs = sopt->DPs;
  278. table->def = sopt->def_DP;
  279. table->red_flags = sopt->flags;
  280. /*
  281. * Every entry point to GRED is synchronized with the above code
  282. * and the DP is checked against DPs, i.e. shadowed VQs can no
  283. * longer be found so we can unlock right here.
  284. */
  285. sch_tree_unlock(sch);
  286. if (sopt->grio) {
  287. gred_enable_rio_mode(table);
  288. gred_disable_wred_mode(table);
  289. if (gred_wred_mode_check(sch))
  290. gred_enable_wred_mode(table);
  291. } else {
  292. gred_disable_rio_mode(table);
  293. gred_disable_wred_mode(table);
  294. }
  295. for (i = table->DPs; i < MAX_DPs; i++) {
  296. if (table->tab[i]) {
  297. pr_warning("GRED: Warning: Destroying "
  298. "shadowed VQ 0x%x\n", i);
  299. gred_destroy_vq(table->tab[i]);
  300. table->tab[i] = NULL;
  301. }
  302. }
  303. return 0;
  304. }
  305. static inline int gred_change_vq(struct Qdisc *sch, int dp,
  306. struct tc_gred_qopt *ctl, int prio,
  307. u8 *stab, u32 max_P,
  308. struct gred_sched_data **prealloc)
  309. {
  310. struct gred_sched *table = qdisc_priv(sch);
  311. struct gred_sched_data *q = table->tab[dp];
  312. if (!q) {
  313. table->tab[dp] = q = *prealloc;
  314. *prealloc = NULL;
  315. if (!q)
  316. return -ENOMEM;
  317. }
  318. q->DP = dp;
  319. q->prio = prio;
  320. q->limit = ctl->limit;
  321. if (q->backlog == 0)
  322. red_end_of_idle_period(&q->vars);
  323. red_set_parms(&q->parms,
  324. ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
  325. ctl->Scell_log, stab, max_P);
  326. red_set_vars(&q->vars);
  327. return 0;
  328. }
  329. static const struct nla_policy gred_policy[TCA_GRED_MAX + 1] = {
  330. [TCA_GRED_PARMS] = { .len = sizeof(struct tc_gred_qopt) },
  331. [TCA_GRED_STAB] = { .len = 256 },
  332. [TCA_GRED_DPS] = { .len = sizeof(struct tc_gred_sopt) },
  333. [TCA_GRED_MAX_P] = { .type = NLA_U32 },
  334. };
  335. static int gred_change(struct Qdisc *sch, struct nlattr *opt)
  336. {
  337. struct gred_sched *table = qdisc_priv(sch);
  338. struct tc_gred_qopt *ctl;
  339. struct nlattr *tb[TCA_GRED_MAX + 1];
  340. int err, prio = GRED_DEF_PRIO;
  341. u8 *stab;
  342. u32 max_P;
  343. struct gred_sched_data *prealloc;
  344. if (opt == NULL)
  345. return -EINVAL;
  346. err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
  347. if (err < 0)
  348. return err;
  349. if (tb[TCA_GRED_PARMS] == NULL && tb[TCA_GRED_STAB] == NULL)
  350. return gred_change_table_def(sch, opt);
  351. if (tb[TCA_GRED_PARMS] == NULL ||
  352. tb[TCA_GRED_STAB] == NULL)
  353. return -EINVAL;
  354. max_P = tb[TCA_GRED_MAX_P] ? nla_get_u32(tb[TCA_GRED_MAX_P]) : 0;
  355. err = -EINVAL;
  356. ctl = nla_data(tb[TCA_GRED_PARMS]);
  357. stab = nla_data(tb[TCA_GRED_STAB]);
  358. if (ctl->DP >= table->DPs)
  359. goto errout;
  360. if (gred_rio_mode(table)) {
  361. if (ctl->prio == 0) {
  362. int def_prio = GRED_DEF_PRIO;
  363. if (table->tab[table->def])
  364. def_prio = table->tab[table->def]->prio;
  365. printk(KERN_DEBUG "GRED: DP %u does not have a prio "
  366. "setting default to %d\n", ctl->DP, def_prio);
  367. prio = def_prio;
  368. } else
  369. prio = ctl->prio;
  370. }
  371. prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
  372. sch_tree_lock(sch);
  373. err = gred_change_vq(sch, ctl->DP, ctl, prio, stab, max_P, &prealloc);
  374. if (err < 0)
  375. goto errout_locked;
  376. if (gred_rio_mode(table)) {
  377. gred_disable_wred_mode(table);
  378. if (gred_wred_mode_check(sch))
  379. gred_enable_wred_mode(table);
  380. }
  381. err = 0;
  382. errout_locked:
  383. sch_tree_unlock(sch);
  384. kfree(prealloc);
  385. errout:
  386. return err;
  387. }
  388. static int gred_init(struct Qdisc *sch, struct nlattr *opt)
  389. {
  390. struct nlattr *tb[TCA_GRED_MAX + 1];
  391. int err;
  392. if (opt == NULL)
  393. return -EINVAL;
  394. err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
  395. if (err < 0)
  396. return err;
  397. if (tb[TCA_GRED_PARMS] || tb[TCA_GRED_STAB])
  398. return -EINVAL;
  399. return gred_change_table_def(sch, tb[TCA_GRED_DPS]);
  400. }
  401. static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
  402. {
  403. struct gred_sched *table = qdisc_priv(sch);
  404. struct nlattr *parms, *opts = NULL;
  405. int i;
  406. u32 max_p[MAX_DPs];
  407. struct tc_gred_sopt sopt = {
  408. .DPs = table->DPs,
  409. .def_DP = table->def,
  410. .grio = gred_rio_mode(table),
  411. .flags = table->red_flags,
  412. };
  413. opts = nla_nest_start(skb, TCA_OPTIONS);
  414. if (opts == NULL)
  415. goto nla_put_failure;
  416. NLA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
  417. for (i = 0; i < MAX_DPs; i++) {
  418. struct gred_sched_data *q = table->tab[i];
  419. max_p[i] = q ? q->parms.max_P : 0;
  420. }
  421. NLA_PUT(skb, TCA_GRED_MAX_P, sizeof(max_p), max_p);
  422. parms = nla_nest_start(skb, TCA_GRED_PARMS);
  423. if (parms == NULL)
  424. goto nla_put_failure;
  425. for (i = 0; i < MAX_DPs; i++) {
  426. struct gred_sched_data *q = table->tab[i];
  427. struct tc_gred_qopt opt;
  428. memset(&opt, 0, sizeof(opt));
  429. if (!q) {
  430. /* hack -- fix at some point with proper message
  431. This is how we indicate to tc that there is no VQ
  432. at this DP */
  433. opt.DP = MAX_DPs + i;
  434. goto append_opt;
  435. }
  436. opt.limit = q->limit;
  437. opt.DP = q->DP;
  438. opt.backlog = q->backlog;
  439. opt.prio = q->prio;
  440. opt.qth_min = q->parms.qth_min >> q->parms.Wlog;
  441. opt.qth_max = q->parms.qth_max >> q->parms.Wlog;
  442. opt.Wlog = q->parms.Wlog;
  443. opt.Plog = q->parms.Plog;
  444. opt.Scell_log = q->parms.Scell_log;
  445. opt.other = q->stats.other;
  446. opt.early = q->stats.prob_drop;
  447. opt.forced = q->stats.forced_drop;
  448. opt.pdrop = q->stats.pdrop;
  449. opt.packets = q->packetsin;
  450. opt.bytesin = q->bytesin;
  451. if (gred_wred_mode(table))
  452. gred_load_wred_set(table, q);
  453. opt.qave = red_calc_qavg(&q->parms, &q->vars, q->vars.qavg);
  454. append_opt:
  455. if (nla_append(skb, sizeof(opt), &opt) < 0)
  456. goto nla_put_failure;
  457. }
  458. nla_nest_end(skb, parms);
  459. return nla_nest_end(skb, opts);
  460. nla_put_failure:
  461. nla_nest_cancel(skb, opts);
  462. return -EMSGSIZE;
  463. }
  464. static void gred_destroy(struct Qdisc *sch)
  465. {
  466. struct gred_sched *table = qdisc_priv(sch);
  467. int i;
  468. for (i = 0; i < table->DPs; i++) {
  469. if (table->tab[i])
  470. gred_destroy_vq(table->tab[i]);
  471. }
  472. }
  473. static struct Qdisc_ops gred_qdisc_ops __read_mostly = {
  474. .id = "gred",
  475. .priv_size = sizeof(struct gred_sched),
  476. .enqueue = gred_enqueue,
  477. .dequeue = gred_dequeue,
  478. .peek = qdisc_peek_head,
  479. .drop = gred_drop,
  480. .init = gred_init,
  481. .reset = gred_reset,
  482. .destroy = gred_destroy,
  483. .change = gred_change,
  484. .dump = gred_dump,
  485. .owner = THIS_MODULE,
  486. };
  487. static int __init gred_module_init(void)
  488. {
  489. return register_qdisc(&gred_qdisc_ops);
  490. }
  491. static void __exit gred_module_exit(void)
  492. {
  493. unregister_qdisc(&gred_qdisc_ops);
  494. }
  495. module_init(gred_module_init)
  496. module_exit(gred_module_exit)
  497. MODULE_LICENSE("GPL");