sch_gred.c 14 KB

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