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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * net/sched/sch_prio.c Simple 3-band priority "scheduler".
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version
  7.  * 2 of the License, or (at your option) any later version.
  8.  *
  9.  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10.  * Fixes:       19990609: J Hadi Salim <hadi@nortelnetworks.com>: 
  11.  *              Init --  EINVAL when opt undefined
  12.  */
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <asm/bitops.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/string.h>
  22. #include <linux/mm.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/in.h>
  26. #include <linux/errno.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/inet.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/notifier.h>
  33. #include <net/ip.h>
  34. #include <net/route.h>
  35. #include <linux/skbuff.h>
  36. #include <net/sock.h>
  37. #include <net/pkt_sched.h>
  38. struct prio_sched_data
  39. {
  40. int bands;
  41. struct tcf_proto *filter_list;
  42. u8  prio2band[TC_PRIO_MAX+1];
  43. struct Qdisc *queues[TCQ_PRIO_BANDS];
  44. };
  45. static __inline__ unsigned prio_classify(struct sk_buff *skb, struct Qdisc *sch)
  46. {
  47. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  48. struct tcf_result res;
  49. u32 band;
  50. band = skb->priority;
  51. if (TC_H_MAJ(skb->priority) != sch->handle) {
  52. if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
  53. if (TC_H_MAJ(band))
  54. band = 0;
  55. return q->prio2band[band&TC_PRIO_MAX];
  56. }
  57. band = res.classid;
  58. }
  59. band = TC_H_MIN(band) - 1;
  60. return band < q->bands ? band : q->prio2band[0];
  61. }
  62. static int
  63. prio_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  64. {
  65. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  66. struct Qdisc *qdisc;
  67. int ret;
  68. qdisc = q->queues[prio_classify(skb, sch)];
  69. if ((ret = qdisc->enqueue(skb, qdisc)) == 0) {
  70. sch->stats.bytes += skb->len;
  71. sch->stats.packets++;
  72. sch->q.qlen++;
  73. return 0;
  74. }
  75. sch->stats.drops++;
  76. return ret;
  77. }
  78. static int
  79. prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
  80. {
  81. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  82. struct Qdisc *qdisc;
  83. int ret;
  84. qdisc = q->queues[prio_classify(skb, sch)];
  85. if ((ret = qdisc->ops->requeue(skb, qdisc)) == 0) {
  86. sch->q.qlen++;
  87. return 0;
  88. }
  89. sch->stats.drops++;
  90. return ret;
  91. }
  92. static struct sk_buff *
  93. prio_dequeue(struct Qdisc* sch)
  94. {
  95. struct sk_buff *skb;
  96. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  97. int prio;
  98. struct Qdisc *qdisc;
  99. for (prio = 0; prio < q->bands; prio++) {
  100. qdisc = q->queues[prio];
  101. skb = qdisc->dequeue(qdisc);
  102. if (skb) {
  103. sch->q.qlen--;
  104. return skb;
  105. }
  106. }
  107. return NULL;
  108. }
  109. static int
  110. prio_drop(struct Qdisc* sch)
  111. {
  112. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  113. int prio;
  114. struct Qdisc *qdisc;
  115. for (prio = q->bands-1; prio >= 0; prio--) {
  116. qdisc = q->queues[prio];
  117. if (qdisc->ops->drop(qdisc)) {
  118. sch->q.qlen--;
  119. return 1;
  120. }
  121. }
  122. return 0;
  123. }
  124. static void
  125. prio_reset(struct Qdisc* sch)
  126. {
  127. int prio;
  128. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  129. for (prio=0; prio<q->bands; prio++)
  130. qdisc_reset(q->queues[prio]);
  131. sch->q.qlen = 0;
  132. }
  133. static void
  134. prio_destroy(struct Qdisc* sch)
  135. {
  136. int prio;
  137. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  138. for (prio=0; prio<q->bands; prio++) {
  139. qdisc_destroy(q->queues[prio]);
  140. q->queues[prio] = &noop_qdisc;
  141. }
  142. MOD_DEC_USE_COUNT;
  143. }
  144. static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
  145. {
  146. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  147. struct tc_prio_qopt *qopt = RTA_DATA(opt);
  148. int i;
  149. if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
  150. return -EINVAL;
  151. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
  152. return -EINVAL;
  153. for (i=0; i<=TC_PRIO_MAX; i++) {
  154. if (qopt->priomap[i] >= qopt->bands)
  155. return -EINVAL;
  156. }
  157. sch_tree_lock(sch);
  158. q->bands = qopt->bands;
  159. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  160. for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
  161. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  162. if (child != &noop_qdisc)
  163. qdisc_destroy(child);
  164. }
  165. sch_tree_unlock(sch);
  166. for (i=0; i<=TC_PRIO_MAX; i++) {
  167. int band = q->prio2band[i];
  168. if (q->queues[band] == &noop_qdisc) {
  169. struct Qdisc *child;
  170. child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
  171. if (child) {
  172. sch_tree_lock(sch);
  173. child = xchg(&q->queues[band], child);
  174. if (child != &noop_qdisc)
  175. qdisc_destroy(child);
  176. sch_tree_unlock(sch);
  177. }
  178. }
  179. }
  180. return 0;
  181. }
  182. static int prio_init(struct Qdisc *sch, struct rtattr *opt)
  183. {
  184. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  185. int i;
  186. for (i=0; i<TCQ_PRIO_BANDS; i++)
  187. q->queues[i] = &noop_qdisc;
  188. if (opt == NULL) {
  189. return -EINVAL;
  190. } else {
  191. int err;
  192. if ((err= prio_tune(sch, opt)) != 0)
  193. return err;
  194. }
  195. MOD_INC_USE_COUNT;
  196. return 0;
  197. }
  198. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  199. {
  200. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  201. unsigned char  *b = skb->tail;
  202. struct tc_prio_qopt opt;
  203. opt.bands = q->bands;
  204. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  205. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  206. return skb->len;
  207. rtattr_failure:
  208. skb_trim(skb, b - skb->data);
  209. return -1;
  210. }
  211. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  212.       struct Qdisc **old)
  213. {
  214. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  215. unsigned long band = arg - 1;
  216. if (band >= q->bands)
  217. return -EINVAL;
  218. if (new == NULL)
  219. new = &noop_qdisc;
  220. sch_tree_lock(sch);
  221. *old = q->queues[band];
  222. q->queues[band] = new;
  223. qdisc_reset(*old);
  224. sch_tree_unlock(sch);
  225. return 0;
  226. }
  227. static struct Qdisc *
  228. prio_leaf(struct Qdisc *sch, unsigned long arg)
  229. {
  230. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  231. unsigned long band = arg - 1;
  232. if (band >= q->bands)
  233. return NULL;
  234. return q->queues[band];
  235. }
  236. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  237. {
  238. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  239. unsigned long band = TC_H_MIN(classid);
  240. if (band - 1 >= q->bands)
  241. return 0;
  242. return band;
  243. }
  244. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  245. {
  246. return prio_get(sch, classid);
  247. }
  248. static void prio_put(struct Qdisc *q, unsigned long cl)
  249. {
  250. return;
  251. }
  252. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
  253. {
  254. unsigned long cl = *arg;
  255. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  256. if (cl - 1 > q->bands)
  257. return -ENOENT;
  258. return 0;
  259. }
  260. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  261. {
  262. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  263. if (cl - 1 > q->bands)
  264. return -ENOENT;
  265. return 0;
  266. }
  267. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  268.    struct tcmsg *tcm)
  269. {
  270. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  271. if (cl - 1 > q->bands)
  272. return -ENOENT;
  273. tcm->tcm_handle |= TC_H_MIN(cl);
  274. if (q->queues[cl-1])
  275. tcm->tcm_info = q->queues[cl-1]->handle;
  276. return 0;
  277. }
  278. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  279. {
  280. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  281. int prio;
  282. if (arg->stop)
  283. return;
  284. for (prio = 0; prio < q->bands; prio++) {
  285. if (arg->count < arg->skip) {
  286. arg->count++;
  287. continue;
  288. }
  289. if (arg->fn(sch, prio+1, arg) < 0) {
  290. arg->stop = 1;
  291. break;
  292. }
  293. arg->count++;
  294. }
  295. }
  296. static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  297. {
  298. struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
  299. if (cl)
  300. return NULL;
  301. return &q->filter_list;
  302. }
  303. static struct Qdisc_class_ops prio_class_ops =
  304. {
  305. prio_graft,
  306. prio_leaf,
  307. prio_get,
  308. prio_put,
  309. prio_change,
  310. prio_delete,
  311. prio_walk,
  312. prio_find_tcf,
  313. prio_bind,
  314. prio_put,
  315. prio_dump_class,
  316. };
  317. struct Qdisc_ops prio_qdisc_ops =
  318. {
  319. NULL,
  320. &prio_class_ops,
  321. "prio",
  322. sizeof(struct prio_sched_data),
  323. prio_enqueue,
  324. prio_dequeue,
  325. prio_requeue,
  326. prio_drop,
  327. prio_init,
  328. prio_reset,
  329. prio_destroy,
  330. prio_tune,
  331. prio_dump,
  332. };
  333. #ifdef MODULE
  334. int init_module(void)
  335. {
  336. return register_qdisc(&prio_qdisc_ops);
  337. }
  338. void cleanup_module(void) 
  339. {
  340. unregister_qdisc(&prio_qdisc_ops);
  341. }
  342. #endif
  343. MODULE_LICENSE("GPL");