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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* net/sched/sch_dsmark.c - Differentiated Services field marker */
  2. /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
  3. #include <linux/config.h>
  4. #include <linux/module.h>
  5. #include <linux/types.h>
  6. #include <linux/string.h>
  7. #include <linux/errno.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/netdevice.h> /* for pkt_sched */
  10. #include <linux/rtnetlink.h>
  11. #include <net/pkt_sched.h>
  12. #include <net/dsfield.h>
  13. #include <asm/byteorder.h>
  14. #if 1 /* control */
  15. #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
  16. #else
  17. #define DPRINTK(format,args...)
  18. #endif
  19. #if 0 /* data */
  20. #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
  21. #else
  22. #define D2PRINTK(format,args...)
  23. #endif
  24. #define PRIV(sch) ((struct dsmark_qdisc_data *) (sch)->data)
  25. /*
  26.  * classid class marking
  27.  * ------- ----- -------
  28.  *   n/a   0 n/a
  29.  *   x:0   1 use entry [0]
  30.  *   ...  ... ...
  31.  *   x:y y>0  y+1 use entry [y]
  32.  *   ...  ... ...
  33.  * x:indices-1 indices use entry [indices-1]
  34.  *   ...  ... ...
  35.  *   x:y  y+1 use entry [y & (indices-1)]
  36.  *   ...  ... ...
  37.  * 0xffff 0x10000 use entry [indices-1]
  38.  */
  39. #define NO_DEFAULT_INDEX (1 << 16)
  40. struct dsmark_qdisc_data {
  41. struct Qdisc *q;
  42. struct tcf_proto *filter_list;
  43. __u8 *mask; /* "owns" the array */
  44. __u8 *value;
  45. __u16 indices;
  46. __u32 default_index; /* index range is 0...0xffff */
  47. int set_tc_index;
  48. };
  49. /* ------------------------- Class/flow operations ------------------------- */
  50. static int dsmark_graft(struct Qdisc *sch,unsigned long arg,
  51.     struct Qdisc *new,struct Qdisc **old)
  52. {
  53. struct dsmark_qdisc_data *p = PRIV(sch);
  54. DPRINTK("dsmark_graft(sch %p,[qdisc %p],new %p,old %p)n",sch,p,new,
  55.     old);
  56. if (!new)
  57. new = &noop_qdisc;
  58. sch_tree_lock(sch);
  59. *old = xchg(&p->q,new);
  60. if (*old)
  61. qdisc_reset(*old);
  62. sch_tree_unlock(sch); /* @@@ move up ? */
  63.         return 0;
  64. }
  65. static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
  66. {
  67. struct dsmark_qdisc_data *p = PRIV(sch);
  68. return p->q;
  69. }
  70. static unsigned long dsmark_get(struct Qdisc *sch,u32 classid)
  71. {
  72. struct dsmark_qdisc_data *p __attribute__((unused)) = PRIV(sch);
  73. DPRINTK("dsmark_get(sch %p,[qdisc %p],classid %x)n",sch,p,classid);
  74. return TC_H_MIN(classid)+1;
  75. }
  76. static unsigned long dsmark_bind_filter(struct Qdisc *sch,
  77.     unsigned long parent, u32 classid)
  78. {
  79. return dsmark_get(sch,classid);
  80. }
  81. static void dsmark_put(struct Qdisc *sch, unsigned long cl)
  82. {
  83. }
  84. static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
  85.     struct rtattr **tca, unsigned long *arg)
  86. {
  87. struct dsmark_qdisc_data *p = PRIV(sch);
  88. struct rtattr *opt = tca[TCA_OPTIONS-1];
  89. struct rtattr *tb[TCA_DSMARK_MAX];
  90. DPRINTK("dsmark_change(sch %p,[qdisc %p],classid %x,parent %x),"
  91.     "arg 0x%lxn",sch,p,classid,parent,*arg);
  92. if (*arg > p->indices)
  93. return -ENOENT;
  94. if (!opt || rtattr_parse(tb, TCA_DSMARK_MAX, RTA_DATA(opt),
  95.  RTA_PAYLOAD(opt)))
  96. return -EINVAL;
  97. if (tb[TCA_DSMARK_MASK-1]) {
  98. if (!RTA_PAYLOAD(tb[TCA_DSMARK_MASK-1]))
  99. return -EINVAL;
  100. p->mask[*arg-1] = *(__u8 *) RTA_DATA(tb[TCA_DSMARK_MASK-1]);
  101. }
  102. if (tb[TCA_DSMARK_VALUE-1]) {
  103. if (!RTA_PAYLOAD(tb[TCA_DSMARK_VALUE-1]))
  104. return -EINVAL;
  105. p->value[*arg-1] = *(__u8 *) RTA_DATA(tb[TCA_DSMARK_VALUE-1]);
  106. }
  107. return 0;
  108. }
  109. static int dsmark_delete(struct Qdisc *sch,unsigned long arg)
  110. {
  111. struct dsmark_qdisc_data *p = PRIV(sch);
  112. if (!arg || arg > p->indices)
  113. return -EINVAL;
  114. p->mask[arg-1] = 0xff;
  115. p->value[arg-1] = 0;
  116. return 0;
  117. }
  118. static void dsmark_walk(struct Qdisc *sch,struct qdisc_walker *walker)
  119. {
  120. struct dsmark_qdisc_data *p = PRIV(sch);
  121. int i;
  122. DPRINTK("dsmark_walk(sch %p,[qdisc %p],walker %p)n",sch,p,walker);
  123. if (walker->stop)
  124. return;
  125. for (i = 0; i < p->indices; i++) {
  126. if (p->mask[i] == 0xff && !p->value[i])
  127. continue;
  128. if (walker->count >= walker->skip) {
  129. if (walker->fn(sch, i+1, walker) < 0) {
  130. walker->stop = 1;
  131. break;
  132. }
  133. }
  134.                 walker->count++;
  135.         }
  136. }
  137. static struct tcf_proto **dsmark_find_tcf(struct Qdisc *sch,unsigned long cl)
  138. {
  139. struct dsmark_qdisc_data *p = PRIV(sch);
  140. return &p->filter_list;
  141. }
  142. /* --------------------------- Qdisc operations ---------------------------- */
  143. static int dsmark_enqueue(struct sk_buff *skb,struct Qdisc *sch)
  144. {
  145. struct dsmark_qdisc_data *p = PRIV(sch);
  146. struct tcf_result res;
  147. int result;
  148. int ret = NET_XMIT_POLICED;
  149. D2PRINTK("dsmark_enqueue(skb %p,sch %p,[qdisc %p])n",skb,sch,p);
  150. if (p->set_tc_index) {
  151. switch (skb->protocol) {
  152. case __constant_htons(ETH_P_IP):
  153. skb->tc_index = ipv4_get_dsfield(skb->nh.iph);
  154. break;
  155. case __constant_htons(ETH_P_IPV6):
  156. skb->tc_index = ipv6_get_dsfield(skb->nh.ipv6h);
  157. break;
  158. default:
  159. skb->tc_index = 0;
  160. break;
  161. };
  162. }
  163. result = TC_POLICE_OK; /* be nice to gcc */
  164. if (TC_H_MAJ(skb->priority) == sch->handle) {
  165. skb->tc_index = TC_H_MIN(skb->priority);
  166. } else {
  167. result = tc_classify(skb,p->filter_list,&res);
  168. D2PRINTK("result %d class 0x%04xn",result,res.classid);
  169. switch (result) {
  170. #ifdef CONFIG_NET_CLS_POLICE
  171. case TC_POLICE_SHOT:
  172. kfree_skb(skb);
  173. break;
  174. #if 0
  175. case TC_POLICE_RECLASSIFY:
  176. /* FIXME: what to do here ??? */
  177. #endif
  178. #endif
  179. case TC_POLICE_OK:
  180. skb->tc_index = TC_H_MIN(res.classid);
  181. break;
  182. case TC_POLICE_UNSPEC:
  183. /* fall through */
  184. default:
  185. if (p->default_index != NO_DEFAULT_INDEX)
  186. skb->tc_index = p->default_index;
  187. break;
  188. };
  189. }
  190. if (
  191. #ifdef CONFIG_NET_CLS_POLICE
  192.     result == TC_POLICE_SHOT ||
  193. #endif
  194.     ((ret = p->q->enqueue(skb,p->q)) != 0)) {
  195. sch->stats.drops++;
  196. return ret;
  197. }
  198. sch->stats.bytes += skb->len;
  199. sch->stats.packets++;
  200. sch->q.qlen++;
  201. return ret;
  202. }
  203. static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
  204. {
  205. struct dsmark_qdisc_data *p = PRIV(sch);
  206. struct sk_buff *skb;
  207. int index;
  208. D2PRINTK("dsmark_dequeue(sch %p,[qdisc %p])n",sch,p);
  209. skb = p->q->ops->dequeue(p->q);
  210. if (!skb)
  211. return NULL;
  212. sch->q.qlen--;
  213. index = skb->tc_index & (p->indices-1);
  214. D2PRINTK("index %d->%dn",skb->tc_index,index);
  215. switch (skb->protocol) {
  216. case __constant_htons(ETH_P_IP):
  217. ipv4_change_dsfield(skb->nh.iph,
  218.     p->mask[index],p->value[index]);
  219. break;
  220. case __constant_htons(ETH_P_IPV6):
  221. ipv6_change_dsfield(skb->nh.ipv6h,
  222.     p->mask[index],p->value[index]);
  223. break;
  224. default:
  225. /*
  226.  * Only complain if a change was actually attempted.
  227.  * This way, we can send non-IP traffic through dsmark
  228.  * and don't need yet another qdisc as a bypass.
  229.  */
  230. if (p->mask[index] != 0xff || p->value[index])
  231. printk(KERN_WARNING "dsmark_dequeue: "
  232.        "unsupported protocol %dn",
  233.        htons(skb->protocol));
  234. break;
  235. };
  236. return skb;
  237. }
  238. static int dsmark_requeue(struct sk_buff *skb,struct Qdisc *sch)
  239. {
  240. int ret;
  241. struct dsmark_qdisc_data *p = PRIV(sch);
  242. D2PRINTK("dsmark_requeue(skb %p,sch %p,[qdisc %p])n",skb,sch,p);
  243.         if ((ret = p->q->ops->requeue(skb, p->q)) == 0) {
  244. sch->q.qlen++;
  245. return 0;
  246. }
  247. sch->stats.drops++;
  248. return ret;
  249. }
  250. static int dsmark_drop(struct Qdisc *sch)
  251. {
  252. struct dsmark_qdisc_data *p = PRIV(sch);
  253. DPRINTK("dsmark_reset(sch %p,[qdisc %p])n",sch,p);
  254. if (!p->q->ops->drop)
  255. return 0;
  256. if (!p->q->ops->drop(p->q))
  257. return 0;
  258. sch->q.qlen--;
  259. return 1;
  260. }
  261. int dsmark_init(struct Qdisc *sch,struct rtattr *opt)
  262. {
  263. struct dsmark_qdisc_data *p = PRIV(sch);
  264. struct rtattr *tb[TCA_DSMARK_MAX];
  265. __u16 tmp;
  266. DPRINTK("dsmark_init(sch %p,[qdisc %p],opt %p)n",sch,p,opt);
  267. if (rtattr_parse(tb,TCA_DSMARK_MAX,RTA_DATA(opt),RTA_PAYLOAD(opt)) < 0 ||
  268.     !tb[TCA_DSMARK_INDICES-1] ||
  269.     RTA_PAYLOAD(tb[TCA_DSMARK_INDICES-1]) < sizeof(__u16))
  270.                 return -EINVAL;
  271. memset(p,0,sizeof(*p));
  272. p->filter_list = NULL;
  273. p->indices = *(__u16 *) RTA_DATA(tb[TCA_DSMARK_INDICES-1]);
  274. if (!p->indices)
  275. return -EINVAL;
  276. for (tmp = p->indices; tmp != 1; tmp >>= 1) {
  277. if (tmp & 1)
  278. return -EINVAL;
  279. }
  280. p->default_index = NO_DEFAULT_INDEX;
  281. if (tb[TCA_DSMARK_DEFAULT_INDEX-1]) {
  282. if (RTA_PAYLOAD(tb[TCA_DSMARK_DEFAULT_INDEX-1]) < sizeof(__u16))
  283. return -EINVAL;
  284. p->default_index =
  285.     *(__u16 *) RTA_DATA(tb[TCA_DSMARK_DEFAULT_INDEX-1]);
  286. }
  287. p->set_tc_index = !!tb[TCA_DSMARK_SET_TC_INDEX-1];
  288. p->mask = kmalloc(p->indices*2,GFP_KERNEL);
  289. if (!p->mask)
  290. return -ENOMEM;
  291. p->value = p->mask+p->indices;
  292. memset(p->mask,0xff,p->indices);
  293. memset(p->value,0,p->indices);
  294. if (!(p->q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)))
  295. p->q = &noop_qdisc;
  296. DPRINTK("dsmark_init: qdisc %pn",&p->q);
  297. MOD_INC_USE_COUNT;
  298. return 0;
  299. }
  300. static void dsmark_reset(struct Qdisc *sch)
  301. {
  302. struct dsmark_qdisc_data *p = PRIV(sch);
  303. DPRINTK("dsmark_reset(sch %p,[qdisc %p])n",sch,p);
  304. qdisc_reset(p->q);
  305. sch->q.qlen = 0;
  306. }
  307. static void dsmark_destroy(struct Qdisc *sch)
  308. {
  309. struct dsmark_qdisc_data *p = PRIV(sch);
  310. struct tcf_proto *tp;
  311. DPRINTK("dsmark_destroy(sch %p,[qdisc %p])n",sch,p);
  312. while (p->filter_list) {
  313. tp = p->filter_list;
  314. p->filter_list = tp->next;
  315. tp->ops->destroy(tp);
  316. }
  317. qdisc_destroy(p->q);
  318. p->q = &noop_qdisc;
  319. kfree(p->mask);
  320. MOD_DEC_USE_COUNT;
  321. }
  322. static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
  323.     struct sk_buff *skb, struct tcmsg *tcm)
  324. {
  325. struct dsmark_qdisc_data *p = PRIV(sch);
  326. unsigned char *b = skb->tail;
  327. struct rtattr *rta;
  328. DPRINTK("dsmark_dump_class(sch %p,[qdisc %p],class %ldn",sch,p,cl);
  329. if (!cl || cl > p->indices)
  330. return -EINVAL;
  331. tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle),cl-1);
  332. rta = (struct rtattr *) b;
  333. RTA_PUT(skb,TCA_OPTIONS,0,NULL);
  334. RTA_PUT(skb,TCA_DSMARK_MASK,1,&p->mask[cl-1]);
  335. RTA_PUT(skb,TCA_DSMARK_VALUE,1,&p->value[cl-1]);
  336. rta->rta_len = skb->tail-b;
  337. return skb->len;
  338. rtattr_failure:
  339. skb_trim(skb,b-skb->data);
  340. return -1;
  341. }
  342. static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
  343. {
  344. struct dsmark_qdisc_data *p = PRIV(sch);
  345. unsigned char *b = skb->tail;
  346. struct rtattr *rta;
  347. rta = (struct rtattr *) b;
  348. RTA_PUT(skb,TCA_OPTIONS,0,NULL);
  349. RTA_PUT(skb,TCA_DSMARK_INDICES,sizeof(__u16),&p->indices);
  350. if (p->default_index != NO_DEFAULT_INDEX) {
  351. __u16 tmp = p->default_index;
  352. RTA_PUT(skb,TCA_DSMARK_DEFAULT_INDEX, sizeof(__u16), &tmp);
  353. }
  354. if (p->set_tc_index)
  355. RTA_PUT(skb, TCA_DSMARK_SET_TC_INDEX, 0, NULL);
  356. rta->rta_len = skb->tail-b;
  357. return skb->len;
  358. rtattr_failure:
  359. skb_trim(skb,b-skb->data);
  360. return -1;
  361. }
  362. static struct Qdisc_class_ops dsmark_class_ops =
  363. {
  364. dsmark_graft, /* graft */
  365. dsmark_leaf, /* leaf */
  366. dsmark_get, /* get */
  367. dsmark_put, /* put */
  368. dsmark_change, /* change */
  369. dsmark_delete, /* delete */
  370. dsmark_walk, /* walk */
  371. dsmark_find_tcf, /* tcf_chain */
  372. dsmark_bind_filter, /* bind_tcf */
  373. dsmark_put, /* unbind_tcf */
  374. dsmark_dump_class, /* dump */
  375. };
  376. struct Qdisc_ops dsmark_qdisc_ops =
  377. {
  378. NULL, /* next */
  379. &dsmark_class_ops, /* cl_ops */
  380. "dsmark",
  381. sizeof(struct dsmark_qdisc_data),
  382. dsmark_enqueue, /* enqueue */
  383. dsmark_dequeue, /* dequeue */
  384. dsmark_requeue, /* requeue */
  385. dsmark_drop, /* drop */
  386. dsmark_init, /* init */
  387. dsmark_reset, /* reset */
  388. dsmark_destroy, /* destroy */
  389. NULL, /* change */
  390. dsmark_dump /* dump */
  391. };
  392. #ifdef MODULE
  393. int init_module(void)
  394. {
  395. return register_qdisc(&dsmark_qdisc_ops);
  396. }
  397. void cleanup_module(void) 
  398. {
  399. unregister_qdisc(&dsmark_qdisc_ops);
  400. }
  401. #endif
  402. MODULE_LICENSE("GPL");