act_gact.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * net/sched/gact.c Generic actions
  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. * copyright Jamal Hadi Salim (2002-4)
  10. *
  11. */
  12. #include <asm/uaccess.h>
  13. #include <asm/system.h>
  14. #include <linux/bitops.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/socket.h>
  21. #include <linux/sockios.h>
  22. #include <linux/in.h>
  23. #include <linux/errno.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/rtnetlink.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/proc_fs.h>
  31. #include <net/sock.h>
  32. #include <net/pkt_sched.h>
  33. #include <linux/tc_act/tc_gact.h>
  34. #include <net/tc_act/tc_gact.h>
  35. /* use generic hash table */
  36. #define MY_TAB_SIZE 16
  37. #define MY_TAB_MASK 15
  38. static u32 idx_gen;
  39. static struct tcf_gact *tcf_gact_ht[MY_TAB_SIZE];
  40. static DEFINE_RWLOCK(gact_lock);
  41. /* ovewrride the defaults */
  42. #define tcf_st tcf_gact
  43. #define tc_st tc_gact
  44. #define tcf_t_lock gact_lock
  45. #define tcf_ht tcf_gact_ht
  46. #define CONFIG_NET_ACT_INIT 1
  47. #include <net/pkt_act.h>
  48. #ifdef CONFIG_GACT_PROB
  49. static int gact_net_rand(struct tcf_gact *p)
  50. {
  51. if (net_random()%p->pval)
  52. return p->action;
  53. return p->paction;
  54. }
  55. static int gact_determ(struct tcf_gact *p)
  56. {
  57. if (p->bstats.packets%p->pval)
  58. return p->action;
  59. return p->paction;
  60. }
  61. typedef int (*g_rand)(struct tcf_gact *p);
  62. static g_rand gact_rand[MAX_RAND]= { NULL, gact_net_rand, gact_determ };
  63. #endif
  64. static int tcf_gact_init(struct rtattr *rta, struct rtattr *est,
  65. struct tc_action *a, int ovr, int bind)
  66. {
  67. struct rtattr *tb[TCA_GACT_MAX];
  68. struct tc_gact *parm;
  69. struct tcf_gact *p;
  70. int ret = 0;
  71. if (rta == NULL || rtattr_parse_nested(tb, TCA_GACT_MAX, rta) < 0)
  72. return -EINVAL;
  73. if (tb[TCA_GACT_PARMS - 1] == NULL ||
  74. RTA_PAYLOAD(tb[TCA_GACT_PARMS - 1]) < sizeof(*parm))
  75. return -EINVAL;
  76. parm = RTA_DATA(tb[TCA_GACT_PARMS - 1]);
  77. if (tb[TCA_GACT_PROB-1] != NULL)
  78. #ifdef CONFIG_GACT_PROB
  79. if (RTA_PAYLOAD(tb[TCA_GACT_PROB-1]) < sizeof(struct tc_gact_p))
  80. return -EINVAL;
  81. #else
  82. return -EOPNOTSUPP;
  83. #endif
  84. p = tcf_hash_check(parm->index, a, ovr, bind);
  85. if (p == NULL) {
  86. p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
  87. if (p == NULL)
  88. return -ENOMEM;
  89. ret = ACT_P_CREATED;
  90. } else {
  91. if (!ovr) {
  92. tcf_hash_release(p, bind);
  93. return -EEXIST;
  94. }
  95. }
  96. spin_lock_bh(&p->lock);
  97. p->action = parm->action;
  98. #ifdef CONFIG_GACT_PROB
  99. if (tb[TCA_GACT_PROB-1] != NULL) {
  100. struct tc_gact_p *p_parm = RTA_DATA(tb[TCA_GACT_PROB-1]);
  101. p->paction = p_parm->paction;
  102. p->pval = p_parm->pval;
  103. p->ptype = p_parm->ptype;
  104. }
  105. #endif
  106. spin_unlock_bh(&p->lock);
  107. if (ret == ACT_P_CREATED)
  108. tcf_hash_insert(p);
  109. return ret;
  110. }
  111. static int
  112. tcf_gact_cleanup(struct tc_action *a, int bind)
  113. {
  114. struct tcf_gact *p = PRIV(a, gact);
  115. if (p != NULL)
  116. return tcf_hash_release(p, bind);
  117. return 0;
  118. }
  119. static int
  120. tcf_gact(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
  121. {
  122. struct tcf_gact *p = PRIV(a, gact);
  123. int action = TC_ACT_SHOT;
  124. spin_lock(&p->lock);
  125. #ifdef CONFIG_GACT_PROB
  126. if (p->ptype && gact_rand[p->ptype] != NULL)
  127. action = gact_rand[p->ptype](p);
  128. else
  129. action = p->action;
  130. #else
  131. action = p->action;
  132. #endif
  133. p->bstats.bytes += skb->len;
  134. p->bstats.packets++;
  135. if (action == TC_ACT_SHOT)
  136. p->qstats.drops++;
  137. p->tm.lastuse = jiffies;
  138. spin_unlock(&p->lock);
  139. return action;
  140. }
  141. static int
  142. tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  143. {
  144. unsigned char *b = skb->tail;
  145. struct tc_gact opt;
  146. struct tcf_gact *p = PRIV(a, gact);
  147. struct tcf_t t;
  148. opt.index = p->index;
  149. opt.refcnt = p->refcnt - ref;
  150. opt.bindcnt = p->bindcnt - bind;
  151. opt.action = p->action;
  152. RTA_PUT(skb, TCA_GACT_PARMS, sizeof(opt), &opt);
  153. #ifdef CONFIG_GACT_PROB
  154. if (p->ptype) {
  155. struct tc_gact_p p_opt;
  156. p_opt.paction = p->paction;
  157. p_opt.pval = p->pval;
  158. p_opt.ptype = p->ptype;
  159. RTA_PUT(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt);
  160. }
  161. #endif
  162. t.install = jiffies_to_clock_t(jiffies - p->tm.install);
  163. t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
  164. t.expires = jiffies_to_clock_t(p->tm.expires);
  165. RTA_PUT(skb, TCA_GACT_TM, sizeof(t), &t);
  166. return skb->len;
  167. rtattr_failure:
  168. skb_trim(skb, b - skb->data);
  169. return -1;
  170. }
  171. static struct tc_action_ops act_gact_ops = {
  172. .kind = "gact",
  173. .type = TCA_ACT_GACT,
  174. .capab = TCA_CAP_NONE,
  175. .owner = THIS_MODULE,
  176. .act = tcf_gact,
  177. .dump = tcf_gact_dump,
  178. .cleanup = tcf_gact_cleanup,
  179. .lookup = tcf_hash_search,
  180. .init = tcf_gact_init,
  181. .walk = tcf_generic_walker
  182. };
  183. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  184. MODULE_DESCRIPTION("Generic Classifier actions");
  185. MODULE_LICENSE("GPL");
  186. static int __init
  187. gact_init_module(void)
  188. {
  189. #ifdef CONFIG_GACT_PROB
  190. printk("GACT probability on\n");
  191. #else
  192. printk("GACT probability NOT on\n");
  193. #endif
  194. return tcf_register_action(&act_gact_ops);
  195. }
  196. static void __exit
  197. gact_cleanup_module(void)
  198. {
  199. tcf_unregister_action(&act_gact_ops);
  200. }
  201. module_init(gact_init_module);
  202. module_exit(gact_cleanup_module);