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 (!red_check_params(ctl->qth_min, ctl->qth_max, ctl->Wlog))
  320. return -EINVAL;
  321. if (!q) {
  322. table->tab[dp] = q = *prealloc;
  323. *prealloc = NULL;
  324. if (!q)
  325. return -ENOMEM;
  326. }
  327. q->DP = dp;
  328. q->prio = prio;
  329. if (ctl->limit > sch->limit)
  330. q->limit = sch->limit;
  331. else
  332. q->limit = ctl->limit;
  333. if (q->backlog == 0)
  334. red_end_of_idle_period(&q->vars);
  335. red_set_parms(&q->parms,
  336. ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
  337. ctl->Scell_log, stab, max_P);
  338. red_set_vars(&q->vars);
  339. return 0;
  340. }
  341. static const struct nla_policy gred_policy[TCA_GRED_MAX + 1] = {
  342. [TCA_GRED_PARMS] = { .len = sizeof(struct tc_gred_qopt) },
  343. [TCA_GRED_STAB] = { .len = 256 },
  344. [TCA_GRED_DPS] = { .len = sizeof(struct tc_gred_sopt) },
  345. [TCA_GRED_MAX_P] = { .type = NLA_U32 },
  346. [TCA_GRED_LIMIT] = { .type = NLA_U32 },
  347. };
  348. static int gred_change(struct Qdisc *sch, struct nlattr *opt)
  349. {
  350. struct gred_sched *table = qdisc_priv(sch);
  351. struct tc_gred_qopt *ctl;
  352. struct nlattr *tb[TCA_GRED_MAX + 1];
  353. int err, prio = GRED_DEF_PRIO;
  354. u8 *stab;
  355. u32 max_P;
  356. struct gred_sched_data *prealloc;
  357. if (opt == NULL)
  358. return -EINVAL;
  359. err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
  360. if (err < 0)
  361. return err;
  362. if (tb[TCA_GRED_PARMS] == NULL && tb[TCA_GRED_STAB] == NULL) {
  363. if (tb[TCA_GRED_LIMIT] != NULL)
  364. sch->limit = nla_get_u32(tb[TCA_GRED_LIMIT]);
  365. return gred_change_table_def(sch, tb[TCA_GRED_DPS]);
  366. }
  367. if (tb[TCA_GRED_PARMS] == NULL ||
  368. tb[TCA_GRED_STAB] == NULL ||
  369. tb[TCA_GRED_LIMIT] != NULL)
  370. return -EINVAL;
  371. max_P = tb[TCA_GRED_MAX_P] ? nla_get_u32(tb[TCA_GRED_MAX_P]) : 0;
  372. err = -EINVAL;
  373. ctl = nla_data(tb[TCA_GRED_PARMS]);
  374. stab = nla_data(tb[TCA_GRED_STAB]);
  375. if (ctl->DP >= table->DPs)
  376. goto errout;
  377. if (gred_rio_mode(table)) {
  378. if (ctl->prio == 0) {
  379. int def_prio = GRED_DEF_PRIO;
  380. if (table->tab[table->def])
  381. def_prio = table->tab[table->def]->prio;
  382. printk(KERN_DEBUG "GRED: DP %u does not have a prio "
  383. "setting default to %d\n", ctl->DP, def_prio);
  384. prio = def_prio;
  385. } else
  386. prio = ctl->prio;
  387. }
  388. prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
  389. sch_tree_lock(sch);
  390. err = gred_change_vq(sch, ctl->DP, ctl, prio, stab, max_P, &prealloc);
  391. if (err < 0)
  392. goto errout_locked;
  393. if (gred_rio_mode(table)) {
  394. gred_disable_wred_mode(table);
  395. if (gred_wred_mode_check(sch))
  396. gred_enable_wred_mode(table);
  397. }
  398. err = 0;
  399. errout_locked:
  400. sch_tree_unlock(sch);
  401. kfree(prealloc);
  402. errout:
  403. return err;
  404. }
  405. static int gred_init(struct Qdisc *sch, struct nlattr *opt)
  406. {
  407. struct nlattr *tb[TCA_GRED_MAX + 1];
  408. int err;
  409. if (opt == NULL)
  410. return -EINVAL;
  411. err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
  412. if (err < 0)
  413. return err;
  414. if (tb[TCA_GRED_PARMS] || tb[TCA_GRED_STAB])
  415. return -EINVAL;
  416. if (tb[TCA_GRED_LIMIT])
  417. sch->limit = nla_get_u32(tb[TCA_GRED_LIMIT]);
  418. else
  419. sch->limit = qdisc_dev(sch)->tx_queue_len
  420. * psched_mtu(qdisc_dev(sch));
  421. return gred_change_table_def(sch, tb[TCA_GRED_DPS]);
  422. }
  423. static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
  424. {
  425. struct gred_sched *table = qdisc_priv(sch);
  426. struct nlattr *parms, *opts = NULL;
  427. int i;
  428. u32 max_p[MAX_DPs];
  429. struct tc_gred_sopt sopt = {
  430. .DPs = table->DPs,
  431. .def_DP = table->def,
  432. .grio = gred_rio_mode(table),
  433. .flags = table->red_flags,
  434. };
  435. opts = nla_nest_start(skb, TCA_OPTIONS);
  436. if (opts == NULL)
  437. goto nla_put_failure;
  438. if (nla_put(skb, TCA_GRED_DPS, sizeof(sopt), &sopt))
  439. goto nla_put_failure;
  440. for (i = 0; i < MAX_DPs; i++) {
  441. struct gred_sched_data *q = table->tab[i];
  442. max_p[i] = q ? q->parms.max_P : 0;
  443. }
  444. if (nla_put(skb, TCA_GRED_MAX_P, sizeof(max_p), max_p))
  445. goto nla_put_failure;
  446. if (nla_put_u32(skb, TCA_GRED_LIMIT, sch->limit))
  447. goto nla_put_failure;
  448. parms = nla_nest_start(skb, TCA_GRED_PARMS);
  449. if (parms == NULL)
  450. goto nla_put_failure;
  451. for (i = 0; i < MAX_DPs; i++) {
  452. struct gred_sched_data *q = table->tab[i];
  453. struct tc_gred_qopt opt;
  454. unsigned long qavg;
  455. memset(&opt, 0, sizeof(opt));
  456. if (!q) {
  457. /* hack -- fix at some point with proper message
  458. This is how we indicate to tc that there is no VQ
  459. at this DP */
  460. opt.DP = MAX_DPs + i;
  461. goto append_opt;
  462. }
  463. opt.limit = q->limit;
  464. opt.DP = q->DP;
  465. opt.backlog = gred_backlog(table, q, sch);
  466. opt.prio = q->prio;
  467. opt.qth_min = q->parms.qth_min >> q->parms.Wlog;
  468. opt.qth_max = q->parms.qth_max >> q->parms.Wlog;
  469. opt.Wlog = q->parms.Wlog;
  470. opt.Plog = q->parms.Plog;
  471. opt.Scell_log = q->parms.Scell_log;
  472. opt.other = q->stats.other;
  473. opt.early = q->stats.prob_drop;
  474. opt.forced = q->stats.forced_drop;
  475. opt.pdrop = q->stats.pdrop;
  476. opt.packets = q->packetsin;
  477. opt.bytesin = q->bytesin;
  478. if (gred_wred_mode(table))
  479. gred_load_wred_set(table, q);
  480. qavg = red_calc_qavg(&q->parms, &q->vars,
  481. q->vars.qavg >> q->parms.Wlog);
  482. opt.qave = qavg >> q->parms.Wlog;
  483. append_opt:
  484. if (nla_append(skb, sizeof(opt), &opt) < 0)
  485. goto nla_put_failure;
  486. }
  487. nla_nest_end(skb, parms);
  488. return nla_nest_end(skb, opts);
  489. nla_put_failure:
  490. nla_nest_cancel(skb, opts);
  491. return -EMSGSIZE;
  492. }
  493. static void gred_destroy(struct Qdisc *sch)
  494. {
  495. struct gred_sched *table = qdisc_priv(sch);
  496. int i;
  497. for (i = 0; i < table->DPs; i++) {
  498. if (table->tab[i])
  499. gred_destroy_vq(table->tab[i]);
  500. }
  501. }
  502. static struct Qdisc_ops gred_qdisc_ops __read_mostly = {
  503. .id = "gred",
  504. .priv_size = sizeof(struct gred_sched),
  505. .enqueue = gred_enqueue,
  506. .dequeue = gred_dequeue,
  507. .peek = qdisc_peek_head,
  508. .drop = gred_drop,
  509. .init = gred_init,
  510. .reset = gred_reset,
  511. .destroy = gred_destroy,
  512. .change = gred_change,
  513. .dump = gred_dump,
  514. .owner = THIS_MODULE,
  515. };
  516. static int __init gred_module_init(void)
  517. {
  518. return register_qdisc(&gred_qdisc_ops);
  519. }
  520. static void __exit gred_module_exit(void)
  521. {
  522. unregister_qdisc(&gred_qdisc_ops);
  523. }
  524. module_init(gred_module_init)
  525. module_exit(gred_module_exit)
  526. MODULE_LICENSE("GPL");