sch_gred.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <asm/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. #if 1 /* control */
  48. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  49. #else
  50. #define DPRINTK(format,args...)
  51. #endif
  52. #if 0 /* data */
  53. #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
  54. #else
  55. #define D2PRINTK(format,args...)
  56. #endif
  57. struct gred_sched_data;
  58. struct gred_sched;
  59. struct gred_sched_data
  60. {
  61. /* Parameters */
  62. u32 limit; /* HARD maximal queue length */
  63. u32 qth_min; /* Min average length threshold: A scaled */
  64. u32 qth_max; /* Max average length threshold: A scaled */
  65. u32       DP; /* the drop pramaters */
  66. char Wlog; /* log(W) */
  67. char Plog; /* random number bits */
  68. u32 Scell_max;
  69. u32 Rmask;
  70. u32 bytesin; /* bytes seen on virtualQ so far*/
  71. u32 packetsin; /* packets seen on virtualQ so far*/
  72. u32 backlog; /* bytes on the virtualQ */
  73. u32 forced; /* packets dropped for exceeding limits */
  74. u32 early; /* packets dropped as a warning */
  75. u32 other; /* packets dropped by invoking drop() */
  76. u32 pdrop; /* packets dropped because we exceeded physical queue limits */
  77. char Scell_log;
  78. u8 Stab[256];
  79. u8              prio;        /* the prio of this vq */
  80. /* Variables */
  81. unsigned long qave; /* Average queue length: A scaled */
  82. int qcount; /* Packets since last random number generation */
  83. u32 qR; /* Cached random number */
  84. psched_time_t qidlestart; /* Start of idle period */
  85. };
  86. struct gred_sched
  87. {
  88. struct gred_sched_data *tab[MAX_DPs];
  89. u32  DPs;   
  90. u32  def; 
  91. u8  initd; 
  92. u8  grio; 
  93. u8  eqp; 
  94. };
  95. static int
  96. gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  97. {
  98. psched_time_t now;
  99. struct gred_sched_data *q=NULL;
  100. struct gred_sched *t= (struct gred_sched *)sch->data;
  101. unsigned long qave=0;
  102. int i=0;
  103. if (!t->initd && skb_queue_len(&sch->q) <= sch->dev->tx_queue_len) {
  104. D2PRINTK("NO GRED Queues setup yet! Enqueued anywayn");
  105. goto do_enqueue;
  106. }
  107. if ( ((skb->tc_index&0xf) > t->DPs) || !(q=t->tab[skb->tc_index&0xf])) {
  108. printk("GRED: setting to default (%d)n ",t->def);
  109. if (!(q=t->tab[t->def])) {
  110. DPRINTK("GRED: setting to default FAILED! dropping!! "
  111.     "(%d)n ", t->def);
  112. goto drop;
  113. }
  114. /* fix tc_index? --could be controvesial but needed for
  115.    requeueing */
  116. skb->tc_index=(skb->tc_index&0xfffffff0) | t->def;
  117. }
  118. D2PRINTK("gred_enqueue virtualQ 0x%x classid %x backlog %d "
  119.     "general backlog %dn",skb->tc_index&0xf,sch->handle,q->backlog,
  120.     sch->stats.backlog);
  121. /* sum up all the qaves of prios <= to ours to get the new qave*/
  122. if (!t->eqp && t->grio) {
  123. for (i=0;i<t->DPs;i++) {
  124. if ((!t->tab[i]) || (i==q->DP))
  125. continue; 
  126. if ((t->tab[i]->prio < q->prio) && (PSCHED_IS_PASTPERFECT(t->tab[i]->qidlestart)))
  127. qave +=t->tab[i]->qave;
  128. }
  129. }
  130. q->packetsin++;
  131. q->bytesin+=skb->len;
  132. if (t->eqp && t->grio) {
  133. qave=0;
  134. q->qave=t->tab[t->def]->qave;
  135. q->qidlestart=t->tab[t->def]->qidlestart;
  136. }
  137. if (!PSCHED_IS_PASTPERFECT(q->qidlestart)) {
  138. long us_idle;
  139. PSCHED_GET_TIME(now);
  140. us_idle = PSCHED_TDIFF_SAFE(now, q->qidlestart, q->Scell_max, 0);
  141. PSCHED_SET_PASTPERFECT(q->qidlestart);
  142. q->qave >>= q->Stab[(us_idle>>q->Scell_log)&0xFF];
  143. } else {
  144. if (t->eqp) {
  145. q->qave += sch->stats.backlog - (q->qave >> q->Wlog);
  146. } else {
  147. q->qave += q->backlog - (q->qave >> q->Wlog);
  148. }
  149. }
  150. if (t->eqp && t->grio) 
  151. t->tab[t->def]->qave=q->qave;
  152. if ((q->qave+qave) < q->qth_min) {
  153. q->qcount = -1;
  154. enqueue:
  155. if (q->backlog <= q->limit) {
  156. q->backlog += skb->len;
  157. do_enqueue:
  158. __skb_queue_tail(&sch->q, skb);
  159. sch->stats.backlog += skb->len;
  160. sch->stats.bytes += skb->len;
  161. sch->stats.packets++;
  162. return 0;
  163. } else {
  164. q->pdrop++;
  165. }
  166. drop:
  167. kfree_skb(skb);
  168. sch->stats.drops++;
  169. return NET_XMIT_DROP;
  170. }
  171. if ((q->qave+qave) >= q->qth_max) {
  172. q->qcount = -1;
  173. sch->stats.overlimits++;
  174. q->forced++;
  175. goto drop;
  176. }
  177. if (++q->qcount) {
  178. if ((((qave+q->qave) - q->qth_min)>>q->Wlog)*q->qcount < q->qR)
  179. goto enqueue;
  180. q->qcount = 0;
  181. q->qR = net_random()&q->Rmask;
  182. sch->stats.overlimits++;
  183. q->early++;
  184. goto drop;
  185. }
  186. q->qR = net_random()&q->Rmask;
  187. goto enqueue;
  188. }
  189. static int
  190. gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
  191. {
  192. struct gred_sched_data *q;
  193. struct gred_sched *t= (struct gred_sched *)sch->data;
  194. q= t->tab[(skb->tc_index&0xf)];
  195. /* error checking here -- probably unnecessary */
  196. PSCHED_SET_PASTPERFECT(q->qidlestart);
  197. __skb_queue_head(&sch->q, skb);
  198. sch->stats.backlog += skb->len;
  199. q->backlog += skb->len;
  200. return 0;
  201. }
  202. static struct sk_buff *
  203. gred_dequeue(struct Qdisc* sch)
  204. {
  205. struct sk_buff *skb;
  206. struct gred_sched_data *q;
  207. struct gred_sched *t= (struct gred_sched *)sch->data;
  208. skb = __skb_dequeue(&sch->q);
  209. if (skb) {
  210. sch->stats.backlog -= skb->len;
  211. q= t->tab[(skb->tc_index&0xf)];
  212. if (q) {
  213. q->backlog -= skb->len;
  214. if (!q->backlog && !t->eqp)
  215. PSCHED_GET_TIME(q->qidlestart);
  216. } else {
  217. D2PRINTK("gred_dequeue: skb has bad tcindex %xn",skb->tc_index&0xf); 
  218. }
  219. return skb;
  220. }
  221. if (t->eqp) {
  222. q= t->tab[t->def];
  223. if (!q)
  224. D2PRINTK("no default VQ set: Results will be "
  225.        "screwed upn");
  226. else
  227. PSCHED_GET_TIME(q->qidlestart);
  228. }
  229. return NULL;
  230. }
  231. static int
  232. gred_drop(struct Qdisc* sch)
  233. {
  234. struct sk_buff *skb;
  235. struct gred_sched_data *q;
  236. struct gred_sched *t= (struct gred_sched *)sch->data;
  237. skb = __skb_dequeue_tail(&sch->q);
  238. if (skb) {
  239. sch->stats.backlog -= skb->len;
  240. sch->stats.drops++;
  241. q= t->tab[(skb->tc_index&0xf)];
  242. if (q) {
  243. q->backlog -= skb->len;
  244. q->other++;
  245. if (!q->backlog && !t->eqp)
  246. PSCHED_GET_TIME(q->qidlestart);
  247. } else {
  248. D2PRINTK("gred_dequeue: skb has bad tcindex %xn",skb->tc_index&0xf); 
  249. }
  250. kfree_skb(skb);
  251. return 1;
  252. }
  253. q=t->tab[t->def];
  254. if (!q) {
  255. D2PRINTK("no default VQ set: Results might be screwed upn");
  256. return 0;
  257. }
  258. PSCHED_GET_TIME(q->qidlestart);
  259. return 0;
  260. }
  261. static void gred_reset(struct Qdisc* sch)
  262. {
  263. int i;
  264. struct gred_sched_data *q;
  265. struct gred_sched *t= (struct gred_sched *)sch->data;
  266. __skb_queue_purge(&sch->q);
  267. sch->stats.backlog = 0;
  268.         for (i=0;i<t->DPs;i++) {
  269.         q= t->tab[i];
  270. if (!q)
  271. continue; 
  272. PSCHED_SET_PASTPERFECT(q->qidlestart);
  273. q->qave = 0;
  274. q->qcount = -1;
  275. q->backlog = 0;
  276. q->other=0;
  277. q->forced=0;
  278. q->pdrop=0;
  279. q->early=0;
  280. }
  281. }
  282. static int gred_change(struct Qdisc *sch, struct rtattr *opt)
  283. {
  284. struct gred_sched *table = (struct gred_sched *)sch->data;
  285. struct gred_sched_data *q;
  286. struct tc_gred_qopt *ctl;
  287. struct tc_gred_sopt *sopt;
  288. struct rtattr *tb[TCA_GRED_STAB];
  289. struct rtattr *tb2[TCA_GRED_STAB];
  290. int i;
  291. if (opt == NULL ||
  292. rtattr_parse(tb, TCA_GRED_STAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) )
  293. return -EINVAL;
  294. if (tb[TCA_GRED_PARMS-1] == 0 && tb[TCA_GRED_STAB-1] == 0 &&
  295.     tb[TCA_GRED_DPS-1] != 0) {
  296. rtattr_parse(tb2, TCA_GRED_DPS, RTA_DATA(opt),
  297.     RTA_PAYLOAD(opt));
  298. sopt = RTA_DATA(tb2[TCA_GRED_DPS-1]);
  299. table->DPs=sopt->DPs;   
  300. table->def=sopt->def_DP; 
  301. table->grio=sopt->grio; 
  302. table->initd=0;
  303. /* probably need to clear all the table DP entries as well */
  304. MOD_INC_USE_COUNT;
  305. return 0;
  306.     }
  307. if (!table->DPs || tb[TCA_GRED_PARMS-1] == 0 || tb[TCA_GRED_STAB-1] == 0 ||
  308. RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
  309. RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
  310. return -EINVAL;
  311. ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
  312. if (ctl->DP > MAX_DPs-1 ) {
  313. /* misbehaving is punished! Put in the default drop probability */
  314. DPRINTK("nGRED: DP %u not in  the proper range fixed. New DP "
  315. "set to default at %dn",ctl->DP,table->def);
  316. ctl->DP=table->def;
  317. }
  318. if (table->tab[ctl->DP] == NULL) {
  319. table->tab[ctl->DP]=kmalloc(sizeof(struct gred_sched_data),
  320.     GFP_KERNEL);
  321. if (NULL == table->tab[ctl->DP])
  322. return -ENOMEM;
  323. memset(table->tab[ctl->DP], 0, (sizeof(struct gred_sched_data)));
  324. }
  325. q= table->tab[ctl->DP]; 
  326. if (table->grio) {
  327. if (ctl->prio <=0) {
  328. if (table->def && table->tab[table->def]) {
  329. DPRINTK("nGRED: DP %u does not have a prio"
  330. "setting default to %dn",ctl->DP,
  331. table->tab[table->def]->prio);
  332. q->prio=table->tab[table->def]->prio;
  333. } else { 
  334. DPRINTK("nGRED: DP %u does not have a prio"
  335. " setting default to 8n",ctl->DP);
  336. q->prio=8;
  337. }
  338. } else {
  339. q->prio=ctl->prio;
  340. }
  341. } else {
  342. q->prio=8;
  343. }
  344. q->DP=ctl->DP;
  345. q->Wlog = ctl->Wlog;
  346. q->Plog = ctl->Plog;
  347. q->limit = ctl->limit;
  348. q->Scell_log = ctl->Scell_log;
  349. q->Rmask = ctl->Plog < 32 ? ((1<<ctl->Plog) - 1) : ~0UL;
  350. q->Scell_max = (255<<q->Scell_log);
  351. q->qth_min = ctl->qth_min<<ctl->Wlog;
  352. q->qth_max = ctl->qth_max<<ctl->Wlog;
  353. q->qave=0;
  354. q->backlog=0;
  355. q->qcount = -1;
  356. q->other=0;
  357. q->forced=0;
  358. q->pdrop=0;
  359. q->early=0;
  360. PSCHED_SET_PASTPERFECT(q->qidlestart);
  361. memcpy(q->Stab, RTA_DATA(tb[TCA_GRED_STAB-1]), 256);
  362. if ( table->initd && table->grio) {
  363. /* this looks ugly but its not in the fast path */
  364. for (i=0;i<table->DPs;i++) {
  365. if ((!table->tab[i]) || (i==q->DP) )    
  366. continue; 
  367. if (table->tab[i]->prio == q->prio ){
  368. /* WRED mode detected */
  369. table->eqp=1;
  370. break;
  371. }
  372. }
  373. }
  374. if (!table->initd) {
  375. table->initd=1;
  376. /* 
  377.          the first entry also goes into the default until
  378.          over-written 
  379. */
  380. if (table->tab[table->def] == NULL) {
  381. table->tab[table->def]=
  382. kmalloc(sizeof(struct gred_sched_data), GFP_KERNEL);
  383. if (NULL == table->tab[table->def])
  384. return -ENOMEM;
  385. memset(table->tab[table->def], 0,
  386.        (sizeof(struct gred_sched_data)));
  387. }
  388. q= table->tab[table->def]; 
  389. q->DP=table->def;
  390. q->Wlog = ctl->Wlog;
  391. q->Plog = ctl->Plog;
  392. q->limit = ctl->limit;
  393. q->Scell_log = ctl->Scell_log;
  394. q->Rmask = ctl->Plog < 32 ? ((1<<ctl->Plog) - 1) : ~0UL;
  395. q->Scell_max = (255<<q->Scell_log);
  396. q->qth_min = ctl->qth_min<<ctl->Wlog;
  397. q->qth_max = ctl->qth_max<<ctl->Wlog;
  398. if (table->grio)
  399. q->prio=table->tab[ctl->DP]->prio;
  400. else
  401. q->prio=8;
  402. q->qcount = -1;
  403. PSCHED_SET_PASTPERFECT(q->qidlestart);
  404. memcpy(q->Stab, RTA_DATA(tb[TCA_GRED_STAB-1]), 256);
  405. }
  406. return 0;
  407. }
  408. static int gred_init(struct Qdisc *sch, struct rtattr *opt)
  409. {
  410. struct gred_sched *table = (struct gred_sched *)sch->data;
  411. struct tc_gred_sopt *sopt;
  412. struct rtattr *tb[TCA_GRED_STAB];
  413. struct rtattr *tb2[TCA_GRED_STAB];
  414. if (opt == NULL ||
  415. rtattr_parse(tb, TCA_GRED_STAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) )
  416. return -EINVAL;
  417. if (tb[TCA_GRED_PARMS-1] == 0 && tb[TCA_GRED_STAB-1] == 0 &&
  418.     tb[TCA_GRED_DPS-1] != 0) {
  419. rtattr_parse(tb2, TCA_GRED_DPS, RTA_DATA(opt),RTA_PAYLOAD(opt));
  420. sopt = RTA_DATA(tb2[TCA_GRED_DPS-1]);
  421. table->DPs=sopt->DPs;   
  422. table->def=sopt->def_DP; 
  423. table->grio=sopt->grio; 
  424. table->initd=0;
  425. MOD_INC_USE_COUNT;
  426. return 0;
  427. }
  428. DPRINTK("n GRED_INIT error!n");
  429. return -EINVAL;
  430. }
  431. static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
  432. {
  433. unsigned long qave;
  434. struct rtattr *rta;
  435. struct tc_gred_qopt *opt = NULL ;
  436. struct tc_gred_qopt *dst;
  437. struct gred_sched *table = (struct gred_sched *)sch->data;
  438. struct gred_sched_data *q;
  439. int i;
  440. unsigned char  *b = skb->tail;
  441. rta = (struct rtattr*)b;
  442. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  443. opt=kmalloc(sizeof(struct tc_gred_qopt)*MAX_DPs, GFP_KERNEL);
  444. if (opt  == NULL) {
  445. DPRINTK("gred_dump:failed to malloc for %Zdn",
  446.     sizeof(struct tc_gred_qopt)*MAX_DPs);
  447. goto rtattr_failure;
  448. }
  449. memset(opt, 0, (sizeof(struct tc_gred_qopt))*table->DPs);
  450. if (!table->initd) {
  451. DPRINTK("NO GRED Queues setup!n");
  452. }
  453. for (i=0;i<MAX_DPs;i++) {
  454. dst= &opt[i]; 
  455. q= table->tab[i]; 
  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. dst->DP=MAX_DPs+i;
  461. continue;
  462. }
  463. dst->limit=q->limit;
  464. dst->qth_min=q->qth_min>>q->Wlog;
  465. dst->qth_max=q->qth_max>>q->Wlog;
  466. dst->DP=q->DP;
  467. dst->backlog=q->backlog;
  468. if (q->qave) {
  469. if (table->eqp && table->grio) {
  470. q->qidlestart=table->tab[table->def]->qidlestart;
  471. q->qave=table->tab[table->def]->qave;
  472. }
  473. if (!PSCHED_IS_PASTPERFECT(q->qidlestart)) {
  474. long idle;
  475. psched_time_t now;
  476. PSCHED_GET_TIME(now);
  477. idle = PSCHED_TDIFF_SAFE(now, q->qidlestart, q->Scell_max, 0);
  478. qave  = q->qave >> q->Stab[(idle>>q->Scell_log)&0xFF];
  479. dst->qave = qave >> q->Wlog;
  480. } else {
  481. dst->qave = q->qave >> q->Wlog;
  482. }
  483. } else {
  484. dst->qave = 0;
  485. }
  486. dst->Wlog = q->Wlog;
  487. dst->Plog = q->Plog;
  488. dst->Scell_log = q->Scell_log;
  489. dst->other = q->other;
  490. dst->forced = q->forced;
  491. dst->early = q->early;
  492. dst->pdrop = q->pdrop;
  493. dst->prio = q->prio;
  494. dst->packets=q->packetsin;
  495. dst->bytesin=q->bytesin;
  496. }
  497. RTA_PUT(skb, TCA_GRED_PARMS, sizeof(struct tc_gred_qopt)*MAX_DPs, opt);
  498. rta->rta_len = skb->tail - b;
  499. kfree(opt);
  500. return skb->len;
  501. rtattr_failure:
  502. if (opt)
  503. kfree(opt);
  504. DPRINTK("gred_dump: FAILURE!!!!n");
  505. /* also free the opt struct here */
  506. skb_trim(skb, b - skb->data);
  507. return -1;
  508. }
  509. static void gred_destroy(struct Qdisc *sch)
  510. {
  511. struct gred_sched *table = (struct gred_sched *)sch->data;
  512. int i;
  513. for (i = 0;i < table->DPs; i++) {
  514. if (table->tab[i])
  515. kfree(table->tab[i]);
  516. }
  517. MOD_DEC_USE_COUNT;
  518. }
  519. struct Qdisc_ops gred_qdisc_ops =
  520. {
  521. NULL,
  522. NULL,
  523. "gred",
  524. sizeof(struct gred_sched),
  525. gred_enqueue,
  526. gred_dequeue,
  527. gred_requeue,
  528. gred_drop,
  529. gred_init,
  530. gred_reset,
  531. gred_destroy,
  532. gred_change, /* change */
  533. gred_dump,
  534. };
  535. #ifdef MODULE
  536. int init_module(void)
  537. {
  538. return register_qdisc(&gred_qdisc_ops);
  539. }
  540. void cleanup_module(void) 
  541. {
  542. unregister_qdisc(&gred_qdisc_ops);
  543. }
  544. #endif
  545. MODULE_LICENSE("GPL");