act_gact.c 5.2 KB

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