sch_gred.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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/config.h>
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <net/pkt_sched.h>
  27. #include <net/red.h>
  28. #define GRED_DEF_PRIO (MAX_DPs / 2)
  29. #define GRED_VQ_MASK (MAX_DPs - 1)
  30. struct gred_sched_data;
  31. struct gred_sched;
  32. struct gred_sched_data
  33. {
  34. u32 limit; /* HARD maximal queue length */
  35. u32 DP; /* the drop pramaters */
  36. u32 bytesin; /* bytes seen on virtualQ so far*/
  37. u32 packetsin; /* packets seen on virtualQ so far*/
  38. u32 backlog; /* bytes on the virtualQ */
  39. u8 prio; /* the prio of this vq */
  40. struct red_parms parms;
  41. struct red_stats stats;
  42. };
  43. enum {
  44. GRED_WRED_MODE = 1,
  45. GRED_RIO_MODE,
  46. };
  47. struct gred_sched
  48. {
  49. struct gred_sched_data *tab[MAX_DPs];
  50. unsigned long flags;
  51. u32 DPs;
  52. u32 def;
  53. struct red_parms wred_set;
  54. };
  55. static inline int gred_wred_mode(struct gred_sched *table)
  56. {
  57. return test_bit(GRED_WRED_MODE, &table->flags);
  58. }
  59. static inline void gred_enable_wred_mode(struct gred_sched *table)
  60. {
  61. __set_bit(GRED_WRED_MODE, &table->flags);
  62. }
  63. static inline void gred_disable_wred_mode(struct gred_sched *table)
  64. {
  65. __clear_bit(GRED_WRED_MODE, &table->flags);
  66. }
  67. static inline int gred_rio_mode(struct gred_sched *table)
  68. {
  69. return test_bit(GRED_RIO_MODE, &table->flags);
  70. }
  71. static inline void gred_enable_rio_mode(struct gred_sched *table)
  72. {
  73. __set_bit(GRED_RIO_MODE, &table->flags);
  74. }
  75. static inline void gred_disable_rio_mode(struct gred_sched *table)
  76. {
  77. __clear_bit(GRED_RIO_MODE, &table->flags);
  78. }
  79. static inline int gred_wred_mode_check(struct Qdisc *sch)
  80. {
  81. struct gred_sched *table = qdisc_priv(sch);
  82. int i;
  83. /* Really ugly O(n^2) but shouldn't be necessary too frequent. */
  84. for (i = 0; i < table->DPs; i++) {
  85. struct gred_sched_data *q = table->tab[i];
  86. int n;
  87. if (q == NULL)
  88. continue;
  89. for (n = 0; n < table->DPs; n++)
  90. if (table->tab[n] && table->tab[n] != q &&
  91. table->tab[n]->prio == q->prio)
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. static inline unsigned int gred_backlog(struct gred_sched *table,
  97. struct gred_sched_data *q,
  98. struct Qdisc *sch)
  99. {
  100. if (gred_wred_mode(table))
  101. return sch->qstats.backlog;
  102. else
  103. return q->backlog;
  104. }
  105. static inline u16 tc_index_to_dp(struct sk_buff *skb)
  106. {
  107. return skb->tc_index & GRED_VQ_MASK;
  108. }
  109. static inline void gred_load_wred_set(struct gred_sched *table,
  110. struct gred_sched_data *q)
  111. {
  112. q->parms.qavg = table->wred_set.qavg;
  113. q->parms.qidlestart = table->wred_set.qidlestart;
  114. }
  115. static inline void gred_store_wred_set(struct gred_sched *table,
  116. struct gred_sched_data *q)
  117. {
  118. table->wred_set.qavg = q->parms.qavg;
  119. }
  120. static int gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  121. {
  122. struct gred_sched_data *q=NULL;
  123. struct gred_sched *t= qdisc_priv(sch);
  124. unsigned long qavg = 0;
  125. u16 dp = tc_index_to_dp(skb);
  126. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  127. dp = t->def;
  128. if ((q = t->tab[dp]) == NULL) {
  129. /* Pass through packets not assigned to a DP
  130. * if no default DP has been configured. This
  131. * allows for DP flows to be left untouched.
  132. */
  133. if (skb_queue_len(&sch->q) < sch->dev->tx_queue_len)
  134. return qdisc_enqueue_tail(skb, sch);
  135. else
  136. goto drop;
  137. }
  138. /* fix tc_index? --could be controvesial but needed for
  139. requeueing */
  140. skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
  141. }
  142. /* sum up all the qaves of prios <= to ours to get the new qave */
  143. if (!gred_wred_mode(t) && gred_rio_mode(t)) {
  144. int i;
  145. for (i = 0; i < t->DPs; i++) {
  146. if (t->tab[i] && t->tab[i]->prio < q->prio &&
  147. !red_is_idling(&t->tab[i]->parms))
  148. qavg +=t->tab[i]->parms.qavg;
  149. }
  150. }
  151. q->packetsin++;
  152. q->bytesin += skb->len;
  153. if (gred_wred_mode(t))
  154. gred_load_wred_set(t, q);
  155. q->parms.qavg = red_calc_qavg(&q->parms, gred_backlog(t, q, sch));
  156. if (red_is_idling(&q->parms))
  157. red_end_of_idle_period(&q->parms);
  158. if (gred_wred_mode(t))
  159. gred_store_wred_set(t, q);
  160. switch (red_action(&q->parms, q->parms.qavg + qavg)) {
  161. case RED_DONT_MARK:
  162. break;
  163. case RED_PROB_MARK:
  164. sch->qstats.overlimits++;
  165. q->stats.prob_drop++;
  166. goto congestion_drop;
  167. case RED_HARD_MARK:
  168. sch->qstats.overlimits++;
  169. q->stats.forced_drop++;
  170. goto congestion_drop;
  171. }
  172. if (q->backlog + skb->len <= q->limit) {
  173. q->backlog += skb->len;
  174. return qdisc_enqueue_tail(skb, sch);
  175. }
  176. q->stats.pdrop++;
  177. drop:
  178. return qdisc_drop(skb, sch);
  179. congestion_drop:
  180. qdisc_drop(skb, sch);
  181. return NET_XMIT_CN;
  182. }
  183. static int gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
  184. {
  185. struct gred_sched *t = qdisc_priv(sch);
  186. struct gred_sched_data *q;
  187. u16 dp = tc_index_to_dp(skb);
  188. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  189. if (net_ratelimit())
  190. printk(KERN_WARNING "GRED: Unable to relocate VQ 0x%x "
  191. "for requeue, screwing up backlog.\n",
  192. tc_index_to_dp(skb));
  193. } else {
  194. if (red_is_idling(&q->parms))
  195. red_end_of_idle_period(&q->parms);
  196. q->backlog += skb->len;
  197. }
  198. return qdisc_requeue(skb, sch);
  199. }
  200. static struct sk_buff *gred_dequeue(struct Qdisc* sch)
  201. {
  202. struct sk_buff *skb;
  203. struct gred_sched *t = qdisc_priv(sch);
  204. skb = qdisc_dequeue_head(sch);
  205. if (skb) {
  206. struct gred_sched_data *q;
  207. u16 dp = tc_index_to_dp(skb);
  208. if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
  209. if (net_ratelimit())
  210. printk(KERN_WARNING "GRED: Unable to relocate "
  211. "VQ 0x%x after dequeue, screwing up "
  212. "backlog.\n", tc_index_to_dp(skb));
  213. } else {
  214. q->backlog -= skb->len;
  215. if (!q->backlog && !gred_wred_mode(t))
  216. red_start_of_idle_period(&q->parms);
  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 = skb->len;
  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. if (net_ratelimit())
  235. printk(KERN_WARNING "GRED: Unable to relocate "
  236. "VQ 0x%x while dropping, screwing up "
  237. "backlog.\n", tc_index_to_dp(skb));
  238. } else {
  239. q->backlog -= len;
  240. q->stats.other++;
  241. if (!q->backlog && !gred_wred_mode(t))
  242. red_start_of_idle_period(&q->parms);
  243. }
  244. qdisc_drop(skb, sch);
  245. return len;
  246. }
  247. if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
  248. red_start_of_idle_period(&t->wred_set);
  249. return 0;
  250. }
  251. static void gred_reset(struct Qdisc* sch)
  252. {
  253. int i;
  254. struct gred_sched *t = qdisc_priv(sch);
  255. qdisc_reset_queue(sch);
  256. for (i = 0; i < t->DPs; i++) {
  257. struct gred_sched_data *q = t->tab[i];
  258. if (!q)
  259. continue;
  260. red_restart(&q->parms);
  261. q->backlog = 0;
  262. }
  263. }
  264. static inline void gred_destroy_vq(struct gred_sched_data *q)
  265. {
  266. kfree(q);
  267. }
  268. static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
  269. {
  270. struct gred_sched *table = qdisc_priv(sch);
  271. struct tc_gred_sopt *sopt;
  272. int i;
  273. if (dps == NULL || RTA_PAYLOAD(dps) < sizeof(*sopt))
  274. return -EINVAL;
  275. sopt = RTA_DATA(dps);
  276. if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
  277. return -EINVAL;
  278. sch_tree_lock(sch);
  279. table->DPs = sopt->DPs;
  280. table->def = sopt->def_DP;
  281. /*
  282. * Every entry point to GRED is synchronized with the above code
  283. * and the DP is checked against DPs, i.e. shadowed VQs can no
  284. * longer be found so we can unlock right here.
  285. */
  286. sch_tree_unlock(sch);
  287. if (sopt->grio) {
  288. gred_enable_rio_mode(table);
  289. gred_disable_wred_mode(table);
  290. if (gred_wred_mode_check(sch))
  291. gred_enable_wred_mode(table);
  292. } else {
  293. gred_disable_rio_mode(table);
  294. gred_disable_wred_mode(table);
  295. }
  296. for (i = table->DPs; i < MAX_DPs; i++) {
  297. if (table->tab[i]) {
  298. printk(KERN_WARNING "GRED: Warning: Destroying "
  299. "shadowed VQ 0x%x\n", i);
  300. gred_destroy_vq(table->tab[i]);
  301. table->tab[i] = NULL;
  302. }
  303. }
  304. return 0;
  305. }
  306. static inline int gred_change_vq(struct Qdisc *sch, int dp,
  307. struct tc_gred_qopt *ctl, int prio, u8 *stab)
  308. {
  309. struct gred_sched *table = qdisc_priv(sch);
  310. struct gred_sched_data *q;
  311. if (table->tab[dp] == NULL) {
  312. table->tab[dp] = kmalloc(sizeof(*q), GFP_KERNEL);
  313. if (table->tab[dp] == NULL)
  314. return -ENOMEM;
  315. memset(table->tab[dp], 0, sizeof(*q));
  316. }
  317. q = table->tab[dp];
  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->parms);
  323. red_set_parms(&q->parms,
  324. ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
  325. ctl->Scell_log, stab);
  326. return 0;
  327. }
  328. static int gred_change(struct Qdisc *sch, struct rtattr *opt)
  329. {
  330. struct gred_sched *table = qdisc_priv(sch);
  331. struct tc_gred_qopt *ctl;
  332. struct rtattr *tb[TCA_GRED_MAX];
  333. int err = -EINVAL, prio = GRED_DEF_PRIO;
  334. u8 *stab;
  335. if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
  336. return -EINVAL;
  337. if (tb[TCA_GRED_PARMS-1] == NULL && tb[TCA_GRED_STAB-1] == NULL)
  338. return gred_change_table_def(sch, opt);
  339. if (tb[TCA_GRED_PARMS-1] == NULL ||
  340. RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
  341. tb[TCA_GRED_STAB-1] == NULL ||
  342. RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
  343. return -EINVAL;
  344. ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
  345. stab = RTA_DATA(tb[TCA_GRED_STAB-1]);
  346. if (ctl->DP >= table->DPs)
  347. goto errout;
  348. if (gred_rio_mode(table)) {
  349. if (ctl->prio == 0) {
  350. int def_prio = GRED_DEF_PRIO;
  351. if (table->tab[table->def])
  352. def_prio = table->tab[table->def]->prio;
  353. printk(KERN_DEBUG "GRED: DP %u does not have a prio "
  354. "setting default to %d\n", ctl->DP, def_prio);
  355. prio = def_prio;
  356. } else
  357. prio = ctl->prio;
  358. }
  359. sch_tree_lock(sch);
  360. err = gred_change_vq(sch, ctl->DP, ctl, prio, stab);
  361. if (err < 0)
  362. goto errout_locked;
  363. if (gred_rio_mode(table)) {
  364. gred_disable_wred_mode(table);
  365. if (gred_wred_mode_check(sch))
  366. gred_enable_wred_mode(table);
  367. }
  368. err = 0;
  369. errout_locked:
  370. sch_tree_unlock(sch);
  371. errout:
  372. return err;
  373. }
  374. static int gred_init(struct Qdisc *sch, struct rtattr *opt)
  375. {
  376. struct rtattr *tb[TCA_GRED_MAX];
  377. if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
  378. return -EINVAL;
  379. if (tb[TCA_GRED_PARMS-1] || tb[TCA_GRED_STAB-1])
  380. return -EINVAL;
  381. return gred_change_table_def(sch, tb[TCA_GRED_DPS-1]);
  382. }
  383. static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
  384. {
  385. struct gred_sched *table = qdisc_priv(sch);
  386. struct rtattr *parms, *opts = NULL;
  387. int i;
  388. struct tc_gred_sopt sopt = {
  389. .DPs = table->DPs,
  390. .def_DP = table->def,
  391. .grio = gred_rio_mode(table),
  392. };
  393. opts = RTA_NEST(skb, TCA_OPTIONS);
  394. RTA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
  395. parms = RTA_NEST(skb, TCA_GRED_PARMS);
  396. for (i = 0; i < MAX_DPs; i++) {
  397. struct gred_sched_data *q = table->tab[i];
  398. struct tc_gred_qopt opt;
  399. memset(&opt, 0, sizeof(opt));
  400. if (!q) {
  401. /* hack -- fix at some point with proper message
  402. This is how we indicate to tc that there is no VQ
  403. at this DP */
  404. opt.DP = MAX_DPs + i;
  405. goto append_opt;
  406. }
  407. opt.limit = q->limit;
  408. opt.DP = q->DP;
  409. opt.backlog = q->backlog;
  410. opt.prio = q->prio;
  411. opt.qth_min = q->parms.qth_min >> q->parms.Wlog;
  412. opt.qth_max = q->parms.qth_max >> q->parms.Wlog;
  413. opt.Wlog = q->parms.Wlog;
  414. opt.Plog = q->parms.Plog;
  415. opt.Scell_log = q->parms.Scell_log;
  416. opt.other = q->stats.other;
  417. opt.early = q->stats.prob_drop;
  418. opt.forced = q->stats.forced_drop;
  419. opt.pdrop = q->stats.pdrop;
  420. opt.packets = q->packetsin;
  421. opt.bytesin = q->bytesin;
  422. if (gred_wred_mode(table)) {
  423. q->parms.qidlestart =
  424. table->tab[table->def]->parms.qidlestart;
  425. q->parms.qavg = table->tab[table->def]->parms.qavg;
  426. }
  427. opt.qave = red_calc_qavg(&q->parms, q->parms.qavg);
  428. append_opt:
  429. RTA_APPEND(skb, sizeof(opt), &opt);
  430. }
  431. RTA_NEST_END(skb, parms);
  432. return RTA_NEST_END(skb, opts);
  433. rtattr_failure:
  434. return RTA_NEST_CANCEL(skb, opts);
  435. }
  436. static void gred_destroy(struct Qdisc *sch)
  437. {
  438. struct gred_sched *table = qdisc_priv(sch);
  439. int i;
  440. for (i = 0; i < table->DPs; i++) {
  441. if (table->tab[i])
  442. gred_destroy_vq(table->tab[i]);
  443. }
  444. }
  445. static struct Qdisc_ops gred_qdisc_ops = {
  446. .id = "gred",
  447. .priv_size = sizeof(struct gred_sched),
  448. .enqueue = gred_enqueue,
  449. .dequeue = gred_dequeue,
  450. .requeue = gred_requeue,
  451. .drop = gred_drop,
  452. .init = gred_init,
  453. .reset = gred_reset,
  454. .destroy = gred_destroy,
  455. .change = gred_change,
  456. .dump = gred_dump,
  457. .owner = THIS_MODULE,
  458. };
  459. static int __init gred_module_init(void)
  460. {
  461. return register_qdisc(&gred_qdisc_ops);
  462. }
  463. static void __exit gred_module_exit(void)
  464. {
  465. unregister_qdisc(&gred_qdisc_ops);
  466. }
  467. module_init(gred_module_init)
  468. module_exit(gred_module_exit)
  469. MODULE_LICENSE("GPL");