sch_gred.c 14 KB

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